如何在初始化新的navigationcontroller时向根视图控制器添加barbuttonitem

时间:2011-10-20 07:36:23

标签: ios uinavigationcontroller

当我初始化一个navigationcontroller时,我想在根视图控制器的导航栏中添加一个rightbarbuttonitem。所以自定义视图控制器不在navigationcontroller中,接下来就是。 我的代码是:

        TipsViewController *tipsView = [[TipsViewController alloc] initWithNibName:@"TipsViewController" bundle:[NSBundle mainBundle]];
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:tipsView];
        self.loginViewDelegate = tipsView;
        UINavigationItem *navigationItem = [[navigationController navigationItem] init];
        UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"load" style:UIBarButtonItemStylePlain target:self action:nil];
        navigationItem.rightBarButtonItem = barButtonItem;
        [self setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
        [loginViewDelegate passRegistrationResult:str];
        [self presentModalViewController:navigationController animated:YES];
        [tipsView release];
        [navigationController release];

我试试这个但不行......

1 个答案:

答案 0 :(得分:0)

api presentModalViewController:动画: 它的第一个参数是UIViewController,而UINavigationController是UIViewController的子类。 因此,不能直接呈现UINavigationController,但您可以提供UIViewController并手动添加UINavigationBar和UIBarbuttonItem或使用Interface Builder。

希望它可以帮助你,尽管我不太确定〜:)