UIButton和IBAction和segue的顺序

时间:2014-12-04 10:18:26

标签: ios uibutton segue

假设我有一个UIButton控件,并且我在点击上附加了@IBAction方法,并将其转换为它。是否保证click方法会在segue之前调用?

提前致谢。

1 个答案:

答案 0 :(得分:3)

这样做,这将在你的情况下完美。

在您的第一个ViewController.h中创建一个存储indexPath的属性

@property (nonatomic,strong) NSIndexPath *indexPath;

在第二个ViewController.h中创建一个存储indexPath的属性

@property (nonatomic,strong) NSIndexPath *indexPath;

现在在你的第一个ViewController.m文件

暂时忘记IBAction,将此选择器写入cellForRow

[cell.btnOnCell addTarget:self action:@selector(btnAction:event:) forControlEvents:UIControlEventTouchUpInside];

编写此方法

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

不编写此方法(此方法将在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;

     }
 }

谢谢......希望这会对你有帮助。