iOS - Calling App Delegate method from ViewController

前端 未结 13 955
鱼传尺愫
鱼传尺愫 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:15

    Update for Swift 3.0 and higher

    //
    // Step 1:- Create a method in AppDelegate.swift
    //
    func someMethodInAppDelegate() {
    
        print("someMethodInAppDelegate called")
    }
    

    calling above method from your controller by followings

    //
    // Step 2:- Getting a reference to the AppDelegate & calling the require method...
    //
    if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
    
        appDelegate.someMethodInAppDelegate()
    }
    

    Output:

提交回复
热议问题