0

I'm just starting with iOS and objective C and one of the things that I'm trying to accomplish in my current app is to perform a segue based on the identity of the row in the table view and identity of the button pressed (these are buttons on top in the nav bar). The reason I'm confused is that normally when I want to do something with multiple buttons I have an IBAction for both the buttons and then use sender tags. However, in this case what I'm essentially trying to do is to have something like (if indexPath.row ==0 && Identity of button pressed ==0) self performSEguewithIdentifier sender
Now I'm not sure what goes in the second part of that if statement, if you could help me out with some code, I would be very grateful.

Thanks

David West
  • 552
  • 2
  • 9
  • 21
  • That's doable. Just to clarify, the user first selects a tableview row, then presses a barbutton up in the navigationbar? – John Sauer Jan 22 '13 at 16:50
  • Actually, it's the other way round. He first pressed a bar button and then chooses a tableview row. Now the way things are, although both of my bar buttons bring up the same tableviewrows (the text contents of the table in both cases is the same) the segues need to be different depending on which button was pressd. – David West Jan 22 '13 at 16:55

1 Answers1

2

If the user needs to press the button first, then pick a row, you can't use the compound if statement like you have in your question, since the row won't have been picked yet. One way to do it, would be to just have your button method set a value on an ivar (based on its tag) which you then check in didSelectRowAtIndexPath:. So, if you had an int ivar, buttonPressed, your IBAction could be:

@implementation ViewController {
    int buttonPressed;
}

-(IBAction)buttonPressed:(UIButton *) sender {
    buttonPressed = sender.tag;
}

Then, in the didSelectRowAtIndexPath method:

-(NSIndexPath *)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if(buttonPressed == 1) {
        // do something with indexPath.row info
        // perform whichever segue
    }else if (buttonPressed == 2) {
       // do something with indexPath.row info
        // perform whichever segue
    }
}
rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • This seems perfect, and exactly what I'm looking for! Could you please tell me how do i declare the int ivar? – David West Jan 22 '13 at 17:22
  • Hmm, I"m still getting an error. This is how I have it setup: -(IBAction)ButtonPressed:(id)sender { NavButtonPressed=sender.tag; } The error I get is: "Semantic Issue. Property 'tag' not found on object of type '__strong id'. – David West Jan 22 '13 at 17:32
  • @DavidWest, change the (id) in the IBAction name to (UIButton *) like I showed in my answer. – rdelmar Jan 22 '13 at 17:36
  • OH never mind I just edited it to be NavButtonPressed=[sender tag] and it worked. Although and I know my basics are completely wrong here, isn't dot notation supposed to work as well? – David West Jan 22 '13 at 17:37
  • @DavidWest, yes, do notation will work if the compiler knows what kind of object sender is, which it will if you do it like I said. – rdelmar Jan 22 '13 at 17:38
  • And yes, what you said worked as well. Could you tell me the difference between (id) and UIButton * and why id worked with [sender tag]? – David West Jan 22 '13 at 17:39
  • 1
    @DavidWest, id just means any object. When you use dot notation, the compiler seems to treat it like a property, and so it gives you an error since id isn't a specific thing, and has no properties. If you use the square bracket notation, the compiler knows that that's a method, and it assumes you know what you're doing, and allows you to write it. Of course, if the object actually doesn't implement that method, it will crash at run time. It's always better to explicitly type your objects, unless you don't know what kind of object you might get. – rdelmar Jan 22 '13 at 17:48