[英]How can you make use a variable as a file type in Swift
在下面的代碼中,如何在打開新的視圖控制器時使用變量controllerFileTypeIs動態選擇要使用的文件?
//MainViewController.swift
var variableForHomePage:HomeMenuViewController!
var variableForEntryViewController:EntryViewController!
var isHome:Bool = false
var controllerFileTypeIs:UIViewController?
var vcIdentifier:String = ""
override func viewDidLoad() {
super.viewDidLoad()
if isHome {
controllerFileTypeIs = variableForHomePage
vcIdentifier = "homeSB"
} else {
controllerFileTypeIs = variableForEntryViewController
vcIdentifier = "entrySB"
}
var vc = storyboard?.instantiateViewControllerWithIdentifier(vcIdentifier) as! controllerFileTypeIs
self.navigationController?.presentViewController(vc, animated: true, completion: nil)
// Do any additional setup after loading the view.
}
由於您已經為控制器設置了情節提要標識符,因此無需鍵入任何內容,只需按照以下方式即可。
let vc = storyboard?.instantiateViewControllerWithIdentifier(vcIdentifier!)
self.navigationController?.presentViewController(vc!, animated: true, completion: nil)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.