ios 以编程方式获取故事板 ID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13708660/
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
Programmatically get a Storyboard ID?
提问by Jalakoo
Trying to see if a UIViewController or UIView can identify its Storyboard ID. So was hoping for:
尝试查看 UIViewController 或 UIView 是否可以识别其 Storyboard ID。所以希望:
UIViewController *aViewController;
NSString *storyboardID = aViewController.storyboard.id; //not an actual property
or:
或者:
NSString *storyboardID = [aViewController.storyboard valueForKey:@"storyboardId"]; //also not a working call
But no joy and couldn't find a similar solution online. Does anyone know if this is even possible?
但没有喜悦,也无法在网上找到类似的解决方案。有谁知道这是否可能?
回答by Lolloz89
You can use the restorationIdentifier, it's right above the Storyboard identifier and it's a UIViewController property.
您可以使用restoreIdentifier,它就在Storyboard 标识符的正上方,并且是一个UIViewController 属性。
回答by Erickson1
You can use the Restoration ID:
您可以使用恢复 ID:
NSString *restorationId = self.restorationIdentifier;
Just check the checkbox 'Use Storyboard ID'
只需选中“使用故事板 ID”复选框
回答by LombaX
The storyboard id is only meant to find and instantiate a VC from a storyboard. As written in the UIStoryboard reference:
故事板 id 仅用于从故事板中查找和实例化 VC。正如 UIStoryboard 参考中所写:
"This identifier is not a property of the view controller object itself and is only used by the storyboard file to locate the view controller."
“此标识符不是视图控制器对象本身的属性,仅由故事板文件用于定位视图控制器。”
Why do you need it?
你为什么需要它?
回答by Apple_iOS0304
You can also try doing something like this:-
你也可以尝试做这样的事情:-
NSString *storyboardId = [viewController valueForKey:@"storyboardIdentifier"];
This will precisely give you the Storyboard Id that you have set via interface builder.
这将准确地为您提供您通过界面构建器设置的 Storyboard Id。
Swift extension:
快速扩展:
extension UIViewController {
var storyboardId: String {
return value(forKey: "storyboardIdentifier") as? String
}
}
回答by serge-k
The most reliable method for returning the "id" of the UIViewController or UIView is...
返回 UIViewController 或 UIView 的“id”的最可靠方法是...
NSString *viewControllerName = [[NSString alloc] initWithString:viewController.nibName];
This will return... "29w-Ic-LNo-view-FDu-oq-UpZ", where "29w-Ic-LNo" is the Object ID of the UIViewController and "FDu-oq-UpZ" is the Object ID of the UIView.
这将返回...“29w-Ic-LNo-view-FDu-oq-UpZ”,其中“29w-Ic-LNo”是 UIViewController 的对象 ID,“FDu-oq-UpZ”是界面视图。
However, you may also use...
但是,您也可以使用...
NSString *viewControllerName = [[NSString alloc] initWithString:viewController.title];
This will return the "Title" of the UIViewController in the Attributes Inspector; so just as easily as you added the Storyboard ID to the UIViewController, you may also add a title.
这将在属性检查器中返回 UIViewController 的“标题”;因此,就像将 Storyboard ID 添加到 UIViewController 一样容易,您还可以添加标题。
回答by Deepak Kumar Sahu
You can compare with class name . import class and then try.
您可以与 class name 进行比较。导入类然后尝试。
NSArray *viewControllers = self.navigationController.viewControllers;
UIViewController *root = [viewControllers objectAtIndex:0];
if ([root isKindOfClass:[UserLogin class]]) {
//--- do ---
}