如何使用Interface Builder创建带有UINavigationController的UIViewController? [英] How to create an UIViewController with UINavigationController with Interface Builder?

查看:106
本文介绍了如何使用Interface Builder创建带有UINavigationController的UIViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的iOS应用程序中,我想提供一个设置视图。 presentModalViewController非常有效:

In my iOS application, I want to provide a settings-view. "presentModalViewController" works very well:

ViewSettings *controller = [[ViewSettings alloc] initWithNibName:@"ViewSettings" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
[self.navigationController presentModalViewController:navController animated:YES];      
[controller release];
[navController release];

不幸的是,我必须更改运行代码并创建ViewSettings,包括Interface Builder中的UINavigationController。为什么?长话故事,我将在这个帖子的最后解释...

Unfortunately, I have to change my running code and create the ViewSettings including the UINavigationController in Interface Builder. Why? Long story, I'll explain it in the end of this thread...

我尝试在我的ViewSettings中拖放UINavigationController并创建一个IBOutlet来访问它在我班上我将此控制器提供给presentModalViewController,但应用程序崩溃...

I try to drag and drop an UINavigationController in my ViewSettings and create an IBOutlet to access it in my class. I give this controller to "presentModalViewController" but the application crashed...

我做错了什么?

错误消息: GDB:程序收到信号:SIGABRT。

错误发生在此代码的最后一行:

Error Message: GDB: Program received signal: "SIGABRT".
The error happens in the last line of this code:

ViewSettings *viewSettings = [[ViewSettings alloc] initWithNibName:@"ViewSettings" bundle:nil];
UINavigationController *navController = viewSettings.navigationController;
UINavigationBar *navBar = navController.navigationBar;
OwnNavigationBar *ownNavBar = (OwnNavigationBar *)navBar;
[ownNavBar drawHeaderImage:YES];
[self.navigationController presentModalViewController:navController animated:YES];

详细错误:由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'应用程序已尝试在目标上展示一个零模态视图控制器。'

[/ EDIT]



感谢您的帮助!
是的,navigationController是nil ...
我想我以错误的方式添加UINavigationController ...我把它放在这个窗口中,因为它不可能直接放在我的视图中:


如何正确添加UINavigationController?

[/ EDIT2]

我的UINavigationBar中需要一个背景图片。我的第一次尝试是:

I need an background image in my UINavigationBar. My first try was:

UIImage *image = [UIImage imageNamed: @"header.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.navigationController.navigationBar addSubview:imageView];
[self.navigationController.navigationBar sendSubviewToBack:imageView];

但在某些问题上,UIBarButton的标题不可见!我尝试了很多,例如在每个视图中设置视图的标记和sendSubviewToBack,但没有成功。这是一个非常烦人的错误!

But in some issues, the title oder a UIBarButton is not visible! I tried a lot, e.g. sets the "tag" of the view and sendSubviewToBack in each view, but no success. This is a very annoying bug!

我的第二次尝试是创建一个类别并覆盖drawRect方法:

My second try was to create a category and overwrite the drawRect-method:

@implementation UINavigationBar(MyNavigationBar)
- (void)drawRect:(CGRect)rect {
    UIImage *image = [UIImage imageNamed: @"header.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end

但现在,我的所有UINavigationBars都有背景图像,我无法停用它。问题是,ViewSettings需要背景图像,但后面的推送视图不需要。

But now, all of my UINavigationBars have an background image and I can't deactivate it. The problem is, that "ViewSettings" needs the background image, but the following pushed views do not.

不幸的是,无法在类别中设置属性或者调用[super drawRect:rect]以避免绘制图像。

Unfortunately, it isn't possible to set a property in a category or call [super drawRect:rect] to avoid painting the image.

我的最后一次尝试是写一个自己的UINavigationBar

My last try is to write an own UINavigationBar

@interface OwnNavigationBar : UINavigationBar {
    BOOL _drawHeaderImage;
}

现在我可以控制drawRect方法!!太棒了!

Now I can control the drawRect-method!! GREAT!!

- (void)drawRect:(CGRect)rect {
    if (_drawHeaderImage) {
        UIImage *image = [UIImage imageNamed: @"header.png"];
        [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    }
    else {
        [super drawRect: rect];
    }
}

但我很早就庆祝...... :-(
无法在UINavigationController中设置自己的UINavigationBar !!!
UINavigationController中的navigationBar是一个只读属性!
AAAAHHHHHHHHHH!

But I celebrate to early... :-( It isn't possible to set an own UINavigationBar in the UINavigationController!!! "navigationBar" in UINavigationController is a read-only property! AAAAHHHHHHHHHH!

我有最后一次机会:在Interface Builder中,可以为UINavigationController提供一个自己的UINavigationBar !!
是的!我知道它!!: - )

I have one last chance: in Interface Builder it is possible to give an UINavigationController an own UINavigationBar!! YES! I GOT IT!! :-)

我在我的MainWindow_iPhone.xib中进行了配置,效果很好!
现在,我必须为我的ViewSettings实现这个,因为这个(模态)视图需要一个新的UINavigationController。

I configured it in my MainWindow_iPhone.xib and it works great! Now, I have to implement this for my ViewSettings, because this (modal) view needs a new UINavigationController.

PS:也许,有人可以将此线程发送给Apple,这一切都是非常烦人的情况和错误: - (

推荐答案

当你初始化时从一个笔尖查看控制器,它实际上并没有立即加载笔尖,所以 viewSettings.navigationController 仍然是零。第一次引用视图属性,将加载笔尖。

When you init a view controller from a nib, it does not actually load the nib right away, so viewSettings.navigationController is still nil. The first time you reference the view property, the nib will be loaded.

更新

您可以自己加载笔尖并抓住导航控制器,如下所示:

You could load the nib yourself and grab the navigation controller, like so:

UINavigationController *navController = [[[NSBundle mainBundle] loadNibNamed:@"ViewSettings" owner:self options:nil] objectAtIndex:1];

I在索引1处使用了对象,因为在你的截图中有一个tableview,我认为它将是索引0。

I used object at index 1 because in your screenshot there's a tableview which I think will be index 0.

这篇关于如何使用Interface Builder创建带有UINavigationController的UIViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