我如何以编程方式在swift中引用我的应用程序中的主要故事板?我查看了应用代表作为参考,但到目前为止我还没找到。
答案 0 :(得分:18)
哦,哎呀,我找到了答案......
在另一个视图中,控制器连接到故事板,您只需使用:
self.storyboard?
答案 1 :(得分:15)
或者任何对象都可以通过引用其名称和包来获取故事板:
let storyboard = UIStoryboard(name: "storyboardNameHere", bundle: nil) //if bundle is nil the main bundle will be used
答案 2 :(得分:8)
这很容易。当我遇到类似的问题时,我写了一个可以从主包中获取任何资源的类。
//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
}
}
以下是一些简单的例子:
//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")
答案 3 :(得分:6)
即使@ ManOfPanda的回答是正确的,但有些情况下您根本没有引用UIViewController
,因此您可以从rootViewController
获取来自UIWindow
的{{1}}对象。
AppDelegate
当然,您也可以使用(如@Mario建议的那样)创建// 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
:
UIStoryboard
但是,根据Apple文档,这将创建一个新的Storyboard实例(即使你已经有一个工作)。我总是喜欢使用现有的实例。
的init(名称:束:)
为指定的故事板资源文件创建并返回storyboard对象。
let storyboard = UIStoryboard(name: "storyboard", bundle:nil)
<强>参数强>
init(name: String, bundle storyboardBundleOrNil: Bundle?)
:没有文件扩展名的storyboard资源文件的名称。如果此参数为nil,则此方法引发异常。name
:包含故事板文件及其相关资源的捆绑包。如果指定nil,则此方法将查找当前应用程序的主包。
答案 4 :(得分:0)
UIStoryboard * mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];