如何在swift中以编程方式引用我的应用程序中的主要故事板?

时间:2021-01-11 02:46:18

How would I reference the main storyboard in my app programmatically in swift? I looked into the app delegate for a reference but so far I haven't found one.

如何在swift中以编程方式引用我的应用程序中的主要故事板?我查看了应用程序代表的参考,但到目前为止我还没有找到一个。

5 个解决方案

#1


16  

Oh whoops I found the answer...

哦,哎呀我找到了答案......

In another view controller of the that is connected to a storyboard you can simply use:

在另一个视图中,控制器连接到故事板,您只需使用:

self.storyboard?

#2


14  

Or any object can get a storyboard by referencing its name and bundle:

或者任何对象都可以通过引用其名称和包来获取故事板:

let storyboard = UIStoryboard(name: "storyboardNameHere", bundle: nil) //if bundle is nil the main bundle will be used

#3


8  

It's easy. When I've come across a similar problem, I wrote a class that can obtain any resources from main bundle.

这很容易。当我遇到类似的问题时,我编写了一个可以从主bundle获取任何资源的类。

//Generate name of the main storyboard file, by default: "Main"
var kMainStoryboardName: String {
    let info = NSBundle.mainBundle().infoDictionary!

    if let value = info["TPMainStoryboardName"] as? String
    {
        return value
    }else{
        return "Main"
    }
}

public class TPBundleResources
{
    class func nib(name: String) -> UINib?
    {
        let nib = UINib(nibName: name, bundle: NSBundle.mainBundle());
        return nib
    }

    //Main storybord
    class func mainStoryboard() -> UIStoryboard
    {
        return storyboard(kMainStoryboardName)
    }

    class func storyboard(name: String) -> UIStoryboard
    {
        let storyboard = UIStoryboard(name: name, bundle: NSBundle.mainBundle())
        return storyboard
    }

    //Obtain file from main bundle by name and fileType
    class func fileFromBundle(fileName: String?, fileType: String?) -> NSURL?
    {
        var url: NSURL?

        if let path = NSBundle.mainBundle().pathForResource(fileName, ofType: fileType)
        {
            url = NSURL.fileURLWithPath(path)
        }

        return url
    }

    class func plistValue(key:String) -> AnyObject?
    {
        let info = NSBundle.mainBundle().infoDictionary!

        if let value: AnyObject = info[key]
        {
            return value
        }else{
            return nil
        }
    }
}


public extension TPBundleResources
{
    //Obtain view controller by name from main storyboard
    class func vcWithName(name: String) -> UIViewController?
    {
        let storyboard = mainStoryboard()
        let viewController: AnyObject! = storyboard.instantiateViewControllerWithIdentifier(name)
        return viewController as? UIViewController
    }

    class func vcWithName(storyboardName:String, name: String) -> UIViewController?
    {
        let sb = storyboard(storyboardName)
        let viewController: AnyObject! = sb.instantiateViewControllerWithIdentifier(name)
        return viewController as? UIViewController
    }

    //Obtain view controller by idx from nib
    class func viewFromNib(nibName: String, atIdx idx:Int) -> UIView?
    {
        let view =  NSBundle.mainBundle().loadNibNamed(nibName, owner: nil, options: nil)[idx] as! UIView
        return view
    }

    class func viewFromNib(nibName: String, owner: AnyObject, atIdx idx:Int) -> UIView?
    {
        let bundle = NSBundle(forClass: owner.dynamicType)
        let nib = UINib(nibName: nibName, bundle: bundle)
        let view = nib.instantiateWithOwner(owner, options: nil)[idx] as? UIView
        return view
    }

    class func viewFromNibV2(nibName: String, owner: AnyObject, atIdx idx:Int) -> UIView?
    {
        let view =  NSBundle.mainBundle().loadNibNamed(nibName, owner: owner, options: nil)[idx] as! UIView
        return view
    }
}

Here are simple examples:

这是一个简单的例子:

//Get a main storyboard
TPBundleResources.mainStoryboard()

//Get view controller form main storyboard
TPBundleResources.vcWithName("MyViewController")

