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?
}

メソッドに問題はありません。正常に実行されますが、nav 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 に答える