如何从窗口中删除视图?
我使用 Cocos2D 作为我的主要框架。在某些情况下,我希望 Cocos2D 加载 nib 文件并将其作为视图:
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
TargetPlayerViewController *myController = [[TargetPlayerViewController alloc]initWithNibName:@"TargetPlayerViewController" bundle:nil];
[window addSubview:[myController view]];
[window makeKeyAndVisible];
这按预期工作,并显示 TargetPlayerViewController
。精彩的!
我需要知道的是:加载该视图后,如何让视图自行删除?我尝试了几种不同的方法,但所有方法都会导致程序崩溃。
为了测试,我在视图上设置了一个按钮来触发此方法:
- (IBAction)GTFOnow:(id)sender {
NSLog(@"GFTO");
//window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//[self.view removeFromSuperview];
//[window makeKeyAndVisible];
}
GTFOnow 是 TargetPlayerViewController 中的一个方法。当它被调用时,当前的子视图(在上面的 Cocos2D 代码中调用)应该从窗口中删除。
I am using Cocos2D for my main framework. In some cases, I want Cocos2D to load a nib file and have that be the view:
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
TargetPlayerViewController *myController = [[TargetPlayerViewController alloc]initWithNibName:@"TargetPlayerViewController" bundle:nil];
[window addSubview:[myController view]];
[window makeKeyAndVisible];
This works as expected, and shows the TargetPlayerViewController
. Wonderful!
What I need to know is: once that view has been loaded, how can I have the view remove itself? I've tried a few different ways, but all of them result in the program crashing.
To test I have a button on the view set up which triggers this method:
- (IBAction)GTFOnow:(id)sender {
NSLog(@"GFTO");
//window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//[self.view removeFromSuperview];
//[window makeKeyAndVisible];
}
GTFOnow is a method in TargetPlayerViewController. When it is called, the current subview (that was called in the Cocos2D code above) should be removed from the window.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您不应该仅仅因为要删除子视图而创建新窗口。其次,无论发生其他情况,都不会导致应用程序崩溃。您在哪一堂课上有
GTFOnow
方法?我想在TargetPlayerViewController
类中?First of all, you shouldn't create a new window just because you want to remove a subview. Secondly, whatever else happens, this shouldn't cause the app to crash. In which class do you have the
GTFOnow
method? I suppose in theTargetPlayerViewController
class?