UIButton and the order of IBAction and segue

喜夏-厌秋 提交于 2019-12-13 04:08:15

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!