navigationController问题

时间:2011-10-02 10:08:17

标签: iphone uinavigationcontroller

我有以下内容:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath(NSIndexPath*)indexPath
{

     audiChassisInputViewController = [[myAudiChassisInputViewController alloc] init];    

    [self.navigationController pushViewController:audiChassisInputViewController animated:YES];

    self.navigationController.navigationBarHidden = NO;

    UIBarButtonItem *retourButton = [[UIBarButtonItem alloc] initWithTitle:@"Retour" style:UIBarButtonItemStyleBordered target:self.navigationController action:@selector(popViewControllerAnimated:)]; 
    [self.navigationController.navigationBar.topItem setLeftBarButtonItem:retourButton];
    [self.navigationController.navigationBar.topItem setTitle:@"Chassis Input"];
    [retourButton release];

    [audiChassisInputViewController release];

}

并且这项工作...显示了新视图。

在新视图中:

myAudiChassisInputViewController.h

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    chassisInputTextView.layer.cornerRadius = 15;
    chassisInputTextView.clipsToBounds = YES;
    [chassisInputTextView becomeFirstResponder];

    UIBarButtonItem *okButton = [[UIBarButtonItem alloc] initWithTitle:@"OK" style:UIBarButtonItemStyleBordered target:self action:@selector(chassisOkPressed)];  
    [self.navigationController.navigationBar.topItem setRightBarButtonItem:okButton];
    [okButton release];

}

我没有错误,但没有显示正确的栏按钮。任何人,任何想法为什么?

3 个答案:

答案 0 :(得分:2)

更改此行:

[self.navigationController.navigationBar.topItem setRightBarButtonItem:okButton];

这一行:

[[self navigationItem] setRightBarButtonItem:okButton];

问题是,在执行viewDidLoad时,导航栏的顶部项目(self.navigationController.navigationBar.topItem)仍然指向后视图控制器的导航项

后视图控制器是在当前顶视图控制器被压入堆栈([[viewControllers objectAtIndex:[viewControllers count] - 2] navigationItem])之前曾经是顶视图控制器的控制器。以下代码段显示导航栏的顶部项目如何仍指向viewDidLoad中后视图控制器的导航项,仅供参考:

// the view controllers currently on the navigation stack
NSArray *viewControllers = self.navigationController.viewControllers;
// The root view controller is at index 0 in the array, the back view controller is at index n-2, and the top controller is at index n-1, where n is the number of items in the array.
UIViewController *backViewController = [viewControllers objectAtIndex:[viewControllers count] - 2];
// get the navigation item of the back view controller
UINavigationItem *backNavigationItem = backViewController.navigationItem;
UINavigationItem *topItem = self.navigationController.navigationBar.topItem;
if  (backNavigationItem == topItem) {
    NSLog(@"This gets logged to the console");
}

答案 1 :(得分:1)

转到

myAudiChassisInputViewController.m 文件

放置以下代码

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIBarButtonItem *retourButton = [[UIBarButtonItem alloc] initWithTitle:@"Retour" style:UIBarButtonItemStyleBordered target:self.navigationController action:@selector(popViewControllerAnimated:)];

    UIBarButtonItem *itemOkey=[[UIBarButtonItem alloc] initWithTitle:@"OK" style:UIBarButtonItemStyleBordered target:self action:@selector(chassisOkPressed)];

    self.navigationItem.rightBarButtonItem=itemOkey;
    self.navigationItem.leftBarButtonItem=retourButton;
}

我有如下有效输出,你想拥有enter image description here

希望对你有所帮助。

答案 2 :(得分:0)

如果您有类的xib文件,则添加导航控制器并添加导航栏,然后添加UIBarButton。