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.
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...
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
And in case you need access to your WatchKit extension delegate from a view controller on watchOS:
extDelegate = WKExtension.sharedExtension().delegate as WKExtensionDelegate?
This is how I do it.
[[[UIApplication sharedApplication] delegate] performSelector:@selector(nameofMethod)];
Dont forget to import.
#import "AppDelegate.h"
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];
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: