【问题标题】:display and hide labels textfields using single button in xcode在 xcode 中使用单个按钮显示和隐藏标签文本字段
【发布时间】:2015-09-22 01:16:37
【问题描述】:

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


}

【问题讨论】:

  • 我在上面的代码中使用了两个带有发件人标签的按钮,但没有成功

标签: ios xcode uibutton ibaction


【解决方案1】:

“在另一个按钮单击时显示/隐藏注销按钮的代码”

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

【讨论】:

    【解决方案2】:
    -(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    
        }    
    }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 我不能像上面那样指定为 sender.tag=1 它会引发错误(在强 id 类型的对象上找不到属性标签)
      • 将发送者类型转换为 UIButton,然后您将拥有 tag 属性,然后您可以更改它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-17
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 2020-05-05
      • 2010-10-13
      相关资源
      最近更新 更多