//
// 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