简体   繁体   English

如何关闭视图— Xcode

[英]How to close a view — Xcode

I created a window based on NSWindowController It is called with... 我创建了一个基于NSWindowController的窗口,它被称为...

view2Controller = [[View2Controller alloc] initWithWindowNibName:@"View2"];
[view2Controller showWindow:self];

That works great, and I have a functioning window. 效果很好,并且我有一个正常运行的窗口。
Now I need to close it. 现在我需要关闭它。

exit(0); // closes the entire application
close(0); // does is in the documentation, but it nothing

I found a suggestion to use.. 我找到了使用建议。

[self dismissModalViewControllerAnimated:YES];

, but that seems to apply only to UI views, and generates an error. ,但这似乎仅适用于UI视图,并会产生错误。

How can I close the window that I have here? 如何关闭此处的窗口?

You seem to be confusing OS X and iOS programming. 您似乎对OS X和iOS编程感到困惑。 Both run objective-c, but only OS X is using initWithWindowNibName . 两者都运行Objective-c,但是只有OS X使用initWithWindowNibName From your tags, it seems like you are developing for iPhones. 从您的标签看来,您似乎正在为iPhone开发。

Look up the tutorials on handling UIViews and UIViewControllers . 查找有关处理UIViewsUIViewControllers的教程。

try [view2Controller removeFromSuperview]; 尝试[view2Controller removeFromSuperview];

In your case, I would use the View Based Application. 在您的情况下,我将使用基于视图的应用程序。 The difference between window and view is that the window template does not create a view controller class and related user interface file (.xib). 窗口和视图之间的区别在于,窗口模板不会创建视图控制器类和相关的用户界面文件(.xib)。 It just gives you a blank window. 它只是给您一个空白窗口。 In iOS there can only be 1 window, but multiple views can be presented on that window. 在iOS中,只能有1个窗口,但是可以在该窗口上显示多个视图。

The View based template does everything that the window based template does PLUS it creates a view controller class and that class's xib file. 基于视图的模板可以执行基于窗口的模板的所有操作,此外,它还可以创建视图控制器类以及该类的xib文件。 In addition, it adds that first view controller to your window. 另外,它将第一个视图控制器添加到您的窗口中。 In iOS, when you want to show another view, you'll almost certainly want another view controller class. 在iOS中,当您想要显示另一个视图时,几乎可以肯定需要另一个视图控制器类。 View controller classes can add additional views on top of themselves with ease. 视图控制器类可以轻松地在自身之上添加其他视图。

Since the Window template gives you 0 to start with and the View template gives you 1 to start with and you'll eventually need 2 for your two views, it'll be less work for you to start with the View template. 由于Window模板以0开头,而View模板以1开头,并且您最终将需要两个视图,因此从View模板开始的工作量较小。

if you're working with UIWindow you should take a look at this . 如果您正在使用UIWindow,则应该看看this but you're working with UIViewController, there are two solution: 但是您正在使用UIViewController,有两种解决方案:

1.) if your UIViewController is presenting as modalViewController 1.)如果您的UIViewController呈现为modalViewController

[myViewController dismissModalViewControllerAnimated:YES];

2.)else if your UIViewController navigated from other UIViewController or UIWindow you should try this: 2)否则,如果您的UIViewController从其他UIViewController或UIWindow导航的,则应尝试以下操作:

[self.navigationController popViewControllerAnimated:YES];

if you're working with UIView, you should use: [myView removeFromSuperView]; 如果使用的是UIView,则应使用: [myView removeFromSuperView];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM