Answers:
在情节提要中,转到“属性”检查器,然后设置视图控制器的“标识符”。然后,您可以使用以下代码显示该视图控制器。
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"myViewController"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:NULL];
[self presentViewcontroller]
按以下顺序用以下几行替换逻辑:1)self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
2)self.window.rootViewController = vc;
和3)[self.window makeKeyAndVisible];
。您也可以摆脱这一限制,modalTransitionStyle
因为这不是来自应用程序委托的模式转换。
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "viewController")
self.navigationController!.pushViewController(vc, animated: true)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("viewController")
self.navigationController!.pushViewController(vc, animated: true)
先决条件
为您的视图控制器分配一个Storyboard ID。
IB>显示身份检查器>身份>故事板ID
斯威夫特(旧版)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("viewController") as? UIViewController
self.navigationController!.pushViewController(vc!, animated: true)
如果您想在没有任何navigationController的情况下使用,则必须使用以下代码:
let Storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = Storyboard.instantiateViewController(withIdentifier: "viewController")
present(vc , animated: true , completion: nil)
self.storyboard
合并并合并line1&2
在属性检查器中,为该视图控制器提供标识符,以下代码对我有用
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
DetailViewController *detailViewController = [storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
[self.navigationController pushViewController:detailViewController animated:YES];
试试这个
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"Login"];
[[UIApplication sharedApplication].keyWindow setRootViewController:vc];
对于3和4,可以执行此操作。优良作法是将故事板的名称设置为等于StoryboardID。
enum StoryBoardName{
case second = "SecondViewController"
}
extension UIStoryBoard{
class func load(_ storyboard: StoryBoardName) -> UIViewController{
return UIStoryboard(name: storyboard.rawValue, bundle: nil).instantiateViewController(withIdentifier: storyboard.rawValue)
}
}
然后您可以像这样将Storyboard加载到ViewController中:
class MyViewController: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
guard let vc = UIStoryboard.load(.second) as? SecondViewController else {return}
self.present(vc, animated: true, completion: nil)
}
}
创建新的Storyboard时,只需在StoryboardID上设置相同的名称,然后在枚举“ StoryBoardName ”中添加Storyboard名称。
您总是可以直接跳到根控制器:
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [storyboard instantiateInitialViewController];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:NULL];
以下扩展名将允许您加载Storyboard
和关联的UIViewController
。示例:如果您有一个UIViewController
名为ModalAlertViewController
和名为“ ModalAlert”的情节提要,例如
let vc: ModalAlertViewController = UIViewController.loadStoryboard("ModalAlert")
将同时加载Storyboard
和UIViewController
和vc
将为类型ModalAlertViewController
。注意假定情节提要的情节提要ID与情节提要具有相同的名称,并且该情节提要被标记为Is Initial View Controller。
extension UIViewController {
/// Loads a `UIViewController` of type `T` with storyboard. Assumes that the storyboards Storyboard ID has the same name as the storyboard and that the storyboard has been marked as Is Initial View Controller.
/// - Parameter storyboardName: Name of the storyboard without .xib/nib suffix.
static func loadStoryboard<T: UIViewController>(_ storyboardName: String) -> T? {
let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
if let vc = storyboard.instantiateViewController(withIdentifier: storyboardName) as? T {
vc.loadViewIfNeeded() // ensures vc.view is loaded before returning
return vc
}
return nil
}
}
[sb instantiateInitialViewController]
如果要在场景的默认视图控制器上启动,则非常方便。