无法使用 xcode beta 6.3 将视图控制器推送到导航控制器
我正在尝试将视图控制器推送到导航控制器中.代码在 xcode 6.1 中似乎是正确的.但是当我将项目更改为 xcode beta6.3 时,xcode 要求我将 typecase 运算符更改为 as!.现在我无法将视图控制器推入导航控制器
i am trying to push view controller into navigation controller. The code seems right in xcode 6.1. But when i changed the project into xcode beta6.3 i was asked by xcode to change the typecase operator as to as!. Now i am not able to push view controller into navigation controller
//delegate method
func sendIndex(row : Int){
switch row {
case 0:
if(!isCurrentMoneyVc){
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let moneySummaryVC: MoneySummaryVC = storyboard.instantiateViewControllerWithIdentifier("moneyVC") as MoneySummaryVC
//self.navigationController?.pushViewController(moneySummaryVC, animated: true)
self.navigationController?.setViewControllers([moneySummaryVC], animated: true)
}else{
hideMenu()
}
case 1:
if(!isCurrentAboutVc){
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let moneySummaryVC1: AccountsVC = storyboard.instantiateViewControllerWithIdentifier("account") as AccountsVC
self.navigationController?.pushViewController(moneySummaryVC1, animated: true)
}else{
hideMenu()
}
case 2:
if(!isCurrentTransactionVc){
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let moneySummaryVC2: Transaction = storyboard.instantiateViewControllerWithIdentifier("transact") as Transaction
self.navigationController?.pushViewController(moneySummaryVC2, animated: true)
}else{
hideMenu()
}
default:
println("no index")
}
}
从技术上讲,如果在情节提要中找不到您的视图控制器,则它可能为零,这可能是 xcode 抱怨的原因.从情节提要中引用视图控制器并推送它的更好方法:
Technically your viewcontroller can be nil if not found in the storyboard, which is probably why xcode is complaining. A better approach to reference a viewcontroller from the storyboard and pushing it:
if let moneySummaryVC2 = storyboard.instantiateViewControllerWithIdentifier("transact") as? Transaction {
self.navigationController?.pushViewController(moneySummaryVC2, animated: true)
}
这里我们现在只在成功创建视图控制器常量 moneySummaryVC2 时才尝试推送视图控制器,这意味着在故事板中找到了视图控制器 ID.不要忘记处理未找到 viewcontroller 的情况(日志记录等).
Here we now only try and push the viewcontroller if the viewcontroller constant moneySummaryVC2 was successfully created, meaning the viewcontroller id was found in the storyboard. Don't forget to handle situations where the viewcontroller wasn't found (logging or something).