2014-12-13 61 views
0
2個UIButton的行動

您好每一個我試圖將兩個不同的動作爲單個的UIButton。在我看來,有兩個像「編輯」和「新」UIButtons有不同的行動和表視圖。當我點擊編輯按鈕,它顯示了表視圖和「新建」按鈕,標題標籤更改的編輯選項,「清除」和「編輯」,以「完成」,當我點擊「清除」文本會自動顯示警告,點擊取消按鈕它將轉到正常的先前狀態。直到這一切都工作正常。如果我們點擊「新建」應該導航到下一個視圖,但什麼我得到的是其顯示的編輯選項,同時導航到下一個視圖。如何添加一個單一的UIButton

//編輯表法

- (void)setEditing:(BOOL)editing animated:(BOOL)animated 
{ 
    [super setEditing:editing animated:animated]; 
    [messagesTableView setEditing:editing animated:YES]; 

} 

//按鈕動作和方法

- (IBAction)editButtonAction:(id)sender 
{ 
    if ([editButton.titleLabel.text isEqualToString:@"Edit"]) 
    { 

     [self setEditing:YES]; 
     // 
     [editButton setTitle:@"Done" forState:UIControlStateNormal]; 
     [editButton setTitle:@"Done" forState:UIControlStateSelected]; 

     [createNewMessageBtn setTitle:@"Clear" forState:UIControlStateNormal]; 
     [createNewMessageBtn setTitle:@"clear" forState:UIControlStateSelected]; 
     createNewMessageBtn.tag = 2; 
     createNewMessageBtn.enabled = YES; 
     createNewMessageBtn.userInteractionEnabled = YES; 

     //  [self.voicemailTable reloadData]; 

    } 
    else if ([editButton.titleLabel.text isEqualToString:@"Done"]) 
    { 
     [self setEditing:NO]; 

     // 
     [editButton setTitle:@"Edit" forState:UIControlStateNormal]; 
     [editButton setTitle:@"Edit" forState:UIControlStateSelected]; 

     [createNewMessageBtn setTitle:@"New" forState:UIControlStateNormal]; 
     [createNewMessageBtn setTitle:@"New" forState:UIControlStateSelected]; 
     createNewMessageBtn.tag = 1; 
     createNewMessageBtn.enabled = YES; 
     createNewMessageBtn.userInteractionEnabled = YES; 
     //  [self.voicemailTable reloadData]; 

    } 

    // 
    [self.messagesTableView reloadData]; 


} 

- (IBAction)newConverstionBtnActn:(id)sender 
{ 
    if ([sender tag] == 1) 
    { 
     [self setEditing:NO]; 

     NSLog(@"tag---%ld",(long)[sender tag]); 
     [self.editButton setTitle:@"Edit" forState:UIControlStateNormal]; 
     [self.editButton setTitle:@"Edit" forState:UIControlStateSelected]; 

     [self.createNewMessageBtn setTitle:@"New" forState:UIControlStateNormal]; 
     [self.createNewMessageBtn setTitle:@"New" forState:UIControlStateSelected]; 
     createNewMessageBtn.enabled = YES; 
     createNewMessageBtn.userInteractionEnabled = YES; 
     [self performSegueWithIdentifier:@"newMessage" sender:self]; 


    } 

    else if ([sender tag] == 2) 
    { 
     NSLog(@"tag---%ld",(long)[sender tag]); 

     [self.createNewMessageBtn setTitle:@"New" forState:UIControlStateNormal]; 
     [self.createNewMessageBtn setTitle:@"New" forState:UIControlStateSelected]; 
     createNewMessageBtn.enabled = YES; 
     createNewMessageBtn.userInteractionEnabled = YES; 

     transparentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 540)]; 
     transparentView.backgroundColor = [UIColor clearColor]; 

     [self.view addSubview: transparentView]; 

     // 
     clearAllOption = [[UIButton alloc]initWithFrame: CGRectMake(5, 430, 310, 39)]; 
     clearAllOption.layer.borderColor = [[SupportFeatures getColorFromHexStr:@"FD8646"] CGColor]; 
     clearAllOption.layer.borderWidth = 2; 
     clearAllOption.layer.cornerRadius = 6; 
     [clearAllOption setTitle:@"Clear All Messages" forState:UIControlStateNormal]; 
     [createNewMessageBtn setTitleColor:[SupportFeatures getColorFromHexStr:@"555562"] forState:UIControlStateNormal]; 
      self.clearAllOption.titleLabel.font = [UIFont fontWithName:@"Lato-Regular" size:15]; 


     [clearAllOption setBackgroundColor:[SupportFeatures getColorFromHexStr:@"FD8646"]]; 
     [clearAllOption addTarget:self action:@selector(clearAllContactsAction) forControlEvents:UIControlEventTouchUpInside]; 
     //[self.view addSubview: clearAllOption]; 
     [self.transparentView addSubview: clearAllOption]; 


     // 
     cancelOption = [[UIButton alloc]initWithFrame: CGRectMake(5, 480, 310, 39)]; 
     cancelOption.layer.borderColor = [[SupportFeatures getColorFromHexStr:@"4BBAC7"] CGColor]; 
     cancelOption.layer.borderWidth = 2; 
     cancelOption.layer.cornerRadius = 6; 
     [cancelOption setTitle:@"Cancel" forState:UIControlStateNormal]; 
     cancelOption.titleLabel.font = [UIFont fontWithName:@"Lato-Regular" size:15]; 
     [cancelOption setTitleColor:[SupportFeatures getColorFromHexStr:@"555562"] forState:UIControlStateNormal]; 

     [cancelOption setBackgroundColor:[SupportFeatures getColorFromHexStr:@"4BBAC7"]]; 
     [cancelOption addTarget:self action:@selector(cancelContactsAction) forControlEvents:UIControlEventTouchUpInside]; 

     //[self.view addSubview: cancelOption]; 
     [self.transparentView addSubview: cancelOption]; 

     // 
     self.editButton.userInteractionEnabled = NO; 
