0

私のviewWillAppearメソッドは「-(void)doSomething」を呼び出します。

- (void)doSomething
{
    Y4AppDelegate * delegate = (Y4AppDelegate *)[[UIApplication sharedApplication] delegate];
    if(delegate.booSomeValue == 0) {
        UIButton * aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [aButton setFrame:CGRectMake(20,360,280,40)];
        [aButton setTitle:@"Title"
                 forState:UIControlStateNormal];
        [aButton addTarget:self
                    action:@selector(mySelector)
          forControlEvents:UIControlEventTouchDown];
        [self.view addSubview:aButton];
    }
}

動作しますが、aButtonは引き続き表示されます。aButtonを非表示にするにはどうすればよいですか?私は3つのUIViewControllerを持っています。3番目に、delegate.booSomeValueをtrueに設定します。以前のUIViewControllerに戻ると、このviewWillAppearを呼び出しますが、aButtonが表示されます。隠したい。

4

2 に答える 2

1

問題は、一度追加したことです。戻ったときに、2つ目の追加はしていませんが、最初に追加したものはまだ残っているため、削除する必要があります。

そのためには、最初にボタンを保存するためのプロパティを作成し、それが存在するかどうかを確認する必要があります

if ( ... show button condition ... ) {

    if (!aButton) {
        ... create and show button ...
    }
}
else {
    if (aButton) {
         [aButton removeFromSuperview];
         aButton = nil;
    }
}
于 2013-01-25T14:43:25.500 に答える
1

このコードをviewDidLoadに移動します

- (void)viewDidLoad {
    [super viewDidLoad]
    UIButton * aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    aButton.tag = 101;
    [aButton setFrame:CGRectMake(20,360,280,40)];
    [aButton setTitle:@"Title"
             forState:UIControlStateNormal];
    [aButton addTarget:self
                action:@selector(mySelector)
      forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:aButton];
}

- (void)doSomething
{
    Y4AppDelegate * delegate = (Y4AppDelegate *)[[UIApplication sharedApplication] delegate];
    UIButton * aButton =  (UIButton*)[self.view viewWithTag:101]; 
    aButton.hidden = delegate.booSomeValue;
}
于 2013-01-25T14:48:28.350 に答える