ios 如何识别导航堆栈中以前的视图控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7005787/
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
How to identify previous view controller in navigation stack
提问by Zhen
I have 2 seperate navigationcontrollers
, one with RootViewController
A and the other with RootViewController
B.
我有 2 个单独的navigationcontrollers
,一个是RootViewController
A,另一个是RootViewController
B。
I am able to push ViewController
C onto either A or B's navigation stack.
我能够将ViewController
C推送到 A 或 B 的导航堆栈上。
Question: When I am in ViewController
C, how can I find out if I am in the stack belonging to A or B?
问题:当我在ViewController
C 时,如何知道我在堆栈中是属于 A 还是 B?
回答by jburns20
You could use the UINavigationController
's viewControllers
property:
您可以使用UINavigationController
的viewControllers
属性:
@property(nonatomic, copy) NSArray *viewControllers
Discussion: The root view controller is at index 0 in the array, the back view controller is at index n-2, and the top controller is at index n-1, where n is the number of items in the array.
@property(nonatomic, copy) NSArray *viewControllers
讨论:根视图控制器位于数组中的索引 0,后视图控制器位于索引 n-2,顶部控制器位于索引 n-1,其中 n 是数组中的项数。
https://developer.apple.com/documentation/uikit/uinavigationcontroller
https://developer.apple.com/documentation/uikit/uinavigationcontroller
You could use that to test whether the root view controller (the one at array index 0) is view controller A or B.
您可以使用它来测试根视图控制器(数组索引为 0 的那个)是视图控制器 A 还是 B。
回答by George
Here's the implementation of the accepted answer:
这是已接受答案的实现:
- (UIViewController *)backViewController
{
NSInteger numberOfViewControllers = self.navigationController.viewControllers.count;
if (numberOfViewControllers < 2)
return nil;
else
return [self.navigationController.viewControllers objectAtIndex:numberOfViewControllers - 2];
}
回答by Chris
- (UIViewController *)backViewController
{
NSInteger myIndex = [self.navigationController.viewControllers indexOfObject:self];
if ( myIndex != 0 && myIndex != NSNotFound ) {
return [self.navigationController.viewControllers objectAtIndex:myIndex-1];
} else {
return nil;
}
}
回答by tjklemz
A more general implementation of the accepted answer:
已接受答案的更一般实现:
- (UIViewController *)backViewController {
NSArray * stack = self.navigationController.viewControllers;
for (int i=stack.count-1; i > 0; --i)
if (stack[i] == self)
return stack[i-1];
return nil;
}
This will return the correct "back view controller" regardless of where the current class is on the navigation stack.
无论当前类在导航堆栈上的哪个位置,这都将返回正确的“后视图控制器”。
回答by grassyburrito
Swift implementation of tjklemzcode as an extension:
作为扩展的tjklemz代码的Swift 实现:
extension UIViewController {
func backViewController() -> UIViewController? {
if let stack = self.navigationController?.viewControllers {
for(var i=stack.count-1;i>0;--i) {
if(stack[i] == self) {
return stack[i-1]
}
}
}
return nil
}
}
回答by Cin316
Here's a modifed version of Prabhu Beeman'sSwift code that adapts it to support Swift 3:
这是Prabhu Beeman 的Swift 代码的修改版本,可对其进行调整以支持 Swift 3:
extension UIViewController {
func backViewController() -> UIViewController? {
if let stack = self.navigationController?.viewControllers {
for i in (1..<stack.count).reverse() {
if(stack[i] == self) {
return stack[i-1]
}
}
}
return nil
}
}
回答by Adam Johns
As a UINavigationController
extension:
作为UINavigationController
扩展:
extension UINavigationController {
func previousViewController() -> UIViewController? {
guard viewControllers.count > 1 else {
return nil
}
return viewControllers[viewControllers.count - 2]
}
}
回答by Alex Reynolds
Access the n-2
element of the viewControllers
property to access the parent view controller.
访问属性的n-2
元素viewControllers
以访问父视图控制器。
Once you have that instance, you can check its type by logging what comes out of the NSStringFromClass()
function. Or you could keep some static const
identifier string in controllers A and B, and a getter function that prints out the string.
一旦你有了那个实例,你就可以通过记录NSStringFromClass()
函数的结果来检查它的类型。或者您可以static const
在控制器 A 和 B 中保留一些标识符字符串,以及一个打印出该字符串的 getter 函数。
回答by cdf1982
Swift implementation of @tjklemz code:
@tjklemz 代码的 Swift 实现:
var backViewController : UIViewController? {
var stack = self.navigationController!.viewControllers as Array
for (var i = stack.count-1 ; i > 0; --i) {
if (stack[i] as UIViewController == self) {
return stack[i-1] as? UIViewController
}
}
return nil
}
回答by Perception
Use the navigationController
method to retrieve it. See documentation on Apple's site.
使用navigationController
方法来检索它。请参阅Apple 网站上的文档。
navigationControllerA parent or ancestor that is a navigation controller. (read-only)
@property(nonatomic, readonly, retain) UINavigationController *navigationController
DiscussionOnly returns a navigation controller if the view controller is in its stack. This property is nil if a navigation controller cannot be found.
AvailabilityAvailable in iOS 2.0 and later.
navigationController作为导航控制器的父级或祖先。(只读)
@property(nonatomic, readonly, retain) UINavigationController *navigationController
讨论如果视图控制器在其堆栈中,则仅返回导航控制器。如果找不到导航控制器,则此属性为零。
可用性在 iOS 2.0 及更高版本中可用。