Iphone &引用;self.navigationItem.RightBarButtonim“;不起作用

Iphone &引用;self.navigationItem.RightBarButtonim“;不起作用,iphone,objective-c,ios,viewdidload,rightbarbuttonitem,Iphone,Objective C,Ios,Viewdidload,Rightbarbuttonitem,下面的代码直接取自苹果公司的NavBar示例代码。我把它放在viewDidLoad方法中,用于我的应用程序中以模式呈现的视图,但它不起作用 UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"AddTitle", @"") style:UIBarB

下面的代码直接取自苹果公司的NavBar示例代码。我把它放在viewDidLoad方法中,用于我的应用程序中以模式呈现的视图,但它不起作用

UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"AddTitle", @"")
                                                               style:UIBarButtonItemStyleBordered
                                                              target:self
                                                              action:@selector(addAction:)] autorelease];
self.navigationItem.rightBarButtonItem = addButton;

有什么建议吗?

我假设您的视图控制器实际上是一个UINavigationController,其他一切都准备好了。在这种情况下,我会改变两件事

  • 我不会自动释放伊斯兰教徒。对于视图控制器来说,这往往是不可靠的,因此请在清理时将该按钮添加到要解除锁定的内容列表中

  • 我会使用setter函数来设置按钮。这是我在导航控制器中使用的代码

    clearAllButton=[[UIBarButtonItem alloc]initWithTitle:@“全部清除”样式:UIBarButtonItemStylePlain目标:自我操作:@selector(右按钮按下:)

    [[self-navigationItem]setRightBarButtonItem:clearAllButton]


  • 您需要在显示其他视图的页面上使用这些代码行

    sceondController *obj=[[[sceondController alloc] initWithNibName:@"sceondController" bundle:nil] autorelease];
            UINavigationController *navController=[[[UINavigationController alloc] initWithRootViewController:obj] autorelease];
    
            [self.navigationController presentModalViewController:navController animated:NO];
    
    在第二个视图中,使用和制作导航按钮相同的代码


    可能它解决了您的问题。

    好的,解释解决方案:

    presentModalViewController:动画:以模式显示viewController,它没有
    UINavigationBar
    ,因此您可以执行以下操作:

  • 在viewController的nib中添加一个
    UINavigationBar
    ,并在那里添加“添加”按钮以及所有需要设置的内容
  • 您可以使用
    pushViewController:animated
    :以模式显示viewController,它将位于导航堆栈上,并具有
    UINavigationBar
    供您添加按钮
  • 如果您的第一个viewController不是
    UINavigationController
    ,则使用
    pushViewController:animated:
    无法解决此问题,因此您可以将
    UINavigationController
    以viewController作为rootViewController的方式呈现:
  • YourViewController*viewController=[[YourViewController alloc]initWithNibName:@“YourViewController”捆绑包:nil]自动释放];
    UINavigationController*navController=[[UINavigationController alloc]initWithRootViewController:viewController]autorelease];
    [self.navigationController presentModalViewController:navController动画:是];
    

    希望这些有助于在真实设备中运行应用程序。在iOS6中,它在模拟器上不工作。

    您在那里有导航栏吗?在presentModalViewController方法中,大多数情况下viewController不接受navigationController。你是说我应该使用导航栏以外的其他东西,以便在模式视图的顶部显示一个栏,以便显示“添加”按钮吗?是的,实际上presentModalViewController是这样做的,以模式显示viewController,而不是nagivationController,在这种情况下,您应该使用pushViewController:animated:或将viewController放入另一个navigationController中,并将该nav显示为模式viewController。。。或者(没有尝试)将navigationController modalTransitionStyle更改为您想要的样式(同样,我自己没有尝试,所以我不知道它是否有效)self.navigationController.navigationBarHidden=NO;把这句话放在第一位就够了。2) 首先,如何让模态视图显示?也许我的问题是退后了一步。听起来是这样的。你的评论也属于上述答案。我认为它是按照赫尔茨的建议进行的。顺便说一句,我同意。也就是说,如果你想以一种方式呈现。就我个人而言,我已经发布了大量的应用程序,但从来没有发现需要一个模态视图控制器。使用setter方法设置autorelease对象会有一个retain,所以autorelease uibarbuttonite是可以的;另外,您使用setter方法所做的与“self.navigationItem.RightBarButtonim”做的事情是一样的,据我所知(我是noob),我知道您是对的,但将此代码放置在firstController中已导致视图完全停止显示。您需要在sceondController处提供类名和nib名称,并在此处提供nib名称initWithNibName:@“nib名称”