iOS UI11_BLOCK传值
2015-08-13 08:46
603 查看
// // 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 ()<UITableViewDataSource,UITableViewDelegate> @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 <UIKit/UIKit.h> 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
相关文章推荐
- 关于ui中的表视图
- IOS开发UI阶段 第五周
- iOS UI11_UITabBar
- OC中UITableView的属性用法
- EF6 CodeFirst+Repository+Ninject+MVC4+EasyUI实践(完)
- HDU 4273 Rescue(三维凸包+重心模板)
- ConditionalWeakTable<TKey, TValue>使编译器可以将对象字段动态附加到托管对象。
- iOS的UILabel设置居上对齐,居中对齐,居下对齐
- Leetcode: Repeated DNA Sequences
- MySQL 5.6 中TIMESTAMP with implicit DEFAULT value is deprecated错误
- php中include()和require()的区别
- php中include()和require()的区别
- Implement Stack using Queues
- could not build module 'uikit'
- Java集合类: Set、List、Map、Queue使用场景梳理
- uva 1152 4 Values whose Sum is 0
- 【UITextField与UITextView】
- 【UISegmentedControl】
- HDU 3397 Sequence operation(区间合并 + 区间更新)
- Request Parameters and Header Values