-1

I've been looking around to find a solution to this, but can't seem to find one that works for me. How to do two actions for one UIButton in iOS Objective C.

My app contain "login user" and "guest user".

  • If login user comes in UITableView enquiry button navigate to one controller.
  • If "guest user" comes in UITableView enquiry button navigate to another controller
Mahendra
  • 7,012
  • 2
  • 25
  • 50
jeevan
  • 71
  • 1
  • 10
  • 1
    can you show what you have tried to get a better understanding? – André Slotta Mar 22 '17 at 10:39
  • Try to set some bool value. If the user is logged in set bool to yes. In button IBAction check that bool value if it is yes navigate to controller you want if it is no then it is guest user so you can navigate to another controller –  Mar 22 '17 at 10:39
  • set tag for your buton based on tag open the VC – Anbu.Karthik Mar 22 '17 at 10:42
  • if (appLoginType==AMGuestLogin) { [projectListCell.enquireButton addTarget:self action:@selector(navigateToEnquire:) forControlEvents:UIControlEventTouchUpInside]; } { [projectListCell.enquireButton addTarget:self action:@selector(enquire:) forControlEvents:UIControlEventTouchUpInside]; } – jeevan Mar 22 '17 at 11:34
  • like this i tried but while calling navigateToEnquire method i m getting enquire method also – jeevan Mar 22 '17 at 11:36
  • in one selector method(enquire:) i am calling alertview that is working fine but while calling another selector method (navigateToEnquire) i am getting alert view also – jeevan Mar 22 '17 at 11:39
  • @Ram at your button click check for entered user id if that id is a guest user id navigate to that controller else if it is from login user navigate to that controller. – Tushar Sharma Mar 22 '17 at 12:01
  • @ Tushar Sharma i did like that only .. but while calling selector methods its showing issue.. means both selector methods calling in one action – jeevan Mar 22 '17 at 12:12

4 Answers4

0

On post login, you get to know that what is the type of the user is logged in. Based on user type move to respective controller. You really dont have to set multiple actions for the same button. When button is clicked , check for the logged in user type i.e guest/login and push to respective controller.

0

For eg. for each button in a tableview cell you can set a @selector() on button.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    .
    //do stuff
    .

    [cell.buttonSave addTarget:self action:@selector(btnSaveTap:) forControlEvents:UIControlEventTouchUpInside];

  if ([user isequaltostring:login user])
    cell.buttonSave.tag = <tagNumber>
   else
     cell.buttonSave.tag = <anothertagNumber>

    //do stuff
    return cell;
}

Add you can implement its action method like this

- (void)btnSaveTap:(UIButton *)sender {

    //here you can check tag number for desired action

   if(sender.tag ==tagNumber) // 
 {
  // Navigate to login user
 }
else
{
 // Navigate to Guest user
 }
}
Anbu.Karthik
  • 77,564
  • 21
  • 153
  • 132
Mahendra
  • 7,012
  • 2
  • 25
  • 50
0

Instead of adding two actions you can manage the action based on the user login flag.

i.e if user is login user then the isLogin flag will be true else it will be false.

for this you can use user defaults as below.

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isLogin"];//here yes if normal user and no if guest user.
[[NSUserDefaults standardUserDefaults] synchronize];

In button click you can maintain it.

- (void)btnclick:(UIButton *)sender {

  //here you can check login flag

 if([[NSUserDefaults standardUserDefaults] boolForKey:@"isLogin"]){
    // Navigate to login user
   }
   else{
    // Navigate to Guest user
   }
}
Nishant Bhindi
  • 2,168
  • 6
  • 21
0

You can try like this

- (IBAction)navigateToEnquire:(id)sender {

    NSLog(@"Button pressed: %@", [sender currentTitle]);    

   if ([[sender currentTitle] isEqualToString: @"login user"]) {

        // do something

   } else if ([[sender currentTitle] isEqualToString: @"guest user"]) {

     // do something

   }

}
Mahendra
  • 7,012
  • 2
  • 25
  • 50
Rajesh Dharani
  • 277
  • 4
  • 19
  • i have resolved my issue long back , by using your answer due to overload of work i didn't login here – jeevan Dec 29 '17 at 11:10