iOS - Calling App Delegate method from ViewController

前端 未结 13 944
鱼传尺愫
鱼传尺愫 2020-11-28 00:29

What I am trying to do is click a button (that was created in code) and have it call up a different view controller then have it run a function in the new view controller.

13条回答
  •  鱼传尺愫
    2020-11-28 01:20

    Just Follow these steps

    1.import your app delegate in your class where you want app delegate object.

    #import "YourAppDelegate.h"
    

    2.inside your class create an instance of your app delegate object(Its basically a singleton).

    YourAppDelegate *appDelegate=( YourAppDelegate* )[UIApplication sharedApplication].delegate;
    

    3.Now invoke method using selector

    if([appDelegate respondsToSelector:@selector(yourMethod)]){
    
            [appDelegate yourMethod];
        }
    

    or directly by

    [appDelegate yourMethod];
    

    for swift

    let appdel : AppDelegate = UIApplication.shared.delegate as! AppDelegate
    

    i will recommend the first one. Run and Go.

提交回复
热议问题