Calling method in current view controller from App Delegate in iOS

后端 未结 4 2140
耶瑟儿~
耶瑟儿~ 2020-12-01 01:23

I have two view controllers (BuildingsViewController and RoomsViewController) that both use a function within the App Delegate called upload. The upload function basically d

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-01 02:15

    Step 1:

    In your App Delegate .h file you need to declare a protocol like so:

    @protocol AppConnectionDelegate 
    @required
       -(void)connectionFinished:(NSObject*)outObject;
    @end
    

    In the same file, add an ivar like so:

    id *delegate;
    

    Declare the ivar as a property:

    @property (nonatomic, assign) id delegate;
    

    In the App Delegate .m file, synthesize the ivar:

    @synthesize delegate;
    

    In the App Delegate .m file, on connectionDidFinishLoading do:

    if([self.delegate respondsToSelector:@selector(connectionFinished:)])
    {
       [self.delegate connectionFinished:objectYouWantToSend];
    }
    

    In your viewcontroller's .h file, implement the AppConnectionDelegate by importing a reference to the app delegate file:

    #import "AppDelegate_iPhone.h" //if using iPhone
    #import "AppDelegate_iPad.h" //if using iPad
    

    In the same file, at the end of the first line of the interface declaration do:

    @interface AppDelegate_iPhone : AppDelegate_Shared 
    

    Declare ivars accordingly:

    AppDelegate_iPhone *appDelegate; //if using iPhone
    AppDelegate_iPad *appDelegate; // if using iPad
    

    In your viewcontroller's .m file in the viewDidLoad(), get a reference to your app delegate using:

    If iPhone;

    appDelegate = (AppDelegate_iPhone*)[[UIApplication sharedApplication] delegate];
    

    If iPad:

    appDelegate = (AppDelegate_iPad*)[[UIApplication sharedApplication] delegate];
    

    Then set the viewcontroller to be the delegate in viewDidLoad() by doing:

    appDelegate.delegate = self;
    

    Now you need to simply implement the connectionFinished method in the .m file:

    - (void)connectionFinished:(NSObject*)incomingObject
    {
    //Do whatever you want here when the connection is finished. IncomingObject is the object that the app delegate sent.
    }
    

    Now whenever your app delegate's connectionDidFinishLoading is called, the view controller will be notified.

    [It's a best practice to set appDelegate.delegate = nil if you're done using the connectionFinished callback]

    This is tried and tested. If you have questions, leave a comment......

    --EDIT--
    This is a robust alternative to NSNotification. I use both depending on the requirements. The process I use to decide between using NSNotification or a delegate callback using a protocol is simply:

    For notifications:
    One sender, multiple listeners.
    No reference possible between sender and listener.
    Complex/multiple objects need not be sent

    For delegate callbacks using protocols:
    One sender, limited (usually 1) listeners.
    A reference between sender and listener is possible.
    Complex/multiple objects are to be sent (for example, response objects that need to be sent)

    I know sending objects is possible through notifications but I prefer protocols for that.
    --EDIT--

提交回复
热议问题