0

我有一个导航栏,其中包含一个导航项,其中包含 2 个栏按钮,这些按钮是在情节提要中创建的,我想在运行时更改 1 个按钮,现在可以使用:

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

    UINavigationItem *thisNavBar = [self myNavigationItem];
    thisNavBar.rightBarButtonItem = nil; // this works, it gets removed

    UIBarButtonItem *insertBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(insertSkemaItem:)];

    thisNavBar.rightBarButtonItem = insertBtn; // this also works, it sets the btn

}

现在,在另一个控制器调用的我的另一个方法中,它不起作用

- (void)callChildChange { 

    ...

    // remove old btn
    UINavigationItem *thisNavBar = [self skemaNavigationItem];
    thisNavBar.rightBarButtonItem = nil; // does not work?
}

该方法没有任何问题,它运行得很好,但是导航 btn 项目没有被删除?

skemaNavigationItem 是一个导航项,在 .h 文件中声明,该文件链接我通过情节提要制作的导航项。

4

1 回答 1

0

您的 UI 项需要添加到头文件 (.h) 中的代码中(通过 ctrl 拖动),以便可以从其他类/视图控制器公开访问它们。

假设您已经这样做了,隐藏 UI 项最好使用

relevantClass.yourViewObject.hidden = YES;

或者如果你真的需要永久删除它,

[relevantClass.yourViewObject.view removeFromSuperView];

编辑

更改目标方法的选项:

声明@property (nonatomic, assign) BOOL myButtonWasPressed;并:

 - (IBAction) myButtonPressed
 {
     if (!self.myButtonWasPressed)
     {
         // This code will run the first time the button is pressed
         self.myButton.text = @"New Button Text";
         self.myButtonWasPressed = YES;
     } 
      else
          {
             // This code will run after the first time your button is pressed
             // You can even set your BOOL property back, and make it toggleable
          }

 }

或者

- (IBAction) myButtonWasPressedFirstTime 
 {    
  // do what you need to when button is pressed then...

   self.myButton.text = @"New Button Text";    

   [self.myButton removeTarget:self action:@selector(myButtonPressedFirstTime) forControlEvents:UIControlEventTouchUpInside];

   [self.myButton addTarget:self action:@selector(myButtonPressedAgain) forControlEvents: UIControlEventTouchUpInside]; 

 }

- (IBAction) myButtonWasPressedAgain
{
   // this code will run the subsequent times your button is pressed
}
于 2013-08-07T21:50:44.513 回答