hide/unhide 单击导航中的栏按钮项的文本字段

hide/unhide textfield by clicking bar button item in navigation

我有一个 viewcontroller。在顶部,我有一个 textfield.And,我在导航栏上也有一个栏按钮项(添加按钮)。在 viewdidload 中,最初我的 texfield 将被隐藏。当用户按下该栏按钮(添加按钮)时,我的文本字段应该可见。现在它运作良好。

需要者: 当用户按下(第一次)时,我的栏按钮(添加按钮)应该可见。然后当用户再次按下同一个栏按钮项(添加按钮)时,它应该再次 hidden.i 可见。但我不知道如何隐藏它......我是初学者请帮帮我

我知道它应该在 if statement.but 中完成我不知道应该使用什么条件...帮助我这个

提前致谢!

#import "ViewController.h"

@interface ViewController () {

    UIBarButtonItem *addButton;

}

@end

@implementation ViewController
@synthesize tableView;
@synthesize textField;


- (void)viewDidLoad {
    [super viewDidLoad];

    addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addButtonPressed:)];

    self.navigationItem.rightBarButtonItem = addButton;

    txtField.hidden = YES;

}
- (void)addButtonPressed:(id)sender
{

    txtField.hidden = NO;


}


@end

你可以通过多种方式做到这一点,就像为 yes:No 条件设置 BOOL,否则使用 Tags 标识,否则使用一些常见的 NSString,例如

]
@interface ViewController () {

UIBarButtonItem *addButton;
NSString *GettouchStr;
}

 - (void)viewDidLoad {
[super viewDidLoad];

addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addButtonPressed:)];
self.navigationItem.rightBarButtonItem = addButton;
txtField.hidden = YES;

 GettouchStr=@"hidden";

}

- (void)addButtonPressed:(id)sender
{
 if ([GettouchStr isEqualToString:@"hidden"])
 {
   [UIView animateWithDuration:0.3 animations:^{
    txtField.alpha = 1;
} completion: ^(BOOL finished) {

  txtField.hidden = NO;
   GettouchStr=@"UNhidden";
 }];


  }
  else
   {

     [UIView animateWithDuration:0.3 animations:^{
    txtField.alpha = 0;
  } completion: ^(BOOL finished) { 
        txtField.hidden = YES;
        GettouchStr=@"hidden";

 }];


  }


 }

选择 - 2 附加动画

- (void)addButtonPressed:(id)sender
{
 if ([GettouchStr isEqualToString:@"hidden"])
 {

  txtField.alpha = 1;
      [UIView animateWithDuration:2.f delay:0.f options:UIViewAnimationOptionCurveEaseIn animations:^{
   txtField.alpha = 0;
} completion:^(BOOL finished) {
    [UIView animateWithDuration:2.f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^{
       txtField.alpha = 1;
       txtField.hidden = NO;
        GettouchStr=@"UNhidden";
    } completion:nil];
}];





  }
  else
   {
      txtField.alpha = 0;
      [UIView animateWithDuration:2.f delay:0.f options:UIViewAnimationOptionCurveEaseIn animations:^{
   txtField.alpha = 1;
} completion:^(BOOL finished) {
    [UIView animateWithDuration:2.f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^{
       txtField.alpha = 0;
       txtField.hidden = YES;
        GettouchStr=@"hidden";
    } completion:nil];
}];






  }


 }

选择 3

看到这个link可能对你有帮助

好的,您必须定义一个 Bool 变量,像这样在单击按钮时更改该 Boolean 变量的状态

#import "ViewController.h"

@interface ViewController () {
    Bool isShowingTF;
    UIBarButtonItem *addButton;

}

@end

@implementation ViewController
@synthesize tableView;
@synthesize textField;


- (void)viewDidLoad {
    [super viewDidLoad];
    isShowingTF = NO;
    addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addButtonPressed:)];

    self.navigationItem.rightBarButtonItem = addButton;

    txtField.hidden = YES;

}
- (void)addButtonPressed:(id)sender
{

    if (isShowingTF) {
        txtField.hidden = YES;
    } else {
        txtField.hidden = NO;
    }
    isShowingTF = ! isShowingTF;
}


@end

在Swift 4、如果你右边只有一个bar button item你可以用这个,

self.navigationItem.rightBarButtonItem = nil; //To Hide

self.navigationItem.rightBarButtonItem = barButtonItem //To show

假设您的右侧有多个条形按钮,例如假设您的导航项右侧有两个条形按钮项(搜索按钮和筛选按钮)。现在你只需要隐藏搜索按钮,你可以使用 like,

self.navigationItem.rightBarButtonItems = [filterItem]

现在发生的事情是,您可以完全隐藏导航项中的搜索按钮,过滤项代替搜索项

那么如果你想显示隐藏的栏按钮,

self.navigationItem.rightBarButtonItems = [searchItem, filterItem]

现在在 navigationItem 中,searchItem 作为第一项,filterItem 作为第二项。