问题
Suppose that I have a UIButton control and I attached both the @IBAction method on the click and segue to it. Is it guaranteed that the click method will bed called before the segue?
Thanks in advance.
回答1:
Do this, this will work perfect in your case..
In your first ViewController.h create a property to store indexPath
@property (nonatomic,strong) NSIndexPath *indexPath;
In your second ViewController.h create a property to store indexPath
@property (nonatomic,strong) NSIndexPath *indexPath;
Now in you first ViewController.m file
Forget IBAction for now, Write this selector in your cellForRow
[cell.btnOnCell addTarget:self action:@selector(btnAction:event:) forControlEvents:UIControlEventTouchUpInside];
Write this method
-(IBAction)btnAction: (UIButton *)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tblView];
self.indexPath = [self.tblView indexPathForRowAtPoint:currentTouchPosition];
NSLog(@"%li",(long)self.indexPath.row);
[self performSegueWithIdentifier:@"segueIdToYourSecondVC" sender:self];
}
No write this method (this method will call after your performSegueWithIdentifier)
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"segueIdToYourSecondVC"])
{
// Get reference to the destination view controller
YourSecondViewController *vc = [segue destinationViewController];
vc.indexPath=self.indexPath;
}
}
Thanks..hope this will help you.
来源:https://stackoverflow.com/questions/27291427/uibutton-and-the-order-of-ibaction-and-segue