使用navigationItem时,首先要确保UINavigationController视图已经加载到ViewController中。其代码为:
[[UINavigationController alloc] initWithRootViewController:viewController];
1、添加按钮
UIBarButtonItem *loginBut =[[UIBarButtonItem alloc] initWithTitle:@"Login" style:UIBarButtonItemStylePlain target:self action:@selector(login)];
self.navigationItem.rightBarButtonItem = loginBut;
[loginBut release];
UIBarButtonItem *createBut =[[UIBarButtonItem alloc] initWithTitle:@"NEW" style:UIBarButtonItemStylePlain target:self action:@selector(creatUser)];
self.navigationItem.leftBarButtonItem = createBut;
[createBut release];
2、添加segmentedControl
UISegmentedControl *m_segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:@"up.png"],
[UIImage imageNamed:@"down.png"],
nil]];
[m_segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
m_segmentedControl.frame = CGRectMake(0, 0, 90, 30);
m_segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
m_segmentedControl.momentary = YES;
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:m_segmentedControl];
[m_segmentedControl release];
self.navigationItem.rightBarButtonItem = segmentBarItem;
[segmentBarItem release];
3、设置标题
self.navigationItem.title = @"详细内容";
4、设置返回按钮
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"主菜单" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];