it possible to Pass Data with popViewControllerAnimated?

前端 未结 5 1359
清歌不尽
清歌不尽 2020-12-06 02:29

I came across an interesting problem, i have main ViewController let\'s call him MainVC with navigationController and i am doing performSegueWithIdentifier from him to Mine

5条回答
  •  心在旅途
    2020-12-06 02:58

    While I agree that the best option is to use Delegate, but still if any one is looking for something different, he can use NSNotificationCenter as an alternative.

    In viewDidLoad of MainVC:

    - (void)viewDidLoad
    {
    
        [super viewDidLoad];
    
        [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(recvData:)
                                                 name:@"SecVCPopped"
                                               object:nil];
    }
    

    And add method recvData in MainVC.m

    - (void) recvData:(NSNotification *) notification
    {
    
        NSDictionary* userInfo = notification.userInfo;
        int messageTotal = [[userInfo objectForKey:@"total"] intValue];
        NSLog (@"Successfully received data from notification! %i", messageTotal);
    }
    

    Now in SecVC, before popping, add this line

    NSMutableDictionary* userInfo = [NSMutableDictionary dictionary];
    [userInfo setObject:[NSNumber numberWithInt:messageTotal] forKey:@"total"];
    
    NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
    [nc postNotificationName:@"SecVCPopped" object:self userInfo:userInfo];
    

提交回复
热议问题