ios 通知机制

在A页面进行操作,使得B页面有所更改的时候需要用到通知机制,在A页面发送通知,在B页面接收A页面发来的通知从而对B页面进行操作。

首先在viewDidload里注册一个通知监听器

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

 //第一个参数是观察者为本身第二个参数表示消息回调的方法第三个消息通知的名字第四个为nil表示表示接受所有发送者的消息~

在需要发送通知的界面

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *resName =[self.restaurantList objectAtIndex:[indexPath row]];
//餐厅的名字
[[NSNotificationCenter defaultCenter]postNotificationName:@"rest_choose"(自己起的名) object:resName userInfo:nil];
//在点击餐厅名字时发送通知
[self. navigationController popViewControllerAnimated:YES];
//在点击餐厅的名字时回到上一个view
}

  

在接收通知的界面的回调方法

-(void)notificationNamerest:(NSNotification *)rnotification
{
    self.restName.text = rnotification.object;
    
//    反馈选餐厅名字的通知
    
    if (self.restName.text != nil)
    {
        [m_btnPackage setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [m_btnPackage setEnabled:YES];//选了餐厅之后设置套餐按钮可以点击
    }
}

最终实现的效果是点击餐厅的名字 餐厅的名字会显示在self.restName.text里  

猜你喜欢

转载自404530969.iteye.com/blog/2258413
今日推荐