2015-09-14 15 views
1

我有一個自定義uiview(稱爲navBarMenu)與五個按鈕作爲子視圖。我將這個navBarMenu添加到導航欄的titleView中。 navBarMenu有它自己的類和nib文件,其中按鈕被定義爲iboutlets。我如何添加目標,以便當我點擊navBarMenu中的按鈕時,它會在我的viewcontroller中觸發一個嵌入在導航控制器中的函數? 使用方法:navBarMenu.Button1.addTarget(self, action: "Button1Tapped:", forControlEvents: UIControlEvents.TouchUpInside)不起作用,我得到一個錯誤,它意外地發現零,同時展開一個可選值。NavigationController titleView中的自定義UIView。我怎樣才能讓按鈕在我的視圖控制器觸發動作

編輯:這是我的視圖控制器和我的自定義視圖的代碼。

class MyCustomView: UIView { 

var view: UIView! 
var nibName: String = "MyCustomView" 

@IBOutlet weak var Button1: UIButton! 
@IBOutlet weak var Button2: UIButton! 
@IBOutlet weak var Button3: UIButton! 
@IBOutlet weak var Button4: UIButton! 
@IBOutlet weak var Button5: UIButton! 

var Button1Text: String? { 
    get { 
     return Button1.titleLabel?.text 
    } 
    set(Button1Text) { 
     Button1.setTitle(Button1Text, forState: UIControlState.Normal) 
    } 
} 

var Button2Text: String? { 
    get { 
     return Button2.titleLabel?.text 
    } 
    set(Button2Text) { 
     Button2.setTitle(Button2Text, forState: UIControlState.Normal) 
    } 
} 

var Button3Text: String? { 
    get { 
     return Button3.titleLabel?.text 
    } 
    set(Button3Text) { 
     Button3.setTitle(Button3Text, forState: UIControlState.Normal) 
    } 
} 

var Button4Text: String? { 
    get { 
     return Button4.titleLabel?.text 
    } 
    set(Button4Text) { 
     Button4.setTitle(ButtonText4, forState: UIControlState.Normal) 
    } 
} 

var Button5Text: String? { 
    get { 
     return Button5.titleLabel?.text 
    } 
    set(Button5Text) { 
     Button5.setTitle(ButtonText5, forState: UIControlState.Normal) 
    } 
} 

override init(frame: CGRect) { 
    super.init(frame: frame) 

    setup() 
    println("view is setup") 
} 
convenience init(controller: UIViewController) { 

    var frameForNavBar: CGRect = CGRect(x: 0.0, y: 0.0, width: 548.0, height: 33.0) 
    self.init(frame: frameForNavBar) 


    Button1.addTarget(controller, action: "Button1Tapped:", forControlEvents: UIControlEvents.TouchUpInside) 
    Button2.addTarget(controller, action: "Button2Tapped:", forControlEvents: UIControlEvents.TouchUpInside) 
    Button3.addTarget(controller, action: "Button3Tapped:", forControlEvents: UIControlEvents.TouchUpInside) 
    Button4.addTarget(controller, action: "Button4Tapped:", forControlEvents: UIControlEvents.TouchUpInside) 
    Button5.addTarget(controller, action: "Button5Tapped:", forControlEvents: UIControlEvents.TouchUpInside) 
} 

required init(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
} 

func setup() { 
    view = loadViewFromNib() 
    view.frame = bounds 
    addSubview(view) 
} 

func loadViewFromNib() -> UIView { 
    let bundle = NSBundle(forClass: self.dynamicType) 
    let nib = UINib(nibName: nibName, bundle: bundle) 
    let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView 

    return view 
} 

}

class MyViewController: UIViewController, UINavigationBarDelegate { 

@IBOutlet weak var webView: UIWebView! 

func Button1Tapped(sender: AnyObject) { 
    println(__FUNCTION__) 
} 
func Button2Tapped(sender: AnyObject) { 
    println(__FUNCTION__) 
} 
func Button3Tapped(sender: AnyObject) { 
    println(__FUNCTION__) 
} 
func Button4Tapped(sender: AnyObject) { 
    println(__FUNCTION__) 
} 
func Button5Tapped(sender: AnyObject) { 
    println(__FUNCTION__) 
} 

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) { 

    super.init(nibName: "MyViewController", bundle: nil) 

} 
convenience init() { 
    self.init(nibName: "MyViewController", bundle: nil) 
    tabBarItem.title = "MyVC" 
    tabBarItem.image = UIImage(named: "MyImage") 
} 
required init(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
} 
override func viewDidLoad() { 
    super.viewDidLoad() 

    let navBarMenu = MyCustomView(controller: self) 
    positionForBar(navigationController!.navigationBar) 
    self.navigationItem.titleView = navBarMenu 
    self.navigationController?.navigationBar.translucent = false 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

func loadWebPage() { 

} 

func positionForBar(bar: UIBarPositioning) -> UIBarPosition { 
    return UIBarPosition.TopAttached 
} 

}

回答

2

當你初始化customView你可以傳遞的UIViewController參數,然後添加目標的按鈕即可。

let myCustomView = MyCustomViewClass(self) 

而在你MyCustomViewClass你可以使用:

init (controller: UIViewController) { 
    // use controller, not self 
    button.addTarget(controller, action: "Button1Tapped:", forControlEvents: UIControlEvents.TouchUpInside) 
} 
+0

我一直試圖讓這個方法一會兒工作,但我仍然得到錯誤「致命錯誤:意外發現零同時展開一個可選值「當它到達第一個」button.addTarget「 – Frodgers

+0

你必須顯示整個代碼才能更好地理解。 – mikle94

+0

只需在編輯中添加代碼即可。感謝您的幫助。 – Frodgers

相關問題