//  self.createNewMessageBtn.userInteractionEnabled = YES; 

    } 
} 


- (void)clearAllContactsAction 
{ 
    // 
    [self.transparentView removeFromSuperview]; 

    // 
    self.messagesTableView.hidden = YES; 

    [self.createNewMessageBtn setTitle:@"New" forState:UIControlStateNormal]; 
    [self.createNewMessageBtn setTitle:@"New" forState:UIControlStateSelected]; 
// createNewMessageBtn.enabled = YES; 


    [self.createNewMessageBtn setTitleColor:[SupportFeatures getColorFromHexStr:@"555562"] forState:UIControlStateNormal]; 
    [self.createNewMessageBtn setTitleColor:[SupportFeatures getColorFromHexStr:@"555562"] forState:UIControlStateSelected]; 
    [self.createNewMessageBtn setUserInteractionEnabled: YES]; 

    // 
    [clearAllOption removeFromSuperview]; 
    [cancelOption removeFromSuperview]; 
    // 
    noRecentsOption = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 150, 30)]; 
    noRecentsOption.text = @"No Recents"; 
    [self.view addSubview: noRecentsOption]; 

    // 
    self.editButton.userInteractionEnabled = NO; 
    self.createNewMessageBtn.userInteractionEnabled = YES; 

} 

- (void)cancelContactsAction 
{ 
    [self setEditing:NO]; 

    [createNewMessageBtn setTitle:@"New" forState:UIControlStateNormal]; 
    [createNewMessageBtn setTitle:@"New" forState:UIControlStateSelected]; 
    createNewMessageBtn.tag = 1; 
    createNewMessageBtn.enabled = YES; 
    createNewMessageBtn.userInteractionEnabled = YES; 
    // 
    [self.transparentView removeFromSuperview]; 

    [clearAllOption removeFromSuperview]; 

    [cancelOption removeFromSuperview]; 
    // 
    [self.editButton setTitle:@"Edit" forState:UIControlStateNormal]; 
    [self.editButton setTitle:@"Edit" forState:UIControlStateSelected]; 
    editButton.enabled = YES; 
    editButton.userInteractionEnabled = YES; 


} 

此操作方法我tried.Thanks提前

+0

([editButton連接.titleLabel.text isEqualToString:@「編輯」]),它顯示了警報和導航到下一個屏幕 – svs 2014-12-16 03:20:09

回答

1

請與以下Code嘗試,在viewDidLoad放下面Code

[self.btnEdit setTitle:@"Edit" forState:UIControlStateNormal]; 
[self.btnNew setTitle:@"New" forState:UIControlStateNormal]; 

現在你有以下MethodRelevant UIButtons

- (IBAction)btnEdit_Done_Click:(id)sender { 

    if([[self.btnEdit titleForState:UIControlStateNormal] isEqualToString:@"Edit"]){ 
     NSLog(@"Put code for allow editing for TableView"); 
     [self.btnNew setTitle:@"Clear" forState:UIControlStateNormal]; 
     [self.btnEdit setTitle:@"Done" forState:UIControlStateNormal]; 
    }else{ 
     [self.btnEdit setTitle:@"Edit" forState:UIControlStateNormal]; 
     [self.btnNew setTitle:@"New" forState:UIControlStateNormal]; 
    } 

} 

- (IBAction)btnNew_Clear_Click:(id)sender { 

    if([[self.btnNew titleForState:UIControlStateNormal]isEqualToString:@"New"]){ 
     NSLog(@"Push to New Controller"); 
    }else{ 
     UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok",@"Cancel", nil]; 
     [alert setTag:101]; 
     [alert show]; 
    } 
} 

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 

    if([alertView tag] == 101){ 

     [self.btnEdit setTitle:@"Edit" forState:UIControlStateNormal]; 
     [self.btnNew setTitle:@"New" forState:UIControlStateNormal]; 
     if(buttonIndex == 1){ 
      NSLog(@"clear All Thing"); 
     }else{ 
      NSLog(@"No Action"); 
     } 
    } 
} 
在[寄件人標籤]的地方用
0

在標籤值的基礎您可以添加喜歡,下面: -

(editButton.tag==1) ? [editButton addTarget:self action:@selector(doEditAction:) 
forControlEvents:UIControlEventTouchUpInside]:[editButton addTarget:self action:@selector(doneAction:) forControlEvents:UIControlEventTouchUpInside]; 

-(void)doEditAction:(id)sender 
{ 

} 
-(void)doneAction:(id)sender 
{ 

} 
+0

怎樣才能把兩個標籤爲一個單一的按鈕?可能嗎?? – svs 2014-12-16 03:21:02

+0

是的,你可以根據你的情況設置兩個標籤。 – 2014-12-16 04:26:08

0

據我的理解,

首先將addTarget設置爲'newbtnaction'按鈕「New」,並在該方法中將按鈕標題更改爲「clear」並將addtarget更改爲'clearaction'方法名稱。

相同的方式向第二個按鈕。

相關問題