接下来,我们要修改的是tabBar容器下的第2个子页面,界面如下。
点击“添加”按键,跳转至添加界面,
在UITextView文本框内输入字符,再点击“确认添加”按键,返回上一界面,并将输入的字符呈现在上一界面的UILabel标签中。
首先,为第2个子界面进行布局,用到了UILabel、UIButton组件,代码如下。
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title=@"联系人";
self.friendsLabel=[[UILabel alloc]init];
self.friendsLabel.frame=CGRectMake(70, 210, 280, 220);
UIColor *color=[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]];
[self.friendsLabel setBackgroundColor:color]; //设置标签的背景图片
self.friendsLabel.textAlignment=NSTextAlignmentCenter;
self.friendsLabel.numberOfLines=0; //支持换行输入
[self.view addSubview:self.friendsLabel];
UIButton *jumpBut=[UIButton buttonWithType:UIButtonTypeRoundedRect];
jumpBut.frame=CGRectMake(130, 500, 150, 60);
[jumpBut setTitle:@"添加" forState:UIControlStateNormal];
[jumpBut addTarget:self action:@selector(add) forControlEvents:UIControlEventTouchUpInside]; //点击事件触发按键跳转至add方法
[jumpBut setBackgroundColor:[UIColor colorWithRed:0.7 green:0.1 blue:0.1 alpha:0.5]];
[jumpBut setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
jumpBut.titleLabel.font=[UIFont systemFontOfSize:17];//设置按键的字体大小
[self.view addSubview:jumpBut];
}
add方法定义如下。
-(void)add{
self.addView=[[AddViewController alloc]init];
self.addView.delegate=self; //指明secondviewcontroller是addviewcontroller的代理
self.addView.hidesBottomBarWhenPushed=YES;//当跳转到下一界面时,可以隐藏底部的bar
[self.navigationController pushViewController:self.addView animated:YES]; //跳转至下一界面
}
按键触发至下一个添加界面,然后通过
代理传值将添加界面的UITextView的值传递回上一界面,呈现在UILabel中
[ 视图从A跳转到B
B→ A , B界面的值传递回A界面
逆向传递,界面传值中的属性传值无法完成,需要用到界面传值中的代理传值delegate
代理传值就是B将事情委托给A来做,所以在B中定义delegate,在A中实现delegate ]
首先添加新的类,命名为AddViewController ,[ 对应于该demo ,A是SecondViewController,B是AddViewController ]
在AddViewController.h中声明AddProtocol协议,在B的协议中声明addFriends方法,而addFriends方法的具体实现则是由A定义。
#import <UIKit/UIKit.h>
@protocol AddProtocol <NSObject> //定义协议
-(void)addFriends:(NSString *)friends; //这是协议定义的方法,用于传值,值的类型是字符串
@end
@interface AddViewController : UIViewController
@property id <AddProtocol> delegate; //利用协议来实现代理
-(void)backItem;
@end
然后在AddViewController.h中添加UITextView和UIButton,并为按键注册点击事件
- (void)viewDidLoad {
[super viewDidLoad];
self.title=@"添加";
self.view.backgroundColor=[UIColor whiteColor];
self.tx=[[UITextView alloc]init];
self.tx.frame=CGRectMake(60, 200, 300, 140);
self.tx.layer.borderColor=[UIColor blackColor].CGColor;
self.tx.layer.borderWidth=1;
[self.tx setFont:[UIFont fontWithName:@"Arial" size:20]]; //设置文本框里字体的大小
[self.view addSubview:self.tx];
UIButton *back=[UIButton buttonWithType:UIButtonTypeRoundedRect];
back.frame=CGRectMake(130, 450, 150, 60);
[back setTitle:@"确认添加" forState:UIControlStateNormal];
back.layer.borderWidth=1;
[back setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
back.layer.borderColor=[UIColor blackColor].CGColor ;
[back addTarget:self action:@selector(backItem) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:back];
}
-(void)backItem{
[self.delegate addFriends:self.tx.text]; //使用协议传递数据
[self.navigationController popViewControllerAnimated:YES]; //返回到上一界面
}
在SecondViewController.m中定义addFriends方法的具体实现,
-(void)addFriends:(NSString *)friends{ //实现协议定义的方法
self.friendsLabel.text=friends;
}
至此,基本实现。