在 xcode 中使用单个按钮显示和隐藏标签文本字段

display and hide labels textfields using single button in xcode

我需要在使用一个按钮执行操作时显示和隐藏一些标签和文本字段。详细地说,我想使用单个 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;
    }


}

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.
   }
}
-(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    
    }    
}

"Code to show/hide Logout Button on another Button click"

-(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];
}