NavigationController titleView 中的自定义 UIView。如何让按钮在我的视图控制器中触发操作

Custom UIView in NavigationController titleView. How can I have the buttons trigger actions in my View Controller

我有一个带有五个按钮作为子视图的自定义 uiview(称之为 navBarMenu)。我将此 navBarMenu 添加到导航栏的 titleView。 navBarMenu 有自己的 class 和 nib 文件,其中按钮被定义为 iboutlets。我如何添加目标,以便当我单击 navBarMenu 中的按钮时,它会触发我的 viewcontroller 中嵌入导航控制器的函数? 使用方法:navBarMenu.Button1.addTarget(self, action: "Button1Tapped:", forControlEvents: UIControlEvents.TouchUpInside) 不起作用,我得到一个错误,它在展开可选值时意外发现 nil。

编辑:这是我的视图控制器和自定义视图的代码。

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
}

}

当您初始化 customView 时,您可以传递 UIViewController 参数,然后为您的按钮添加目标。

let myCustomView = MyCustomViewClass(self)

在您的 MyCustomViewClass 中,您可以使用:

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