简体   繁体   English

如何关闭UIVIewController?

[英]How can I close UIVIewController?

I have a MainWindowController that I want to close on "touch inside" button event and open a new UIView. 我有一个MainWindowController,我想关闭“触摸内部”按钮事件并打开一个新的UIView。 The code that I use is this: 我使用的代码是这样的:

NewViewController *controller = [[NewViewController alloc]
initWithNibName:@"NewView" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];

But in this way the MainWindowController don't close it self. 但是通过这种方式,MainWindowController不会自行关闭它。

试试这个吧

[self dismissViewControllerAnimated:YES completion:nil];

You should use a UINavigationController to manage the push and pop of UIViewControllers. 您应该使用UINavigationController来管理UIViewControllers的推送和弹出。 In the UINavigationController you should define a root view controller, child view controllers are added over it like layers. 在UINavigationController中,您应该定义一个根视图控制器,子视图控制器就像层一样添加在它上面。

you could have a root and add a child to it. 你可以有一个根并添加一个孩子。 When you press the button, pop the child and make the root push another child. 当您按下按钮时,弹出子项并使根推送另一个孩子。

the action would be something like: 行动将是这样的:

root push child1 (root is covered by child1) root push child1(root由child1覆盖)

root pop child1 (root is visible) root pop child1(root是可见的)

root push child2 (root is covered by child2) root push child2(root由child2覆盖)

here's a link to UINavigationController documentation. 这是UINavigationController文档的链接

if you want to remove UIView's from an UIViewController then you don't need a UINavigation controller. 如果你想从UIViewController中删除UIView,那么你不需要UINavigation控制器。 You can create 2 UIView's in your viewController and swap them when you push the button. 您可以在viewController中创建2个UIView,并在按下按钮时交换它们。

1) Create a UIView to hold the swapped views(container) 1)创建一个UIView来保存交换的视图(容器)

2) Construct your UIViews that you want to swap between. 2)构建你想要交换的UIViews。 Put them in an array. 把它们放在一个数组中。

3) Hook your button up to an action method. 3)将按钮挂钩到动作方法。 This method should figure out the index of the view that needs to be shown, remove all of container's subviews, add the subview [viewsArray objectAtIndex:nextViewIndex] to container. 此方法应该找出需要显示的视图的索引,删除所有容器的子视图,将子视图[viewsArray objectAtIndex:nextViewIndex]添加到容器中。

In the selector/method, which your are calling on "touch up inside" event, try using 在您正在调用“内部触摸”事件的选择器/方法中,请尝试使用

[self dismissViewControllerAnimated:YES completion:{
//Optional, but could be used to do something magical, once controller has been dismissed
}];

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

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