Warning: Attempt to present *** whose view is not in the window hierarchy

前提是你 提交于 2019-12-25 07:12:56

问题


I'm receiving this error when I am using an attached long press gesture to get a modal view to come up using the following code:

// 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];

}

The modal segue is hitched up on the storyboard from the viewcontroller to the destination view. I know there are reports of this problem when there are multiple segues on the storyboard, but I just have the one as I can't create a segue from the button for a long press on Storyboard.

Any idea why this is happening?


回答1:


I have fixed this by altering the code for handling the gestures, as below:

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

}

This seems to fix the problem.



来源:https://stackoverflow.com/questions/25826814/warning-attempt-to-present-whose-view-is-not-in-the-window-hierarchy

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