سؤال

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

هل كانت مفيدة؟

المحلول

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
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top