如何使用 Interface Builder 使用 UINavigationController 创建 UIViewController?
在我的 iOS 应用程序中,我想提供一个设置视图。 “presentModalViewController”工作得很好:
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。为什么?长话短说,我将在本线程的末尾解释它......
我尝试在我的 ViewSettings 中拖放 UINavigationController 并创建一个 IBOutlet 以在我的类中访问它。我将此控制器提供给“presentModalViewController”,但应用程序崩溃了......
我做错了什么?
[编辑]
错误消息:GDB:程序收到信号:“SIGABRT”。
错误发生在这段代码的最后一行:
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”而终止应用程序,原因:“应用程序试图在目标上呈现一个零模态视图控制器。”
[/编辑]
[编辑2]
感谢您的帮助! 是的,navigationController 为零... 我认为我以错误的方式添加了 UINavigationController...我把它放在这个窗口中,因为不可能将它直接放在我的视图中:
如何正确添加 UINavigationController?
[/EDIT2]
PS:为什么我必须使用IB? (你可以跳过这个...)
我的 UINavigationBar 中需要一个背景图像。我的第一次尝试是:
UIImage *image = [UIImage imageNamed: @"header.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.navigationController.navigationBar addSubview:imageView];
[self.navigationController.navigationBar sendSubviewToBack:imageView];
但在某些问题中,UIBarButton 的标题不可见!我尝试了很多,例如在每个视图中设置视图的“标签”和sendSubviewToBack,但没有成功。这是一个非常烦人的错误!
我的第二次尝试是创建一个类别并覆盖 drawRect 方法:
@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
但是现在,我所有的 UINavigationBar 都有背景图像,我无法停用它。问题是,“ViewSettings”需要背景图像,但以下推送的视图不需要。
不幸的是,无法在类别中设置属性或调用 [super drawRect:rect] 来避免绘制图像。
我最后的尝试是编写一个自己的 UINavigationBar
@interface OwnNavigationBar : UINavigationBar {
BOOL _drawHeaderImage;
}
现在我可以控制 drawRect 方法了!伟大的!!
- (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”是只读属性! 啊啊啊啊啊啊!
我还有最后一次机会:在 Interface Builder 中,可以为 UINavigationController 提供一个自己的 UINavigationBar! 是的!我得到了它!! :-)
我在 MainWindow_iPhone.xib 中配置了它,效果很好! 现在,我必须为我的 ViewSettings 实现此功能,因为此(模式)视图需要一个新的 UINavigationController。
PS:也许,有人可以将此线程发送给Apple,这都是非常烦人的情况和错误:-(
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];
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...
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...
What I'm doing wrong?
[EDIT]
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];
Detailed Error: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target .'
[/EDIT]
[EDIT2]
Thanks for your help!
Yes, navigationController is nil...
I think I add the UINavigationController in a wrong way... I put it in this window, because it was not possible to put it directly in my view:
How do I add the UINavigationController correct?
[/EDIT2]
PS: Why do I have to use IB? (you can skip this...)
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];
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!
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
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.
Unfortunately, it isn't possible to set a property in a category or call [super drawRect:rect] to avoid painting the image.
My last try is to write an own UINavigationBar
@interface OwnNavigationBar : UINavigationBar {
BOOL _drawHeaderImage;
}
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];
}
}
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!
I have one last chance: in Interface Builder it is possible to give an UINavigationController an own UINavigationBar!!
YES! I GOT IT!! :-)
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: Maybe, someone can send this thread to Apple, this all are very annoying circumstances and bugs :-(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您从笔尖初始化视图控制器时,它实际上并没有立即加载笔尖,因此 viewSettings.navigationController 仍然为零。第一次引用
view
属性时,将加载 nib。更新
您可以自己加载笔尖并抓取导航控制器,如下所示:
我在索引 1 处使用了对象,因为在您的屏幕截图中有一个表格视图,我认为该视图的索引为 0。
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 theview
property, the nib will be loaded.Update
You could load the nib yourself and grab the navigation controller, like so:
I used object at index 1 because in your screenshot there's a tableview which I think will be index 0.
如果您要显示 nib 文件的内容,那将会很有帮助,因为我怀疑 viewSettings.navigationController 为零。
navigationController
是所有UIViewControllers
的一个属性,它为您提供它们的父导航视图控制器(如果存在)。听起来您从未将viewSettings
视图控制器添加到导航控制器的视图控制器堆栈中。It would be helpful if you were to show the contents of your nib file because I suspect
viewSettings.navigationController
is nil.navigationController
is a property of allUIViewControllers
which gives you their parent navigation view controller, if one exists. It sounds like you never added yourviewSettings
view controller to a navigation controller's stack of view controllers.