首先,我想强调一下,这段代码可以运行,但看起来很糟糕。我需要以编程方式更改 View Controller (不能使用 segue),所以我使用以下代码:
NSString *storyboardName = @"Main";
UIStoryboard *storyboard =
[UIStoryboard storyboardWithName:storyboardName bundle:nil];
PurchaseViewController *vc = [storyboard
instantiateViewControllerWithIdentifier"urchaseViewController"];
[controller presentViewController:vc animated:YES completion:nil];
显然它的工作原理。不幸的是,在创建第二个 Storyboard(iPad)后,我被迫将此代码更改为:
#define IDIOM UI_USER_INTERFACE_IDIOM()
#define IPAD UIUserInterfaceIdiomPad
/*
...
*/
NSString *storyboardName;
if (IDIOM == IPAD)
storyboardName = @"Main_iPad";
else
storyboardName = @"Main";
UIStoryboard *storyboard =
[UIStoryboard storyboardWithName:storyboardName bundle:nil];
PurchaseViewController *vc = [storyboard
instantiateViewControllerWithIdentifier"urchaseViewController"];
[controller presentViewController:vc animated:YES completion:nil];
它一定是比这更好的解决方案,但我没有找到。你知道替代(更好的)解决方案吗?
保持您的 iPad Storyboard名称与 iPhone 相同,只需在 '.storyboard' 之前附加 '~ipad' 并在 info.plist 中添加 'Main storyboard file base name (iPad)' 并将 Storyboard 名称作为值。然后使用下面的代码根据设备获取 Storyboard名称。
NSBundle *bundle = [NSBundle mainBundle];
NSString *storyboardName = [bundle objectForInfoDictionaryKey"UIMainStoryboardFile"];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:storyboardName bundle:bundle];
希望这会有所帮助。
关于ios - Obj-C 以编程方式更改 ViewController(两个 Storyboard),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32393755/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |