您的位置:首页 > 产品设计 > UI/UE

UI08_UITableView界面传值(后往前)

2015-08-08 16:35 295 查看
应用的方法还是协议模式

在传值的试图控制器SecondViewController的.h文件中

@protocol SencondViewControllerDelegate <NSObject>
-(void)changeValue:(NSString *)str;
@end
定义所用的属性
@interface SencondViewController : UIViewController
@property (nonatomic,copy)NSString *name;
@property (nonatomic,assign)id<SencondViewControllerDelegate>delegate;
@end


SecondViewController.m

定义所需要的属性:
@interface SencondViewController ()
@property (nonatomic,retain)UILabel *label;
@property (nonatomic,retain)UITextField *textField;
@property (nonatomic,retain)UIButton *button;
@end
属性内存释放:
- (void)dealloc
{
[_label release];
[_textField release];
[_name release];
[super dealloc];
}
ViewDidLoad方法里创建所需的控件
如下:
self.view.backgroundColor=[UIColor redColor];
self.label=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 40)];
self.label.layer.borderWidth=1;
self.label.layer.cornerRadius=10;

self.label.text=self.name;
[self.view addSubview:self.label];
[_label release];
self.textField=[[UITextField alloc]initWithFrame:CGRectMake(100, 200, 100, 40)];
self.textField.layer.borderWidth=1;
self.textField.layer.cornerRadius=10;
[self.view addSubview:self.textField];
[_textField release];

self.button=[UIButton buttonWithType:UIButtonTypeSystem];
[self.button setTitle:@"返回" forState:UIControlStateNormal];
self.button.layer.borderWidth=1;
self.button.frame=CGRectMake(100, 300, 100, 40);
[self.view addSubview:self.button];
[self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

点击方法
-(void)buttonAction:(UIButton *)button{
[self.navigationController popToRootViewControllerAnimated:YES];
[self.delegate changeValue:self.textField.text];
}


被传值的MainViewContrller.m文件中

在此文件中对tableView进行初始值设置,向其内添加视图文件,签署两个协议实现它的两个必须实现的方法和点击方法
我们在它的点击方法里,设置相关的协议内容
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SencondViewController *second=[[SencondViewController alloc]init];
second.delegate=self;
second.name=self.arr[indexPath.row];
[self.navigationController pushViewController:second animated:YES];
}

实现协议方法
-(void)changeValue:(NSString *)str
{
//属性的数组,相当于是数据源,把传过来的值添加到数组中
[self.arr addObject:str];
//对tableView进行刷新操作
[self.tableView reloadData];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: