在AppDelegate.m文件中添加,如下代码,更改根视图控制器。
//创建导航栏控制器的对象,初始化根视图
UINavigationController * pNavigation = [[UINavigationController alloc]initWithRootViewController:self.viewController];
//把Navigation作为根视图控制器
self.window.rootViewController = pNavigation;
//释放创建的对象
[pNavigation release];
在子视图.m文件中的-(void)viewDidLoad方法中添加代码,为Navigation设置相关的属性。
- (void)viewDidLoad
{
[super viewDidLoad];
//为根视图命名
self.navigationItem.title = @"根视图";
//设置根视图上的左右按钮,关联的方法为空,可以编写实现
UIBarButtonItem *pLeftBar = [[UIBarButtonItem alloc]initWithTitle:@"左按钮" style:UIBarButtonItemStyleBordered target:self action:nil];
UIBarButtonItem *pRightBar = [[UIBarButtonItem alloc]initWithTitle:@"右按钮" style:UIBarButtonItemStylePlain target:self action:nil];
//把左右按钮添加到导航栏上
self.navigationItem.rightBarButtonItem = pRightBar;
self.navigationItem.leftBarButtonItem = pLeftBar;
//释放创建的按钮对象
[pRightBar release];
[pLeftBar release];
}
//Custom view to use in lieu of a title. May be sizedhorizontally. Only used when item is topmost on the stack.
自定义视图以代替标题使用。可以水平尺寸。只有当产品最顶层的栈上使用。
导航栏的一个重要属性,把它作为视图,可以进行视图的相关操作,比如:添加背景图片、设置背景颜色、设置选择控制控件等。