简体   繁体   English

如何为单个UIButton添加两个UIButton操作

[英]How to add two UIButton actions for a single UIButton

Hello every one i'm trying to add two different actions for a single UIButton. 大家好,我想为一个UIButton添加两个不同的操作。 In my view there are two UIButtons like "Edit" and "New" has different actions and a table view. 在我看来,有两个UIButton,例如“ Edit”和“ New”具有不同的操作和一个表视图。 When i click the edit button it shows editing option of the table view and the "New" button title label changes to "clear" and "Edit" to "Done", when i clicked the "Clear" text it will show alert, clicking the cancel button it goes to normal previous state. 当我单击“编辑”按钮时,它将显示表视图的编辑选项,“新建”按钮的标题标签将变为“清除”,“编辑”为“完成”,当我单击“清除”文本时,它将显示警报,单击取消按钮将恢复到正常的先前状态。 Up to this all are working fine. 至此,一切工作正常。 If we click "New" it should navigate to next view, but what i'm getting is its displaying editing options and also navigating to next view. 如果我们单击“新建”,它应该导航到下一个视图,但是我得到的是它显示的编辑选项以及导航到下一个视图。

//Editing table method //编辑表格方法

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

}

//button actions and methods //按钮动作和方法

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


}

This action methods i've tried.Thanks in advance 我已经尝试过此操作方法。

Please try with following Code , In viewDidLoad put following Code 请尝试以下Code ,在viewDidLoad放入以下Code

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

Now you have to connect following Method with your Relevant UIButtons 现在,您必须将以下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");
        }
    }
}

On the basis of tag value you can add like that below:- 根据标签值,您可以添加以下内容:-

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

}

As far as my understanding, 据我了解

first set addTarget as 'newbtnaction' to the button "New" and inside that method change the button title as "clear" and addtarget as 'clearaction' method name. 首先将addTarget设置为“ newbtnaction”,然后在该方法内部将按钮标题更改为“ clear”,将addtarget更改为“ clearaction”方法名称。

same way to the second button. 与第二个按钮相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM