ios 访问模态视图控制器父级
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6735170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access modal view controller parent
提问by Nielsou Hacken-Bergen
I'm presenting a ViewController modally. How can I access the parent view controller ?
我正在以模态方式呈现一个 ViewController。如何访问父视图控制器?
My architecture is TabBarController=>VC1=>VC2=>VC3=>MVC1, and I want to reach VC3 from MVC1.
我的架构是TabBarController=>VC1=>VC2=>VC3=>MVC1,我想从MVC1到达VC3。
In VC3, I have this code :
在 VC3 中,我有这个代码:
- (void) editAd{
AskPasswordViewController *modalViewController = [[AskPasswordViewController alloc] initWithNibName:@"AskPasswordView" bundle:nil];
NSLog(@"modalparent class=%@", [[modalViewController parentViewController] class]);
[self presentModalViewController:modalViewController animated:YES];
[modalViewController release];
}
I tried this in MVC1:
我在 MVC1 中试过这个:
- (void) sendRequest {
NSLog(@"classe : %@",[[self parentViewController] class] );
}
but it returns my TabBarViewController...
但它返回我的 TabBarViewController ...
采纳答案by justin
The way I'd go about something like this is to simply create a delegate. In AskPasswordViewController
's header, put
我要做这样的事情的方法是简单地创建一个委托。在AskPasswordViewController
的标题中,放入
id delegate;
and
和
@property (nonatomic, assign) id delegate;
Synthesize it in the implementation file. Then after you alloc/init the modal controller, and before you present it, set modalViewController.delegate = self;
. Then within the modal controller, you can call self.delegate
to get information from the view controller that presented it. I hope this helps
在实现文件中合成它。然后在你分配/初始化模态控制器之后,在你展示它之前,设置modalViewController.delegate = self;
. 然后在模态控制器中,您可以调用self.delegate
从呈现它的视图控制器获取信息。我希望这有帮助
回答by Vibhor Goyal
You can access parent by calling:
您可以通过调用访问父级:
self.presentingViewController
As per apple documentation:
根据苹果文档:
The view controller that presented this view controller (or its farthest ancestor.)
呈现此视图控制器(或其最远祖先)的视图控制器。
回答by Cyprian
You can always go further back just by calling parentViewController like so:
你总是可以通过像这样调用 parentViewController 来更进一步:
self.parentViewController.parentViewController
.... and so on until you reach the right one.
self.parentViewController.parentViewController
.... 依此类推,直到找到合适的为止。
回答by Angela Mao
Addition to Vibhor Goyal - Sometimes, self.presentingViewController registers as NavigationController. Therefore try:
除了 Vibhor Goyal - 有时,self.presentingViewController 注册为 NavigationController。因此尝试:
if let vc = self.presentingViewController.childViewControllers.last as? YourViewController {
// do stuff here
}
回答by Maris
Here I came up with universal method to navigate from any place to root.
在这里,我想出了从任何地方导航到根目录的通用方法。
You create a new Class file with this class, so that it's accessible from anywhere in your project:
import UIKit class SharedControllers { static func navigateToRoot(viewController: UIViewController) { var nc = viewController.navigationController // If this is a normal view with NavigationController, then we just pop to root. if nc != nil { nc?.popToRootViewControllerAnimated(true) return } // Most likely we are in Modal view, so we will need to search for a view with NavigationController. let vc = viewController.presentingViewController if nc == nil { nc = viewController.presentingViewController?.navigationController } if nc == nil { nc = viewController.parentViewController?.navigationController } if vc is UINavigationController && nc == nil { nc = vc as? UINavigationController } if nc != nil { viewController.dismissViewControllerAnimated(false, completion: { nc?.popToRootViewControllerAnimated(true) }) } } }
Usage from anywhere in your project:
{ ... SharedControllers.navigateToRoot(self) ... }
您使用此类创建一个新的 Class 文件,以便可以从项目中的任何位置访问它:
import UIKit class SharedControllers { static func navigateToRoot(viewController: UIViewController) { var nc = viewController.navigationController // If this is a normal view with NavigationController, then we just pop to root. if nc != nil { nc?.popToRootViewControllerAnimated(true) return } // Most likely we are in Modal view, so we will need to search for a view with NavigationController. let vc = viewController.presentingViewController if nc == nil { nc = viewController.presentingViewController?.navigationController } if nc == nil { nc = viewController.parentViewController?.navigationController } if vc is UINavigationController && nc == nil { nc = vc as? UINavigationController } if nc != nil { viewController.dismissViewControllerAnimated(false, completion: { nc?.popToRootViewControllerAnimated(true) }) } } }
在项目中的任何地方使用:
{ ... SharedControllers.navigateToRoot(self) ... }
回答by Arben Pnishi
//You have to get the root, iterate through the root's ViewControllers and then ask for the ParentViewController.
UINavigationController ?*root = (UINavigationController*?)[[(AppDelegate*) [[UIApplication sharedApplication]delegate] window] rootViewController];
for (UIViewController *VC in root.viewControllers) {
if([VC isKindOfClass:[YourParentViewController class]]){
YourParentViewController* parent = (YourParentViewController*)VC;
[parent callMethod]; // your code here
[self dismissViewControllerAnimated:YES completion:nil];
}
}