iOS - Calling App Delegate method from ViewController

IosObjective CUiviewcontrollerAppdelegate

Ios Problem Overview


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.

I know it could be done relatively easily in IB but that isn't an option.

An example of what I want to do would be if you had two view controllers one with a splash screen of house. The other view controller had a walk through of the house on it that you could go through all the rooms in a set order. The splash screen would have buttons for each room that would allow you to jump to any point on the walk through.

Ios Solutions


Solution 1 - Ios

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];

Solution 2 - Ios

And if anyone is wondering how to do this in swift:

if let myDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
   myDelegate.someMethod()
}

Solution 3 - Ios

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...

Solution 4 - Ios

This is how I do it.

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

Dont forget to import.

#import "AppDelegate.h"

Solution 5 - Ios

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.

Solution 6 - Ios

NSObject <UIApplicationDelegate> * universalAppDelegate = 
    ( NSObject <UIApplicationDelegate> * ) [ [ UIApplication sharedApplication ] delegate ];

It avoid having to include your AppDelegate.h everywhere. It's a simple cast that goes a long way, allowing to develop independent Controller and reuse them elsewhere without to worry about class name and so on...

Enjoy

Solution 7 - Ios

A lot of good answers are already added. Though I want to add something which suits me most of the time.

#define kAppDelegate ((YourAppDelegate *)[[UIApplication sharedApplication] delegate]);

and thats it. Use it throughout the application just like a constant.

e.g.

[kAppDelegate methodName];

Don't forget to import yourAppDelegate.h in corresponding .pch or macros file.

Solution 8 - Ios

If someone need the same in Xamarin (Xamarin.ios / Monotouch), this worked for me:

var myDelegate = UIApplication.SharedApplication.Delegate as AppDelegate;

(Require using UIKit;)

Solution 9 - Ios

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:

enter image description here

Solution 10 - Ios

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

extDelegate = WKExtension.sharedExtension().delegate as WKExtensionDelegate?

Solution 11 - Ios

You can add #define uAppDelegate (AppDelegate *)[[UIApplication sharedApplication] delegate] in your project's Prefix.pch file and then call any method of your AppDelegate in any UIViewController with the below code.

[uAppDelegate showLoginView];

Solution 12 - Ios

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

Solution 13 - Ios

Even if technically feasible, is NOT a good approach. When You say: "The splash screen would have buttons for each room that would allow you to jump to any point on the walk through." So you want to pass through appdelegate to call these controllers via tohc events on buttons?

This approach does not follow Apple guidelines and has a lot of drawbacks.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMytheralView Question on Stackoverflow
Solution 1 - IosCristian RaduView Answer on Stackoverflow
Solution 2 - IoscanhazbitsView Answer on Stackoverflow
Solution 3 - IossradforthView Answer on Stackoverflow
Solution 4 - Iosmikemike396View Answer on Stackoverflow
Solution 5 - IosTunvir Rahman TusherView Answer on Stackoverflow
Solution 6 - Iosbruno.yvan.morelView Answer on Stackoverflow
Solution 7 - IosZaEeM ZaFaRView Answer on Stackoverflow
Solution 8 - IosDaniele D.View Answer on Stackoverflow
Solution 9 - IosiAjView Answer on Stackoverflow
Solution 10 - IosMarco MiltenburgView Answer on Stackoverflow
Solution 11 - IosAdaView Answer on Stackoverflow
Solution 12 - IosBreno Medeiros de OliveiraView Answer on Stackoverflow
Solution 13 - IosingcontiView Answer on Stackoverflow