页面传值之观察者模式nsnotifsfication

来源:互联网 发布:网络用语bs是什么意思 编辑:程序博客网 时间:2024/06/18 15:02
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];

              

}

原创粉丝点击