iOS - Calling App Delegate method from ViewController

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

    Sounds like you just need a UINavigationController setup?

    You can get the AppDelegate anywhere in the program via

    YourAppDelegateName* blah = (YourAppDelegateName*)[[UIApplication sharedApplication]delegate];
    

    In your app delegate you should have your navigation controller setup, either via IB or in code.

    In code, assuming you've created your 'House overview' viewcontroller already it would be something like this in your AppDelegate didFinishLaunchingWithOptions...

    self.m_window = [[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds] autorelease];
    self.m_navigationController = [[[UINavigationController alloc]initWithRootViewController:homeViewController]autorelease];
    [m_window addSubview:self.m_navigationController.view];
    

    After this you just need a viewcontroller per 'room' and invoke the following when a button click event is picked up...

    YourAppDelegateName* blah = (YourAppDelegateName*)[[UIApplication sharedApplication]delegate];
    [blah.m_navigationController pushViewController:newRoomViewController animated:YES];
    

    I've not tested the above code so forgive any syntax errors but hope the pseudo code is of help...

    0 讨论(0)
  • 2020-11-28 01:09

    In 2020, iOS13, xCode 11.3. What is working for Objective-C is:

    #import "AppDelegate.h"

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

    It's working for me, i hope the same to you! :D

    0 讨论(0)
  • 2020-11-28 01:11

    And in case you need access to your WatchKit extension delegate from a view controller on watchOS:

    extDelegate = WKExtension.sharedExtension().delegate as WKExtensionDelegate?
    
    0 讨论(0)
  • 2020-11-28 01:12

    This is how I do it.

    [[[UIApplication sharedApplication] delegate] performSelector:@selector(nameofMethod)];
    

    Dont forget to import.

    #import "AppDelegate.h"
    
    0 讨论(0)
  • 2020-11-28 01:15

    You can access the delegate like this:

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

    Replace MainClass with the name of your application class.

    Then, provided you have a property for the other view controller, you can call something like:

    [appDelegate.viewController someMethod];
    
    0 讨论(0)
  • 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:

    0 讨论(0)
提交回复
热议问题