tags:

views:

63

answers:

1

Is it a good practice to use one main AppDelegate.h to handle all the ibaction stuff?

is it even possible? if so who does one do this? my IB only lets me link to the associated .m file

firstView.xib only respond to ibaction in firstView.m

I want a button on firstView.xib to respond to ibaction in AppDelegate.m

any thoughts?

A: 

No, it's not good practice. But if you really want to...

  • If you aren't worried about having the same instance of appDelegate holding the IBActions, you can just drag a generic object from the library into your firstView.xib, then change the class to appDelegate. That will allow you to link actions.

  • You can hook up the actions programmatically: create IBOutlets from the view controller, and in the viewDidLoad method of the view controller, get the [[UIApplication sharedApplication] delegate] and attach it's IBAction methods to the actions of the IBOutlets for the buttons

To access the app delegate from elsewhere in your code, do the following:

#import "AppDelegate.h"

AppDelegate * appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
Sam Dufel
If it is a bad practice then I will not do it.
Vilasack
So I should put all my ibaction stuff in the .m file of the view?
Vilasack
Yeah, that's what the view controller is for. If you need access to data in the app delegate, you can get a reference to it from anywhere in your code.
Sam Dufel
OK. Thanks for the infor. Now if I want to create a universal app; and have some shared functionality, should i create a class commonFeatures.m that can be instantiated from both the iphone and ipad controllers? I've seen some of the youtube tutorials and they do a lot of copy / paste of block of code. I was wondering if this shared code should have been in a common library.
Vilasack
Can you provide the sample code snip for accessing the appdelegate from let say the myviewcontroller.m
Vilasack
I just edited my answer to put the sample code in it ^^ And yes, it's definitely the way to go to put your common code in its own class and import it into both the ipad and iphone view controllers.
Sam Dufel