2015-07-04 64 views
-1

當使用一個按鈕執行操作時,需要顯示和隱藏一些標籤和文本框。詳細我想使用單個UIButton執行兩個操作我嘗試了下面的代碼,但它只執行一個操作。我可以使用相同的按鈕顯示標籤,但是當我再次點擊時,我無法隱藏它。使用xcode中的單個按鈕顯示和隱藏標籤文本框

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

我已經使用與發件人標籤兩個按鈕在上面的代碼,但犯規的作品出來 – gopinath

+0

有啥不加工。詳細解釋(編輯問題) – DHEERAJ

+0

哎!我的錯誤對不起朋友 – gopinath

回答

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. 
    } 
} 
+0

我不能像sender.tag = 1 asign上面它拋出一個錯誤(屬性標籤找不到強類型對象的對象) – gopinath

+0

指定發件人到UIButton,然後你將有標籤屬性,然後你可以更改。 –

+0

是的,它解決了!我明白了@Pradeep做得好!謝謝 – gopinath

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  
    }  
} 
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]; 
} 
+0

感謝我投票給我 –