Alessandro
Alessandro

Reputation: 4110

AppDelegate to UIViewController

I have a sample code with a header and an implementation file and the two appDelegate files. I would like to add what the project does to my app. The first two files are a ViewController file, so I just need to drag it in, but the other two are AppDelegate, and I obviously can't have two app delegates. But in the case of this sample app the app delegate is used as a proper viewcontroller, because in the .m file of the UIViewController file, there is this code:

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

Since the AppDelegate doesn't implement methods such as applicationWillBecomeActive:, how can I transform the file into a UIViewController file? What do I need to change in the code above to call the controller, instead of the Delegate (my AppDelegate will so remain untouched).

The code is on GitHub

This is what I have done (the code needs the Facebook API to be included). Go on the download tab and download Archive.zip and AppDelegate.zip: https://github.com/Alexmitico45/FacebookRequests/downloads

Basically the controller ContactFBSViewController is linked the a viewcontroller in the storyboard.

Upvotes: 4

Views: 1003

Answers (2)

Alessandro
Alessandro

Reputation: 4110

I solved the problem. What I had to do is make the viewcontroller a singleton:

+ (FacebookViewController *) sharedManager;

..then use this code to access it:

FacebookManager *manager = [FacebookManager sharedManager];

And after days of trial and error it finally works!!

Upvotes: 1

KevinH
KevinH

Reputation: 2054

If I understand correctly, you want the functionality of the AppDelegate in the sample to be in your app. But you don't want to replace your app's existing AppDelegate?

An AppDelegate class isn't really any different from another class. What differentiates it is the fact that it implements UIApplicationDelegate, and is explicitly referred to in main.m, as the startup class for your app.

If you want to transform that into just another class, I would:

  1. Rename the class (.h, .m, interface and implementation names), then put it in your project.
  2. In NewName.h, remove the implementation of UIApplicationDelegate at the top.
  3. In NewName.m, you want to look at any of the functionality in the UIApplication methods section, as that's functionality specifically allocated to be performed by an AppDelegate class, and see if or how that needs to be merged into your existing AppDelegate class. Then remove those UIApplicationDelegate methods from NewName.m, as they can only exist there when you implement UIApplicationDelegate.

Since ViewController takes advantage of the fact that its expected AppDelegate is a long-lived class in the UIApplication hierarchy, you'll need to replicate that behavior. This is probably most easily achieved by creating a new weak property to reference NewName, in ViewController.h:

ViewController.h

@class NewName;
@property (nonatomic, weak) NewName *newNameDelegate;

ViewController.m

#import "NewName.h"

...

@implementation ViewController
@synthesize newNameDelegate;

...

- (IBAction)sendRequestButtonAction:(id)sender {
    if (FBSession.activeSession.isOpen) {
        [newNameDelegate sendRequest];
    }
}

NewName.m

...

self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.viewController.newNameDelegate = self;

...

Hopefully this gives you an idea of where to start.

Upvotes: 3

Related Questions