views:

82

answers:

1

I'm having this problem because I originially made everything in the main NIB, but then after reading learned that it is better to use subviews.

I've got my IBActions in the AppDelegate, and I've successfully connected and loaded my subviews. However, when I try to connect my buttons in the subviews to the IBActions in the AppDelegate, the IBActions appear under the "First Responder". They seem to connect fine, but when running the application they do not trigger the IBActions (I've confirmed this with some NSLogs, it's not an error in the code within the IBActions). What am I doing wrong?

Thanks!

+1  A: 

The AppDelegate should only be used for very specific items such as implementing the UIApplicationDelegate protocol (i.e. methods like applicationDidFinishLaunching) or in some cases storing global variables.

You should keep IBActions and other outlets in their respective view controller files (i.e. if you created MyViewController.h and MyViewController.m which are linked with MyViewController.xib where you have some buttons, images, etc.). They can then be hooked up via dragging the inspector control you want (i.e. TouchUpInside) to the File's Owner.

Something you should read to better understand view controllers: http://developer.apple.com/iphone/library/featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html

Typically it is best to create a unique view controller for each view you will present to the user. For instance, if I had a main screen and then an "about" or a settings screen, I would make each of those their own view controller. It helps organize things better than using one view with a whole bunch of subviews that you hide/show and will also improve loading times and general performance.

Update for your 2nd question in the comments about accessing the app delegate:

First, you need to import the .h file (i.e. #import "AppDelegate.h") for the app delegate into whichever view controller .m file you wanna use to access whatever variables, arrays, etc you have stored in the app delegate files. Make sure you synthesize whichever objects you create in the app delegate's .h file in the app delegate's .m file so the getters and setters are created (so you can access them).

Then in the view controller .m file, in whichever method you are using:

-(void)someMethod { 
 // here we just create a shortcut to the app delegate called "theAppDelegate"
 YourAppDelegateFileNameHere *theAppDelegate = (YourAppDelegateFileNameHere *)[[UIApplication sharedApplication] delegate];

 // now you can use the dot notation if you wanna access a variable
 int SomeNewInteger = theAppDelegate.someIntegerYouHaveStored;

 // or some array you have stored
 return [theAppDelegate.someArrayYouCreated count]; 
 }

Hope that helps!

iWasRobbed
Thanks! What about accessing objects from the AppDelegate in the view controller? I've declared the AppDelegate like so://InterfaceAppDelegate *appDelegate;@property (nonatomic, retain) AppDelegate *appDelegate;//Implementation@synthesize appDelegate;appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];yet when I try to do something, I always get "accessing unknown getter method". Sorry stackOverflow won't let me format my comment nicely.
Wind Up Toy
Please see my updated response for how to call it correctly. It's a little bit different with the app delegate since you don't need to set up properties or synthesize it like how you were doing (which is why you are getting the unknown getter method error)
iWasRobbed
P.S. If the answer is useful to you, please get in the habit of upvoting whichever answers are most useful. It's sort of like giving people a pat on the back for their help around here :)
iWasRobbed
ok i figured out that i needed to use the arrow syntax (i was trying to do [theAppDelegate.networkClient sendMessage:@"whatever"]; when in fact i needed [theAppDelegate->networkClient sendMessage:@"whatever"]; ). However now I am getting no errors when compiling but the application just crashes...i wasn't able to upvote until now. required so much reputation or something like that. :)thanks for your help.
Wind Up Toy
You should probably start a new question since this is getting a bit off topic of your original question. In your new question, could you explain more what `networkClient` is? Is it just a method that you are trying to call and pass a variable to or are you trying to access an instance variable? Also, what error message is showing up in the Console (under the Run menu in XCode) after it crashes.
iWasRobbed