通过单击导航中的栏按钮项隐藏/取消隐藏文本字段

时间:2015-09-04 15:23:08

标签: ios tableview

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

Needer:  当用户按下(第一次)时,我的栏按钮(添加按钮)应该可见。然后再次当用户按下相同的栏按钮项目(添加按钮)时,它应该再次隐藏。我做了可见。但我不知道如何使其隐藏...我是非常初学者请帮帮我

我知道它应该在if语句中完成。但我不知道应该使用什么条件......帮助我

提前致谢!

#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

3 个答案:

答案 0 :(得分:1)

您可以通过多种方式执行此操作,就像将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可能对您有所帮助

答案 1 :(得分:1)

好的,你必须定义一个Bool变量,点击你的按钮改变那个布尔变量的状态

#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

答案 2 :(得分:0)

在Swift 4中,如果右侧只有一个条形按钮,则可以使用它,

  

self.navigationItem.rightBarButtonItem = nil; //隐藏

     

self.navigationItem.rightBarButtonItem = barButtonItem //显示

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

  

self.navigationItem.rightBarButtonItems = [filterItem]

现在发生了什么,您可以将导航按钮中的搜索按钮完全隐藏起来,而过滤器项将代替搜索项

然后,如果要显示隐藏的条形按钮,

  

self.navigationItem.rightBarButtonItems = [searchItem,filterItem]

现在NavigationItem searchItem出现在第一项中,filterItem出现在第二项中。