当前位置:首页 > 联系方式 >

iOS UI11_BLOCK传值

  • 2017-09-09 10:31:53
  • 浏览:
  • 作者:admin
iOS UI11_BLOCK传值 // // MainViewController.m // UI11_block练习 // // Created by dllo on 15/8/12. // Copyright (c) 2015年 zhozhicheng. All rights reserved. // #import MainViewController.h #import SecondViewController.h @interface MainViewController () @property(nonatomic,retain)NSMutableArray *arr; @property(nonatomic,retain)UITableView *tableView; @end @implementation MainViewController -(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.arr = [NSMutableArray arrayWithObjects:@宋江, @卢俊义, @吴用, @公孙胜, @关胜, @林冲, @秦明 ,@呼延灼 , @花容,@柴进, @李应, @朱仝,@鲁智深,@武松,nil]; }return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.navigationController.navigationBar.translucent=NO; self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.height-64) style:UITableViewStylePlain]; [self.view addSubview:self.tableView]; [self.tableView release]; self.tableView.delegate=self; self.tableView.dataSource=self; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.arr.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *reuse=@reuse; UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:reuse]; if (!cell) { cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease]; } cell.textLabel.text=self.arr[indexPath.row]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { #pragma mark block作为属性的时候,为了防止block进入到栈区,栈区内存不需要我们进行管理,很可能出现block消失的情况,所以需要拷贝一份到堆区,这样能防止block在用的时候消失 SecondViewController *secVC=[[SecondViewController alloc] init]; [self.navigationController pushViewController:secVC animated:YES]; [secVC release]; ////2. //写一个参数是nsstring *的block void(^block)(NSString *)=^(NSString *str){ NSLog(@%@,str); //处理数据 [self.arr addObject:str]; [tableView reloadData]; }; secVC.block=block; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end // // SecondViewController.h // UI11_block练习 // // Created by dllo on 15/8/12. // Copyright (c) 2015年 zhozhicheng. All rights reserved. // #import typedef void(^Block)(NSString *); @interface SecondViewController : UIViewController ////1. @property(nonatomic,copy)Block block; @end // // SecondViewController.m // UI11_block练习 // // Created by dllo on 15/8/12. // Copyright (c) 2015年 zhozhicheng. All rights reserved. // #import SecondViewController.h @interface SecondViewController () @end @implementation SecondViewController -(void)dealloc { //block自己的release方法 Block_release(_block); } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor=[UIColor cyanColor]; UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame = CGRectMake(100, 100, 150, 40); button.layer.borderWidth = 1; button.layer.cornerRadius = 10; [self.view addSubview:button]; [button setTitle:@返回 forState:UIControlStateNormal]; [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; } -(void)click:(UIButton *)button { [self.navigationController popToRootViewControllerAnimated:YES]; ////3. self.block(@跳洞虎 陈达); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end

原始状态


点击任意一个


点击返回,得到传值

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:天门网站建设 http://tianmen.45qun.com

上一篇:学习ASP.NET MVC5框架揭秘笔记-ASP.NET MVC是如何运行

下一篇:最后一页

相关文章