双击按钮并回调到segue会导致崩溃

时间:2013-11-07 01:00:52

标签: iphone objective-c callback uibutton segue

我有一个按钮:

- (IBAction)themeBtnAction:(id)sender
{
    NSString *language = [[OnlineStore sharedStore]getTheLanguage];
        [[OnlineStore sharedStore]getTheThemeBaseGuides:language callback:^{
        [self performSegueWithIdentifier:@"from main to themecategory" sender:self];
    }]
}

但是当用户碰巧按下按钮时会导致崩溃。可能是因为下一个viewcontroller被加载到堆上两次(我的猜测),当我尝试从那个UINavigationController弹出UIViewController时,我得到的错误消息是:Unbalanced calls to begin/end appearance transitions for <MySecondViewController: 0xb257b80>.

我该如何防止这种情况?

我试图把它放在回调中:

    if ([NSStringFromClass([[viewControlles lastObject] class]) isEqualToString: @"MainViewController"]) {

我试图在btn的选择器goToNextView中进行回调

   [self performSelector:@selector(goToNextView)  withObject:self afterDelay:1.0];

没有运气。 有什么建议。请问这是不是很清楚,因为我有点累了,现在正试着睡觉:)

1 个答案:

答案 0 :(得分:1)

在视图控制器消失之前,您可以忽略用户交互:

- (IBAction)themeBtnAction:(id)sender
{
    NSString *language = [[OnlineStore sharedStore]getTheLanguage];
    [[OnlineStore sharedStore]getTheThemeBaseGuides:language callback:^{
        [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
        [self performSegueWithIdentifier:@"from main to themecategory" sender:self];
    }]
}

- (void)viewDidDisappear:(BOOL)animated
{
    [[UIApplication sharedApplication] endIgnoringInteractionEvents];
}

或者只是以类似的方式禁用按钮上的用户互动:

button.userInteractionEnabled = NO;
...
button.userInteractionEnabled = YES;