开发者

using interface builder to configure and instantiate UI objects (but not display them in view)

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 inst开发者_运维百科antiate 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


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];


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