//Get view from MyView.nib at index 0
TPBundleResources.viewFromNib("MyView", atIdx: 0)

//Get plist value by key
TPBundleResources.plistValue("key")

#4


5  

Even if @ManOfPanda's answer is correct, there are cases where you simply don't have a reference to a UIViewController, so you can grab it from the rootViewController of the UIWindow object from your AppDelegate.

即使@ManOfPanda的答案是正确的,也有些情况下你根本没有对UIViewController的引用,所以你可以从AppDelegate的UIWindow对象的rootViewController中获取它。

// First import your AppDelegate
import AppDelegate

// ...

// Then get a reference of it.
let appDelegate = UIApplication().shared.delegate as! AppDelegate

// From there, get your UIStoryboard reference from the
// rootViewController in your UIWindow
let rootViewController = appDelegate.window?.rootViewController
let storyboard = rootViewController?.storyboard

You could also, of course, simply create a UIStoryboard by using (as @Mario suggested):

当然,您也可以使用(如@Mario建议的那样)创建UIStoryboard:

let storyboard = UIStoryboard(name: "storyboard", bundle:nil)

But that will, according to Apple documentation, create a new instance of the Storyboard (even if you already have one working). I always prefer to use an existing instance.

但是,根据Apple文档,这将创建一个新的Storyboard实例(即使你已经有一个工作)。我总是喜欢使用现有的实例。

init(name:bundle:)

Creates and returns a storyboard object for the specified storyboard resource file.

为指定的故事板资源文件创建并返回Storyboard对象。

init(name: String, bundle storyboardBundleOrNil: Bundle?)

init(name:String,bundle storyboardBundleOrNil:Bundle?)

Parameters

参数

  • name: The name of the storyboard resource file without the filename extension. This method raises an exception if this parameter is nil.
  • name:没有文件扩展名的storyboard资源文件的名称。如果此参数为nil,则此方法引发异常。
  • storyboardBundleOrNil: The bundle containing the storyboard file and its related resources. If you specify nil, this method looks in the main bundle of the current application.
  • storyboardBundleOrNil:包含故事板文件及其相关资源的包。如果指定nil,则此方法将查找当前应用程序的主包。

Source: Apple documentation

来源:Apple文档

#5


0  

UIStoryboard * mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

#1


16  

Oh whoops I found the answer...

哦,哎呀我找到了答案......

In another view controller of the that is connected to a storyboard you can simply use:

在另一个视图中,控制器连接到故事板,您只需使用:

self.storyboard?

#2


14  

Or any object can get a storyboard by referencing its name and bundle:

或者任何对象都可以通过引用其名称和包来获取故事板:

let storyboard = UIStoryboard(name: "storyboardNameHere", bundle: nil) //if bundle is nil the main bundle will be used

#3


8  

It's easy. When I've come across a similar problem, I wrote a class that can obtain any resources from main bundle.

这很容易。当我遇到类似的问题时,我编写了一个可以从主bundle获取任何资源的类。

//Generate name of the main storyboard file, by default: "Main"
var kMainStoryboardName: String {
    let info = NSBundle.mainBundle().infoDictionary!

    if let value = info["TPMainStoryboardName"] as? String
    {
        return value
    }else{
        return "Main"
    }
}

public class TPBundleResources
{
    class func nib(name: String) -> UINib?
    {
        let nib = UINib(nibName: name, bundle: NSBundle.mainBundle());
        return nib
    }

    //Main storybord
    class func mainStoryboard() -> UIStoryboard
    {
        return storyboard(kMainStoryboardName)
    }

    class func storyboard(name: String) -> UIStoryboard
    {
        let storyboard = UIStoryboard(name: name, bundle: NSBundle.mainBundle())
        return storyboard
    }

    //Obtain file from main bundle by name and fileType
    class func fileFromBundle(fileName: String?, fileType: String?) -> NSURL?
    {
        var url: NSURL?

        if let path = NSBundle.mainBundle().pathForResource(fileName, ofType: fileType)
        {
            url = NSURL.fileURLWithPath(path)
        }

        return url
    }

