当使用一个按钮执行操作时,我需要显示和隐藏一些标签和文本字段。详细内容我想使用单个UIButton执行两个操作我尝试了下面的代码,但它只执行一个操作。我可以使用相同的按钮显示标签,但是当我再次点击时,我无法隐藏它。
-(IBAction)changePassword:(id)sender {
if ([sender tag]==0)
{
newPasswordLbl.hidden=NO;
oldPasswordLbl.hidden=NO;
confirmPasswordLbl.hidden=NO;
newPassword.hidden=NO;
oldPassword.hidden=NO;
confirmPassword.hidden=NO;
submit.hidden=NO;
settings.hidden=NO;
connection.hidden=YES;
openTrades.hidden=YES;
closedTrades.hidden=YES;
}
else if([sender tag]==1)
{
newPasswordLbl.hidden=YES;
oldPasswordLbl.hidden=YES;
confirmPasswordLbl.hidden=YES;
newPassword.hidden=YES;
oldPassword.hidden=YES;
confirmPassword.hidden=YES;
submit.hidden=YES;
settings.hidden=NO;
connection.hidden=YES;
openTrades.hidden=YES;
closedTrades.hidden=YES;
}
}
答案 0 :(得分:2)
"在另一个按钮上显示/隐藏退出按钮的代码单击"
-(IBAction)menuBtnListing:(id)sender;{
if ([self.menubtn tag]==0) {
logoutButton = [UIButton buttonWithType:UIButtonTypeCustom];
logoutButton.frame=CGRectMake(220, 50, 100, 50);
logoutButton.layer.cornerRadius = 4;
logoutButton.layer.borderWidth = 1;
logoutButton.layer.borderColor = [UIColor colorWithRed:179.0/255.0 green:179.0/255.0 blue:179.0/255.0 alpha:1.0].CGColor;
[logoutButton setTitleColor:[UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0] forState:UIControlStateNormal];
logoutButton.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.75];
[logoutButton setTitle:@"Log Out" forState:UIControlStateNormal];
[logoutButton addTarget:self action:@selector(logout) forControlEvents:UIControlEventTouchUpInside];
logoutButton.translatesAutoresizingMaskIntoConstraints = NO;
[logoutButton setExclusiveTouch:YES];
[self.view addSubview:logoutButton];
NSLayoutConstraint * c_1 =[NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:logoutButton
attribute:NSLayoutAttributeRight
multiplier:1.0 constant:10];
NSLayoutConstraint * c_2 =[NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:logoutButton
attribute:NSLayoutAttributeTop
multiplier:1.0 constant:-1*60];
NSLayoutConstraint * equal_w = [NSLayoutConstraint constraintWithItem:logoutButton
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:0
multiplier:1.0
constant:100];
NSLayoutConstraint * equal_h = [NSLayoutConstraint constraintWithItem:logoutButton
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:0
multiplier:1.0
constant:40];
[self.view addConstraints:@[c_1,c_2]];
[logoutButton addConstraints:@[equal_w,equal_h]];
self.menubtn.tag=1;
}
else if([self.menubtn tag]==1)
{
self.menubtn.tag = 0;
logoutButton.hidden=YES;
}
}
- (void)logout
{
loginPage *secondView = [self.storyboard instantiateViewControllerWithIdentifier:@"loginPage"];
[self.navigationController pushViewController:secondView animated:YES];
}
答案 1 :(得分:1)
在changePassword
方法后更改按钮的标记。
-(IBAction)changePassword:(id)sender {
UIButton pressedButton = (UIButton *) sender;
if ([pressedButton tag]==0)
{
// change your buttons tag here.
pressedButton.tag = 1;
// change the visibility of the buttons.
}
else if([pressedButton tag]==1)
{
// change your buttons tag here.
pressedButton.tag = 0;
// change the visibility of the buttons.
}
}
答案 2 :(得分:1)
-(IBAction)changePassword:(UIButton *) sender {
sender.selected = !sender.selected;
if (sender.selected) {
// write your code here for tag 0
} else {
// write your code here for tag 1
}
}