【发布时间】:2011-05-25 17:20:55
【问题描述】:
我是 iPhone 开发的新手。 我的应用程序基于 UInavigationBar 。 我想在我的一个 xib 中添加一个导航栏项目,但在 .xib 中我只是模拟导航栏,所以我不能拖放该项目。谢谢你
【问题讨论】:
标签: iphone objective-c
我是 iPhone 开发的新手。 我的应用程序基于 UInavigationBar 。 我想在我的一个 xib 中添加一个导航栏项目,但在 .xib 中我只是模拟导航栏,所以我不能拖放该项目。谢谢你
【问题讨论】:
标签: iphone objective-c
您需要以编程方式添加导航栏按钮。你看,你的 xib 有一个显示在 UINavigationController 的内容视图中的视图。它是您的应用程序可以访问并控制导航栏项目的 UINavigationBar。正如您所指出的,您的 xib 只有一个导航栏的占位符,这对您来说确实很方便,因此您的视图在布局时可以正确调整大小。
在 xib 的 UIViewController 中,您可以使用类似代码的代码添加适合视图的导航栏项目
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle:@"View" style:UIBarButtonItemStylePlain
target:self
action:@selector(viewControls:)] autorelease];
这有意义吗?
【讨论】:
target 和action 分别是点击按钮时要调用的对象和对象的方法。
为了能够将项目添加到 UINavigationBar,您需要先将 UINavigationBar 添加到您的视图,然后再向其中添加项目。
您不能在模拟导航栏上拖放项目。如果您通过其他方式或通过代码添加导航栏,则模拟导航栏可确保您正确估计可用的视图大小。
【讨论】:
您应该将 UINavigationController 用于基于导航的层次结构。这将处理如何使导航按您希望的方式工作的许多较低的细节。我还建议以编程方式进行设置。以下是您将如何做到的。
// Initial setup of navigation
YourViewController *yvc = [[YourViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:yvc];
[[self view] addSubview:[nav view]];
然后当你想去一个新的视图控制器(你通常看到的动画滑动)时,你这样做
// From inside 'YourViewController',
// this is normally when the user touches a table view cell
NewViewController *nvc = [[NewViewController alloc] init];
[self.navigationController pushViewController:nvc];
如果您想更改标题或按钮,请执行此操作
// This is normally in viewDidLoad or something similar
[self.navigationItem setTitle:@"Hello World!"];
[self.navigationItem.rightBarButtonItem:/* A UIBarButtonItem */];
【讨论】: