it possible to Pass Data with popViewControllerAnimated?

前端 未结 5 1360
清歌不尽
清歌不尽 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:47

    The best way to do it is by using a delegate.

    //SecVCDelegate.h

    #import 
    
    @protocol SecVSDelegate 
    
    @optional
    - (void)secVCDidDismisWithData:(NSObject*)data;
    @end
    

    //SecVC.h

    #import 
    #import "SecVSDelegate.h"
    
    @interface SecVC : UIViewController
    
    /** Returns the delegate */
    @property (nonatomic, assign)   id delegate;
    
    @end
    

    //SecVC.M

    ...
    
    - (void) dealloc
    {
    ...
    delegate = nil
    ...
    }
    

    When ever you popViewControllerAnimated, right after it (or before it) you do this

    if(_delegate && [_delegate respondsToSelector:@selector(secVCDidDismisWithData:)])
    {
    [_delegate secVCDidDismisWithData:myDataObject];
    }
    

    And in the MainVC you must be certain that you implement the delegate function //MainVC.m

    - (void)secVCDidDismisWithData
    {
    //do whatever you want with the data
    }
    

    To avoid any warnings you must tell that the MainVC class implements the delegate like this:

    //MainVC.h

    #import "SecVCDelegate.h"
    ...
    @interface MainVC : UIViewController 
    ...
    secVCInstance.delegate = self;
    [self.navigationController pushViewController:secVCInstance]; 
    ...
    

提交回复
热议问题