【问题标题】:performSegue inside UIButton callback not workingUIButton 回调中的 performSegue 不起作用
【发布时间】:2017-02-06 05:18:43
【问题描述】:

我在tableviewCell 中有UIButton,我从xib 加载它。通过单击该按钮,它应该转到下一个视图...我已经为此写了回调..

这是我在 xib 中的代码 TableViewCell.h

typedef void(^CallBack)(UIButton *sender);

@property (copy, nonatomic) CallBack callBack;

TableViewCell.m

- (IBAction)buttonTapped:(id)sender {
    if (_callBack) {
    _callBack(sender);
}

在视图控制器中

cell.callBack = ^(UIButton *sender) {
    [self performSegueWithIdentifier:@"segueIdentifier" sender:nil];
};

performSegue 行正在命中,但需要一些时间才能命中...为什么

【问题讨论】:

  • 可能你需要把它包装在 GCD 中
  • 我们需要在视图控制器中使用 dispatch_async 吗? @GeneCode
  • 我认为应该只在你的回调中。仅包装 performSegue。

标签: ios objective-c callback segue


【解决方案1】:

试试这个:

@interface WACustomCell : UITableViewCell

@property(readwrite,copy)void (^cellCallBackMethod)(NSInteger rowIndexPath);

-(IBAction)buttonTappedFromCell:(id)sender;

@end

@implementation WACustomCell

@synthesize cellCallBackMethod;

-(IBAction)buttonTappedFromCell:(id)sender{

 self.cellCallBackMethod(0);

//any value can be given as we will retrieve this value from CellForRowAtIndex method

 }

在视图控制器中

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

cel = ....
cell.cellCallBackMethod = ^(NSInteger cellIndex)

{

    [self cellButtonTappedFromCellWithIndex:cellIndex];

};

return cell;
}

-(void)cellButtonTappedFromCellWithIndex:(NSInteger )cellIndex
{

    [self performSegueWithIdentifier:@"segueIdentifier" sender:nil];

}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    if ([[segue identifier]isEqualToString:@"segueIdentifier"])

    {

      ViewController1 *vc = [segue destinationViewController];

  }

}

【讨论】:

  • 只有我这样做了,除了将索引路径传递给回调
  • 但我尝试了下面的代码。它工作正常,不需要时间
【解决方案2】:

在自定义 UIButton 和 UIView 中将 UserInteractionEnabled 设置为 NO 解决了我的问题...感谢您的回复...

【讨论】:

    猜你喜欢
    • 2017-04-26
    • 2017-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多