Reputation: 267
I have a UIButton
in a View Controller that is NOT the root view controller in my app. I cannot drag and create an outlet or an action for it in my header file. I can only do it from the root view controller.
Is there a simple reason for this?
Upvotes: 2
Views: 2953
Reputation: 53830
When using the storyboard, XCode only creates the .h and .m file for the first view controller (named ViewController.h and ViewController.m).
For each additional view controller that you add via Interface Builder, you should manually add additional custom class files in order to customize the view controller.
Once you add a View Controller to your story board via Interface Builder, follow these instructions:
UITableViewController
for a Table View Controller).You should then be able to CTRL-click drag any UIButtons
or other UI elements onto the new .h or .m file and implement any custom behavior for the view there.
If you don't create files to implement your custom classes, you are stuck with the default UI classes like UIViewController
, which only have their default behaviors unless you extend them with custom classes.
You generally want one .m and .h file per view controller.
Upvotes: 5
Reputation: 122381
Ensure the NIB's File's Owner is set to the right class. Select File's Owner from Interface Builder and ensure the correct class is set in the Identity Inspector tab:
Upvotes: 0
Reputation: 534885
Yes, there is a simple reason for it, though it's hard to say in your case exactly what that simple reason is, since you don't provide quite enough information. Basically, it all comes down to how view controllers work and how they are loaded / instantiated from a storyboard. The short version:
You can make an outlet / action only between objects within the same "scene".
An outlet / action is complicated: you can only create one if there is a corresponding IBOutlet / IBAction in code. Xcode can help create that as part of the outlet / action creation process, but this merely masks the fact that there are two things, the IBOutlet / IBAction in the code, and the outlet/action in the nib/storyboard.
You might be helped further by reading my book's chapter on how nibs really work:
http://www.apeth.com/iOSBook/ch07.html#_nib_loading_and_file_8217_s_owner
This applies equally to storyboards, which effectively are just a way of combining multiple nibs into a single file. Storyboards add some complications, though, since there is another kind of thing you can drag between objects, namely a relationship or segue (and it may be that you are accidentally creating one of those):
http://www.apeth.com/iOSBook/ch19.html#_storyboards
Upvotes: 0
Reputation: 89509
You can connect the UIButton to an outlet that is defined as part of the view controller.
If you also make that outlet a property, then you can expose and make that button available to other view controllers to set/get/query/etc. This latter thing will only work if the button-hosting view controller exists in memory (which it won't, at least initially, assuming the view controller the button is visible in doesn't appear at the same time the first controller is visible).
Upvotes: 0
Reputation: 884
Are you trying to drag into the header file for that viewController? I'm guessing you're using a storyboards not xibs? I think we need some more details. A screenshot maybe?
Sounds like your assistant is on the wrong view. You can change this in the top left drop down of the assistant view to be the matching header file for that view controller. But like I said we need more details.
Upvotes: 0