几个页面跳转以及传值(通知传值)

来源:互联网 发布:ubuntu tty 编辑:程序博客网 时间:2024/05/01 00:23

A→B→C→D→A

其中A不是根目录,但是在D上还要跳转回去,并且不用alloc,这时我们可以用如下方法

[self.navigationController popToViewController: [self.navigationController.viewControllers objectAtIndex: ([self.navigationController.viewControllers count] -4)] animated:YES];


并且D要传值给A,这时通知就很好用了

通知传值(一个发送者,一堆接收者,不需要代理协议,直接通过系统的通知中心工作notificationCenter)

在传值页面

首先弄个系统通知中心

NSNotificationCenter * center = [NSNotificationCenter defaultCenter];

 NSNotification * notify = [NSNotification notificationWithName:@"regisonName" object:[NSString stringWithFormat:@"%@",[self.dataArray[indexPath.row] objectForKey:@"name"]]];

//第一个参数是通知的名字,就是标示,名一个名字,以备你用这个名字去找对象 第二个参数就是你要传的值,接收要接收的值,这个值必须是对象
    [center postNotification:notify];

在接收页面

//NSNotificationCenter * center = [NSNotificationCenter defaultCenter];

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(setRegionTitle:)
                                                 name:@"regisonName"

                                             object:nil];

//第一个参数:将要干活的对象   第二个参数(方法名,将要干的活)   第三个参数(干活的名字,要与发送者一致)   第四个参数(系统的,暂时不用管)

- (void)setRegionTitle:(NSNotification *)notify{
    NSString * object = [notify object];
    [_regionButton setTitle:object forState:UIControlStateNormal];
    [_regionButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}

0 0