    class func plistValue(key:String) -> AnyObject?
    {
        let info = NSBundle.mainBundle().infoDictionary!

        if let value: AnyObject = info[key]
        {
            return value
        }else{
            return nil
        }
    }
}


public extension TPBundleResources
{
    //Obtain view controller by name from main storyboard
    class func vcWithName(name: String) -> UIViewController?
    {
        let storyboard = mainStoryboard()
        let viewController: AnyObject! = storyboard.instantiateViewControllerWithIdentifier(name)
        return viewController as? UIViewController
    }

    class func vcWithName(storyboardName:String, name: String) -> UIViewController?
    {
        let sb = storyboard(storyboardName)
        let viewController: AnyObject! = sb.instantiateViewControllerWithIdentifier(name)
        return viewController as? UIViewController
    }

    //Obtain view controller by idx from nib
    class func viewFromNib(nibName: String, atIdx idx:Int) -> UIView?
    {
        let view =  NSBundle.mainBundle().loadNibNamed(nibName, owner: nil, options: nil)[idx] as! UIView
        return view
    }

    class func viewFromNib(nibName: String, owner: AnyObject, atIdx idx:Int) -> UIView?
    {
        let bundle = NSBundle(forClass: owner.dynamicType)
        let nib = UINib(nibName: nibName, bundle: bundle)
        let view = nib.instantiateWithOwner(owner, options: nil)[idx] as? UIView
        return view
    }

    class func viewFromNibV2(nibName: String, owner: AnyObject, atIdx idx:Int) -> UIView?
    {
        let view =  NSBundle.mainBundle().loadNibNamed(nibName, owner: owner, options: nil)[idx] as! UIView
        return view
    }
}

Here are simple examples:

这是一个简单的例子:

//Get a main storyboard
TPBundleResources.mainStoryboard()

//Get view controller form main storyboard
TPBundleResources.vcWithName("MyViewController")

//Get view from MyView.nib at index 0
TPBundleResources.viewFromNib("MyView", atIdx: 0)

//Get plist value by key
TPBundleResources.plistValue("key")

#4


5  

Even if @ManOfPanda's answer is correct, there are cases where you simply don't have a reference to a UIViewController, so you can grab it from the rootViewController of the UIWindow object from your AppDelegate.

即使@ManOfPanda的答案是正确的,也有些情况下你根本没有对UIViewController的引用,所以你可以从AppDelegate的UIWindow对象的rootViewController中获取它。

// First import your AppDelegate
import AppDelegate

// ...

// Then get a reference of it.
let appDelegate = UIApplication().shared.delegate as! AppDelegate

// From there, get your UIStoryboard reference from the
// rootViewController in your UIWindow
let rootViewController = appDelegate.window?.rootViewController
let storyboard = rootViewController?.storyboard

You could also, of course, simply create a UIStoryboard by using (as @Mario suggested):

当然,您也可以使用(如@Mario建议的那样)创建UIStoryboard:

let storyboard = UIStoryboard(name: "storyboard", bundle:nil)

But that will, according to Apple documentation, create a new instance of the Storyboard (even if you already have one working). I always prefer to use an existing instance.

但是,根据Apple文档,这将创建一个新的Storyboard实例(即使你已经有一个工作)。我总是喜欢使用现有的实例。

init(name:bundle:)

Creates and returns a storyboard object for the specified storyboard resource file.

为指定的故事板资源文件创建并返回Storyboard对象。

init(name: String, bundle storyboardBundleOrNil: Bundle?)

init(name:String,bundle storyboardBundleOrNil:Bundle?)

Parameters

参数

  • name: The name of the storyboard resource file without the filename extension. This method raises an exception if this parameter is nil.
  • name:没有文件扩展名的storyboard资源文件的名称。如果此参数为nil,则此方法引发异常。
  • storyboardBundleOrNil: The bundle containing the storyboard file and its related resources. If you specify nil, this method looks in the main bundle of the current application.
  • storyboardBundleOrNil:包含故事板文件及其相关资源的包。如果指定nil,则此方法将查找当前应用程序的主包。

Source: Apple documentation

来源:Apple文档

#5


0  

UIStoryboard * mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];