2015-09-28 71 views
4

更新到Xcode 7後,出現此錯誤「類型爲UIViewController的值沒有成員topViewController。其他人遇到這個? topViewController不再是UIViewController的一部分嗎?Xcode 7更新後類型UIViewController的值沒有成員topViewController

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.navigationItem.leftBarButtonItem = self.editButtonItem() 
    let addButton = UIBarButtonItem(barButtonSystemItem: .Search, 
     target: self, action: "addButtonPressed:") 
    self.navigationItem.rightBarButtonItem = addButton 

    if let split = self.splitViewController { 
     let controllers = split.viewControllers 
     self.detailViewController = 
      controllers[controllers.count-1].topViewController as? ### Error heres 
     DetailViewController 
    } 

    model = Model(delegate: self) // create the Model 
    model.synchronize() // tell model to sync its data 
    self.navigationController?.toolbarHidden = false; 
    self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"Home", style:.Plain, target:nil, action:nil) 
} 

回答

4

你必須讓它明確,以便它知道你正試圖從UINavigationController得到topViewController斯威夫特2:

self.detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController 
+1

燁,我最終計算出來。我把這個:如果讓split = self.splitViewController讓controllers = split.viewControllers self.detailViewController = (controllers [controllers.count-1] as!UINavigationController).topViewController as? DetailViewController } –

+1

'as? DetailViewController'的鑄造非常重要。謝謝。 – tymac

+0

感謝tymac指出這一點;) – Marin

相關問題