ios 如何从类中以编程方式加载故事板?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9896406/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:33:40  来源:igfitidea点击:

How can I load storyboard programmatically from class?

iosobjective-cxcodestoryboardxib

提问by kokoko

My problem is that I was looking for way to use both storyboardand xib. But I can't find proper way to load and show storyboard programmatically. Project was started developing with xib, and now it's very hard to nest all xib files in storyboard. So I was looking a way to do it in code, like with alloc, init, pushfor viewControllers. In my case I have only one controller in storyboard: UITableViewController, which has static cells with some content I want to show. If anyone knows proper way to work both with xib and storyboard without huge refactoring, I will appreciate for any help.

我的问题是我正在寻找同时使用storyboardxib 的方法。但是我找不到以编程方式加载和显示故事板的正确方法。项目开始使用xib开发,现在很难将所有xib文件嵌套在storyboard中。所以我正在寻找一种在代码中做到这一点的方法,比如alloc, init, push用于 viewControllers。在我的例子中,我在 storyboard: 中只有一个控制器UITableViewController,它有一些我想展示的内容的静态单元格。如果有人知道在没有大量重构的情况下使用 xib 和故事板的正确方法,我将不胜感激。

回答by James Beith

In your storyboard go to the Attributes inspector and set the view controller's Identifier. You can then present that view controller using the following code.

在您的故事板中,转到属性检查器并设置视图控制器的标识符。然后,您可以使用以下代码呈现该视图控制器。

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"myViewController"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:NULL];

回答by SwiftArchitect

Swift 3

斯威夫特 3

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "viewController")
self.navigationController!.pushViewController(vc, animated: true)

Swift 2

斯威夫特 2

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("viewController")
self.navigationController!.pushViewController(vc, animated: true)

Prerequisite

先决条件

Assign a Storyboard IDto your view controller.

为您的视图控制器分配一个Storyboard ID

Storyboard ID

故事板 ID

IB > Show the Identity inspector > Identity > Storyboard ID

IB > 显示身份检查器 > 身份 > 故事板 ID

Swift (legacy)

斯威夫特(遗留)

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("viewController") as? UIViewController
self.navigationController!.pushViewController(vc!, animated: true)

Edit: Swift 2 suggested in a comment by Fred A.

编辑:Fred A在评论中建议使用 Swift 2

if you want to use without any navigationController you have to use like following :

如果你想在没有任何 navigationController 的情况下使用,你必须使用如下:

    let Storyboard  = UIStoryboard(name: "Main", bundle: nil)
    let vc = Storyboard.instantiateViewController(withIdentifier: "viewController")
    present(vc , animated: true , completion: nil)

回答by chaithraVeeresh

In attribute inspector give the identifier for that view controller and the below code works for me

在属性检查器中给出该视图控制器的标识符,下面的代码对我有用

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
DetailViewController *detailViewController = [storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
[self.navigationController pushViewController:detailViewController animated:YES];

回答by Adetiloye Philip Kehinde

Try this

尝试这个

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"Login"];

[[UIApplication sharedApplication].keyWindow setRootViewController:vc];

回答by Tarik

in swift
NavigationControllerand pushControlleryou can replace for

在 swift
NavigationControllerpushController 中,您可以替换为

present(vc, animated:true , completion: nil)

回答by gandhi Mena

For swift 3 and 4, you can do this. Good practice is set name of Storyboard equal to StoryboardID.

对于swift 3 和 4,你可以这样做。好的做法是将 Storyboard 的名称设置为等于 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)
   }
}

and then you can load your Storyboard in your ViewController like this:

然后你可以像这样在你的 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)
     }

}

}

When you create a new Storyboard just set the same name on StoryboardID and add Storyboard name in your enum "StoryBoardName"

当您创建一个新的 Storyboard 时,只需在 StoryboardID 上设置相同的名称并在您的枚举“ StoryBoardName”中添加 Storyboard 名称

回答by user938797

You can always jump right to the root controller:

你总是可以直接跳转到根控制器:

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [storyboard instantiateInitialViewController];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:NULL];

回答by Norman

The extension below will allow you to load a Storyboardand it's associated UIViewController. Example: If you have a UIViewControllernamed ModalAlertViewControllerand a storyboard named "ModalAlert" e.g.

下面的扩展将允许您加载一个Storyboard和它相关联的UIViewController. 示例:如果您有一个名为“ModalAlert”的UIViewController命名ModalAlertViewController和故事板,例如

let vc: ModalAlertViewController = UIViewController.loadStoryboard("ModalAlert")

Will load both the Storyboardand UIViewControllerand vcwill be of type ModalAlertViewController. NoteAssumes that the storyboard's Storyboard ID has the same name as the storyboard and that the storyboard has been marked as Is Initial View Controller.

将加载StoryboardandUIViewControllervc类型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
    }
}