我有一个 View Controller 。在顶部我有一个文本字段。而且我在导航栏上有一个栏按钮项(添加按钮)。在 viewdidload
中,最初我的 texfield 将被隐藏。当用户按下该栏按钮(添加按钮)时,我的文本字段应该可见。现在它运作良好。
需要者: 当用户按下(第一次)时,我的栏按钮(添加按钮)应该可见。然后当用户再次按下同一个栏按钮项(添加按钮)时,它应该再次被隐藏。我在可见中做了。但我不知道如何再次隐藏它......我是初学者请帮帮我
我知道它应该在 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
最佳答案
好的,您必须定义一个 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
关于ios - 通过单击导航中的栏按钮项隐藏/取消隐藏文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32401550/