AppDelegate for UIViewController

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

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

Since AppDelegate does not implement methods such as applicationWillBecomeActive: how can I convert the file to a UIViewController file? What do I need to change in the above code to invoke the controller, not the delegate (my AppDelegate will remain untouched this way).

The code is on github

This is what I did (the code should include the Facebook API). Go to the download tab and download Archive.zip and AppDelegate.zip: https://github.com/Alexmitico45/FacebookRequests/downloads

Basically, the ContactFBSViewController is associated with the view manager in the storyboard.

+4
source share
2 answers

I solved the problem. I had to make the viewcontroller single:

 + (FacebookViewController *) sharedManager; 

.. then use this code to access it:

 FacebookManager *manager = [FacebookManager sharedManager]; 

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

+1
source

If I understand correctly, you want the AppDelegate functionality in the sample to be in your application. But you do not want to replace your AppDelegate application?

The AppDelegate class is actually no different from another class. What makes this fact different is that it implements UIApplicationDelegate and is explicitly mentioned in main.m as the launch class for your application.

If you want to convert this to another class, I would:

  • Rename the class names (.h, .m, interface and implementation), and then put them in your project.
  • In NewName.h, remove the UIApplicationDelegate implementation at the top.
  • In NewName.m you want to look at any of the functions in the UIApplication methods section, since this function is specifically designed to be executed by the AppDelegate class and see how to combine into an existing AppDelegate class. Then remove these UIApplicationDelegate methods from NewName.m, as they can only exist where you implement UIApplicationDelegate.

Since the ViewController exploits the fact that its expected AppDelegate is a long-lived class in the UIApplication hierarchy, you will need to reproduce this behavior. This is probably most easily achieved by creating a new weak property for the NewName link, 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; ... 

Hope this gives you an idea of ​​where to start.

+3
source

Source: https://habr.com/ru/post/1445385/


All Articles