views:

69

answers:

2

Hello, I'm working on my first iOS app. I haven't had a problem using interface builder in the standard way.

I want to use interface builder to configure and instantiate a button, but not add it to a view (because I want to add the button to a navigation bar, which is created in a different nib).

I tried to simply add the button (a segmented control) to the "document" in interface builder, add an outlet, and add it to the self.navigationItem in viewDidAppear:, but the outlet variable is null in viewDidAppear:.

I can create the button entirely programmatically, but I'd rather not. So, my questions are:

1) Can I configure and instantiate UI objects in interface builder, connect them to an outlet, and display them later programmatically? If so, what's the best way to do this?

2) Can I add my button to the navigation controller created in the other nib directly in interface builder?

Thanks! -Jeff

A: 

Create the button. Look in the inspector in Interface Builder. There's a checkbox labelled "hidden." Check it. Create an outlet for your button.

UIButton *myButton;

Later, you can call

[myButton setHidden:NO];
Paul Schreiber
I'm not sure I like this solution, because I feel bad about adding the button to an arbitrary view, hiding it, removing it from that view before displaying it, and then adding it to the correct view (the navigation controller) at runtime.I think I figured it out...
Jeff
A: 

I think I figured it out.

Create a new nib whose owner is a UIViewController. Create and configure the UISegmentedControl. Set the view for the UIViewController to the UISegmentedControl.

Programatically:

UIViewController* buttonController = [[UIViewController alloc] initWithNibName:@"AddRemoveCustomButton" bundle: nil];
UIView* segmentedControl = [buttonController view];
UIBarButtonItem* bbi =  [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
self.navigationItem.rightBarButtonItem = bbi;
[bbi release];
[segmentedControl release];
[buttonController release];

But, is there another way to reference the UINavigationController object, even if it was created in another nib? If so, I could do everything in IB, and avoid a "manual" solution.

Thanks! -Jeff

Jeff