Using segues and nsnotificationcenter
-
07-12-2019 - |
Frage
I have 2 viewcontrollers and I want to send a notification out from one to the other and have a label changed based on the name of the notification (pressing a UIButton). I just started using segues and found they are a very useful way to get from one view to another. The problem I am facing is that using a segue (modal at the moment), the second view controller is not receiving the notification. I believe that segues release and create new view controllers when used, not sure. Is there any way around this?
Thanks!
Lösung
I think using NSNotification is not the right method for passing data from one VC to another. Like Rog said, use prepareForSegue and use a property on the destination VC:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Always check to make sure you are handling the right Segue
if ([segue.identifier isEqualToString:@"mySegue"]) {
MyOtherViewController *movc = segue.destinationViewController;
movc.myProperty = @"MyValue";
}
}
Andere Tipps
Use prepareForSegue
for going-to a segue, and use the delegate pattern for coming back. Within prepareForSegue
, set the originating viewController as the delegate of the destination viewController. When returning, have the destination viewController call it's delegate (the original viewController) for whatever method you implement.