警告:尝试显示其视图不在窗口层次结构中的***

时间:2014-09-13 19:24:06

标签: objective-c modalviewcontroller viewdidload

当我使用附加的长按手势以使用以下代码获取模态视图时,我收到此错误:

// Long press to go to settings for one
- (void)longPressOne:(UILongPressGestureRecognizer*)gesture {
       [self performSegueWithIdentifier:@"buttonOne" sender:self];
}

// Long press to go to settings for two
- (void)longPressTwo:(UILongPressGestureRecognizer*)gesture {
    [self performSegueWithIdentifier:@"buttonTwo" sender:self];
}

- (void)viewDidLoad {

    // Add gesture to buttonOne
    UILongPressGestureRecognizer *longPressOne = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressOne:)];
    [self.buttonOne addGestureRecognizer:longPressOne];


    // Add gesture to buttonTwo
    UILongPressGestureRecognizer *longPressTwo = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressTwo:)];
    [self.buttonTwo addGestureRecognizer:longPressTwo];

}

模态segue在视图控制器到目标视图的故事板上被挂起。我知道当故事板上有多个segue时会有这个问题的报告,但我只有一个,因为我无法通过按钮创建一个segue来长按Storyboard。

知道为什么会这样吗?

1 个答案:

答案 0 :(得分:1)

我已经通过改变处理手势的代码来解决这个问题,如下所示:

// Long press to go to settings for one
- (void)longPressOne:(UILongPressGestureRecognizer*)gesture {

        if (gesture.state == UIGestureRecognizerStateBegan)
        {
            [self performSegueWithIdentifier:@"buttonOne" sender:self];
        }

}

// Long press to go to settings for two
- (void)longPressTwo:(UILongPressGestureRecognizer*)gesture {
    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        [self performSegueWithIdentifier:@"buttonTwo" sender:self];
    }

}

这似乎解决了这个问题。

相关问题