页面传值之观察者模式nsnotifsfication

6 NSNotification  (观察者模式)

实现功能:第一个页面点击button,进入第二个页面,选中一条记录,按select按钮,把值传回第一个页面的textfield

 

//

//  ViewController.m

//  demo1

//

//  Created by jessewang on 11/29/11.

//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.

//

- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:nil action:nil];

    self.navigationItem.backBarButtonItem = backButton;

    [backButton release];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(showOutput:) name:@"myNotification"object:nil];

}

-(void) showOutput:(NSNotification *)note

{

    NSLog(@"%@",note);

    self.output.text = note.object;


}



 

//

//  NextView.m

//  demo1

//

//  Created by jessewang on 11/29/11.

//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.

//

 

- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib.

    NSArray *myarray = [[NSArray allocinitWithObjects:@"A",@"B",@"C",@"D",@"E",nil];

    self.array = myarray;

    [myarray release];

    

    UIBarButtonItem *selectButton = [[UIBarButtonItem alloc] initWithTitle:@"select" style:UIBarButtonItemStyleBordered target:self action:@selector(selectButton:)];

    

    self.navigationItem.rightBarButtonItem = selectButton;

    

    [selectButton release];    

    

}


- (void)viewDidUnload

{

    [super viewDidUnload];

    // Release any retained subviews of the main view.

    // e.g. self.myOutlet = nil;

    self.lastIndexPath = nil;

    self.array = nil;

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    // Return YES for supported orientations

    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    // Return the number of rows in the section.

    return [self.array count];

}


// Customize the appearance of table view cells.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];

    }

    NSUInteger row = [indexPath row];

    NSUInteger oldRow = [self.lastIndexPath row];

    cell.textLabel.text = [array objectAtIndex:row];

    cell.accessoryType = (row == oldRow && lastIndexPath != nil) ? UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;

    

    return cell;

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

{

     //choose one

     int newRow = [indexPath row];

     int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;

     if(newRow != oldRow)

     {

     UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];

     newCell.accessoryType = UITableViewCellAccessoryCheckmark;

     

     UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastIndexPath];

     oldCell.accessoryType = UITableViewCellAccessoryNone;

     

     self.value = [self.array objectAtIndex:[indexPath row]];

     self.lastIndexPath = indexPath;

     }

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

 

-(IBAction)selectButton:(id)sender

{

    [[NSNotificationCenter defaultCenter] postNotificationName:@"myNotification" object:self.value]; 

    [self.navigationController popViewControllerAnimated:YES];

              

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值