【问题标题】:Changing UIBarButtonItem title impossible?更改 UIBarButtonItem 标题不可能?
【发布时间】:2012-12-03 01:16:35
【问题描述】:

傻事,如果满足 for 循环中的某个条件,我正在尝试更改 UIBarButtonItem 上的标题。但是我遇到了麻烦。

我试过了

self.barButtonItem.title=@"NewTitle"

和

[self.barButtonItem setTitle:@"New Title"];

之后我什至在视图上做了一个 setNeedsDisplay,但没有任何运气。顺便说一句,是的,我正在从 self.navigationBar.items 获取旧的按钮数组,删除旧按钮并设置新按钮。

如果我改变屏幕方向(翻转 ipad),按钮标题会改变。但除此之外,它保持不变。

我错过了什么吗?

【问题讨论】:

    标签: ios xcode ipad title uibarbuttonitem


    【解决方案1】:

    问题

    如果你浏览文档,苹果会说:

    “您应该在将项目添加到栏之前设置此属性。”

    因此,

    cancel_btn.title = "something"  // It does not work !
    

    因为您已经将项目设置为栏。

    解决方案

    //
    let theTitle = "something"
    
    //
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: theTitle, style: 
    UIBarButtonItemStyle.plain, target: self, action: 
    #selector(cancelAction))
    

    现在你在将项目添加到栏之前设置标题。

    【讨论】:

      【解决方案2】:

      为此,我使用了UIBarButtonItem 的possibleTitles 属性。

      在创建按钮时试试这个:

      self.barButtonItem = [[UIBarButtonItem alloc] init...];
      self.barButtonItem.possibleTitles = [NSSet setWithObjects:@"Title 1", @"Title 2", nil];
      

      然后,要更改标题,请执行以下操作:

      self.barButtonItem.title = @"Title X"; // must be one of the possibleTitles
      

      【讨论】:

      • 好建议,仍然是相同的行为 - 使用 ipad 翻转,否则保持不变..
      • in viewDidLoad: self.visitButton=[[UIBarButtonItem alloc]initWithTitle:@"Visit" style:UIBarButtonItemStyleBordered target:self action:@selector(visitPlace)]; self.visitButton.possibleTitles=[NSSet setWithObjects:@"Visit", @"Unvisit", nil]; NSMutableArray *buttons=[self.navigationBar.items mutableCopy]; [按钮 addObject:self.visitButton]; self.navigationBar.items=按钮;
      • UIBarButtonItem 代码很好,但其余的都是错误的。 self.navigationBar.items 数组是 UINavigationItem 的数组,而不是 UIBarButtonItem。你想调整self.navigationItem上的按钮。
      • rmaddy,我不认为你说的是​​正确的,因为在苹果自己的 UISplitViewController 委托中,他们通过 -(void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *) 设置了一个 UIBarButtonItem aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc;当我将垫子放入纵向时,它可以工作
      • 查看UINavigationBar items] 的文档。这是UINavigationItem 的数组,而不是UIBarButtonItem。我不知道这与您刚刚发布的 UISplitViewControllerDelegate 方法有什么关系。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多