【发布时间】:2014-01-01 12:22:22
【问题描述】:
我想要达到的目标:
使用以下行为创建带有一些自定义图标、标签等的处理屏幕。
在窗口上添加视图,在删除之前不允许用户触摸应用程序中的任何内容。 (如处理/加载屏幕)
当显示此视图时,所有其他操作(如添加子视图、执行 segue 等)应该正常工作,但在我的加载视图下方。
希望方法 showProcessingScreen 在任何线程上工作(无论线程切换代码等应该在各自的显示/隐藏方法中)。
- 应在调用相应方法后立即显示/删除。
代码:
-(void) showProcessingScreen
{
dispatch_async(dispatch_get_main_queue(),
^{
UIStoryboard *mystoryboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
processingScreen = [mystoryboard instantiateViewControllerWithIdentifier:@"loadingViewController"];
UIWindow* mainWindow = [[UIApplication sharedApplication] keyWindow];
[mainWindow addSubview: processingScreen.view];
[mainWindow bringSubviewToFront:processingScreen.view];
});
}
-(void) hideProcessingScreen
{
dispatch_async(dispatch_get_main_queue(),
^{
[processingScreen.view removeFromSuperview];
});
}
问题:
我希望上面的代码能够立即显示/隐藏加载屏幕。
- (IBAction)proceedBtnPressed:(id)sender
{
[[GUIUtilities sharedObj] showProcessingScreen];
//Some other code here
}
当我像上面那样调用 showProcessingScreen 时,处理屏幕大约需要 2-3 秒才能显示。 但是当我删除它下面的其他代码(//其他一些代码)时,它会立即显示屏幕。
我尝试过的:
- 将代码放在 showProcessingScreen 中的其他方法中,并使用 performSelectorOnMainThread 在主线程上调用。
- 在后台调用 showProcessingScreen 并使用 performSelector 在主线程上执行显示代码。
- 这行得通
//代码
-(IBAction)proceedBtnPressed:(id)sender
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0),
^{
[[GUIUtilities sharedObj] showProcessingScreen];
//Some other code here
});
}
但我不想要 showProcessingScreen 之外的任何线程切换机制。
这是几乎在每个应用程序中都使用的常见屏幕。我在 xib 中使用了类似的代码,在我以前的应用程序中没有使用故事板等的自定义视图, 我知道这与线程有关,我在这里做错了什么?实现这一目标的最佳做法是什么? 任何帮助将不胜感激。
【问题讨论】:
标签: ios objective-c storyboard uiwindow