此处使用协议的新手。
我有一组使用名为mainNavBarItems的协议的viewcontrollers。
我想要以下内容,但下面的代码会遇到一系列错误:
protocol mainNavBarItems {
var rootView:Bool {get set}
func gotoRootProfileView()
init()
init(rootView:Bool)
}
extension mainNavBarItems{
// how do i set the default to be true?
init(rootView: Bool) {
self.init()
self.rootView = rootView
}
func addMainNavBarItems(){
let s = self as! UIViewController
// ERROR: using a string, but not sure how to define a selector in this case
s.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: s, action: "gotoRootGlobalView")
}
}
class ProfileViewController: UIViewController, mainNavBarItems{
func gotoRootProfileView() {
// does stuff
}
}
// How do i set the boolean for rootview here?
let vc:ProfileViewController = UIStoryboard.getController("Profile", vc: "profileVC")
// ERROR: profileviewcontroller does not have a rootview
vc.rootView = true
AppDelegate.rootGotoWithMainNav(vc)
答案 0 :(得分:0)
您的ProfileViewController
没有rootView
协议的mainNavBarItems
属性。
class ProfileViewController: UIViewController, mainNavBarItems{
var rootView = false // this is default
func gotoRootProfileView() {
// does stuff
}
}
...和协议应该有大写的名称。