控制器之间的通信(传值方法)以及多次调用通信
//1.调用通知进行回调方法
//A控制器中 - (void)toDo{ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test) name:@"testName" object:nil]; } //当B控制器中的viewDidLoad方法调用时,此方法会被触发 -(void)test{ NSLog(@"已回调");
//先移除通知,再重新注册(否则连续多次调用通信时,此方法会调用多次)
[[NSNotificationCenter defaultCenter]removeObserver:self name:@"testName" object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(test) name:@"testName" object:nil]; } //===================== //B控制器中 - (void)viewDidLoad{ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test) name:@"testName" object:nil]; }
//2.通过AppDelegate传值取值
//A控制器
通过AppDelegate传值(在当前控制器中导入AppDelegate.h) AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; appDelegate.address = _address; //B控制器
通过AppDelegate取值(在当前控制器中导入AppDelegate.h) AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; _address = appDelegate.address;