在方法中间显示模态窗口

时间:2013-01-12 19:31:34

标签: macos cocoa modal-dialog nswindow

所以我有一个程序,在try / catch块发生后我需要一个模态窗口出现,这样用户可以做出特定的选择,然后我希望程序继续。我不知道如何做这项工作,我不断得到这些例外。

*** Assertion failure in -[NSApplication _commonBeginModalSessionForWindow:relativeToWindow:modalDelegate:didEndSelector:contextInfo:], /SourceCache/AppKit/AppKit-1187.34/AppKit.subproj/NSApplication.m:3920

Exception detected while handling key input.

Modal session requires modal window

我已经将我的模态表配置为以两种不同的方式显示,第一种方式是通过按钮按下,第二种方式是在我的try catch块之后。当我通过直接链接到Configure Game的按钮按下它时,它工作正常,但是当我通过另一个方法中的try catch块执行它时,它会抛出上面的所有异常。

//Method that opens the modal sheet
- (IBAction)configureGame:(id)sender
{
    //Calls a webview for the user to go to a specific location   
    NSString *FGstarter = @"http://www.google.com";
    NSURL *FGplayerUrl = [NSURL URLWithString:FGstarter];        
    [webView setMainFrameURL:[FGplayerUrl absoluteString]];

    //Opens the Modal Sheet
    [NSApp beginSheet:configureSheet modalForWindow:mainWindow
        modalDelegate:self didEndSelector:NULL contextInfo:nil];

}

//Select Method to a Select button which also closes the Sheet
- (IBAction)select:(id)sender{
    //sets a NSString Instance Var to the Current URL of the webView
    currentPage = [webView stringByEvaluatingJavaScriptFromString:@"window.location.href"]);

    //Closes the sheet
    [NSApp endSheet:configureSheet];     
}


-(NSMutableArray *)loadPlayer:(NSString *)name{
@try {
   // Code here might cause exception that gets caught in the catch
}
@catch (NSException *exception) {
    //When I call this function I get all the exceptions listed in the top of the post
    [self configureGame:nil];
    //Ideally here what would happen here is the modal sheet would pop up the user would hit the select button that calls the select method then the program continues running.  
}
   NSString *page = currentPage;
   //...Continue Using this method
}

1 个答案:

答案 0 :(得分:1)

请不要这样做。来自Apple Docs ......

  

重要在许多环境中,使用异常是相当普遍的。例如,您可能会抛出异常来表示例程无法正常执行 - 例如,当文件丢失或数据无法正确解析时。 Objective-C中的例外是资源密集型的。您不应将异常用于一般流量控制,或仅用于表示错误。相反,您应该使用方法或函数的返回值来指示发生了错误,并在错误对象中提供有关问题的信息。有关详细信息,请参阅错误处理编程指南。

另见这个问题的最佳答案:
When porting Java code to ObjC, how best to represent checked exceptions?