首先在处理iOS-UI中,也许在不少地方须要用到两种甚至多种不一样界面之间的传值,相比这也是不少iOS入门成员头疼问题,一样做为新手的我在接触这类传值时候也一脸懵然,通过一段时间的研究,对于简单的传值有必定的了解,下面依次对属性传值,代理传值作必定的说明,但愿对于部分有须要的朋友有帮助。ide
1、属性传值:ui
经过图片能够看出,属性传值的是当前界面日后面的界面传值。首先根据我的的须要建立两个不一样的界面,在前面的atom
UITextField中输入须要的值而后点击跳转就能够进入下一届面,而后对应的值会显示在下界面的提示框中:这里用两个例子(firstViewController,secondViewController),在secondViewController的.h文件中声明属性:设计
@property(nonatomic,strong)NSString *textString;(属性传值第一步)3d
而后在.m文件中声明属性代理
@property(nonatomic,strong)UILabel *label;orm
接着:对象
- (void)viewDidLoad {blog
[super viewDidLoad];图片
self.view.backgroundColor = [UIColor whiteColor];
//配置导航栏标题
self.navigationItem.title = @"属性";
[self uibuttongs];
//建立lable
[self customLable];
//属性传值第三步
//将属性中存储的值取出来赋值给_lable.text
_label.text= _textString;
}
- (void)dealloc{
self.textString = nil;
}
- (void)customLable{
//建立lable对象
_label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 120, 40)];
_label.layer.cornerRadius = 7;
_label.backgroundColor = [UIColor cyanColor];
_label.textColor = [UIColor blackColor];
[self.view addSubview:_label];
}
- (void)uibuttongs{
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 160, 60, 30)];
button.backgroundColor =[UIColor lightGrayColor];
[button setTitle:@"上一页" forState:UIControlStateNormal];
[button addTarget:self action:@selector(acction_butong:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)acction_butong:(UIButton *)sender//这里是返回上界面
{
[self.navigationController popViewControllerAnimated:YES];
}
而后只须要在 firstViewController中的.m文件完成以下声明属性:
@property(nonatomic,strong)UITextField *textField;
而后:
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//配置导航栏标题
self.tabBarItem.title = @"属性传值";
//建立导航栏右功能键
[self customRightItem];
//建立textField
[self customTextFied];
}
#pragma mark - 建立导航栏右侧按钮
- (void)customTextFied{
//建立控件
_textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 120, 40)];
_textField.borderStyle = UITextBorderStyleRoundedRect;
_textField.layer.borderWidth = 1;
[self.view addSubview:_textField];
}
#pragma mark - 建立导航栏右侧按钮
- (void)customRightItem{
UIBarButtonItem *rightItem =[[UIBarButtonItem alloc]initWithTitle:@"跳转" style:UIBarButtonItemStyleDone target:self action:@selector(handleRight:)];
self.navigationItem.rightBarButtonItem = rightItem;
}
#pragma mark - 导航栏右侧按钮关联方法
- (void)handleRight:(UIBarButtonItem *)sender{
//建立跳转页面视图
scendViewController *scender = [[scendViewController alloc]init];
//属性传值第二步
//在push以前将——textField的值取出来赋值给second.textString
scender.textString = _textField.text;
[self.navigationController pushViewController:scender animated:YES];
}
2、代理传值:
a.后一个页面(找代理) 制定协议
b.写delegate属性
c.在合适的时候, 让代理执行协议方法
d.前一个页面(成为代理) 创建关系
e.遵照协议
f.实现协议方法
g.通知代理执行协议方法
*/
一、二、
三、
这里依旧用(firstViewController,secondViewController)两个界面来作例子,具体的界面根据我的的需求来定义声明。
从图片不难看出代理传值时一种传值方式从后界面往前界面往前传值的过程,首先在secondViewController的.h文件中制定协议;
@protocol senderViewControllerDelegate <NSObject>
//代理传值第一步:制定协议
//根据要传的数据类型设计方法的参数
- (void)passValue:(NSString *)string;
@end
而后定义属性:
@property(nonatomic,assign)id<senderViewControllerDelegate>delegate;
//代理传值第二步,定义代理协议属性
而后在其.m文件中封装的属性:
@property(nonatomic,strong)UITextField *textField;
接着:
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(hengde:)];
_textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, 220, 40)];
_textField.layer.borderWidth = 1;
[self.view addSubview:_textField];
}
- (void)hengde:(UIBarButtonItem *)sender{
//代理传值第六步:在pop以前代理对象执行协议中的方法
if ([_delegate respondsToSelector:@selector(passValue:)]) {
[_delegate passValue:_textField.text];
}
[self.navigationController popViewControllerAnimated:YES];
}
完成在 secondViewController的操做后,在firstViewController中.m文件的操做:
//代理传值第四步:代理对象所在的类遵循协议
@interface firstViewController ()<senderViewControllerDelegate>
@property(nonatomic,strong)UILabel *lable;//封装属性
@end
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"代理";
_lable = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 120, 40)];
_lable.layer.borderWidth = 1;
[self.view addSubview:_lable];
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(130, 180, 100, 50)];
[button setTitle:@"下一页" forState:UIControlStateNormal];
[button addTarget:self action:@selector(hended:) forControlEvents:UIControlEventTouchUpInside];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.view addSubview:button];
}
- (void)hended:(UIBarButtonItem *)sender{
senderViewController *sendedfr = [[senderViewController alloc]init];
//代理传值第三步:指定代理对象
sendedfr.delegate = self;
[self.navigationController pushViewController:sendedfr animated:YES];
}
//代理传值第五步:实现协议方法
- (void)passValue:(NSString *)string{
_lable.text = string;
}
而后代理的简单传值方式大概即便这样!
附:别忘了在firstViewController或者secondVIewController中导入对应的文件.h文件!