从自定义UIBarButtonItem中发现

时间:2015-11-10 10:20:17

标签: ios swift

我在其中使用UIBarButtonItemUIView创建了自定义UIImageView。以下是用于创建和UIBarButtonItem导航栏

的代码
    let containView = UIView(frame: CGRectMake(0, 0, 70, 40))

    let imageview = UIImageView(frame: CGRectMake(45, 5, 27, 27))
    imageview.image = UIImage(named: "giftbox-icon")
    imageview.contentMode = UIViewContentMode.ScaleAspectFill
    containView.addSubview(imageview)

    let circleImageview = UIImageView(frame: CGRectMake(61, 1, 17, 17))
    circleImageview.image = UIImage(named: "red-circle")
    circleImageview.contentMode = UIViewContentMode.ScaleAspectFill
    containView.addSubview(circleImageview)

    let label = UILabel(frame: CGRectMake(60, 1, 20, 15))
    label.text = String(cartQty)
    label.textAlignment = NSTextAlignment.Center
    label.textColor = UIColor.whiteColor()
    label.font = label.font.fontWithSize(11)
    containView.addSubview(label)

    self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: containView)

这使它看起来像这样

enter image description here

现在我想要,当用户点击这个UIBarButtonItem时,应该发生一个segue到另一个视图控制器。如何使用Swift以编程方式将动作与此相关联?

4 个答案:

答案 0 :(得分:1)

在Swift中创建UIBarButtonItem的扩展名,如下所示。

import UIKit

extension UIBarButtonItem {
    func addTargetWithAct(target: AnyObject, action: Selector) {
        self.target = target
        self.action = action
    }
}

并更改以下代码

self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: containView)

var barbuttonItem = UIBarButtonItem(customView: containView);
barbuttonItem.addTargetForAction(self, action:    Selector("mySelectorName"))
self.navigationItem.rightBarButtonItem = barbuttonItem

其中“mySelectorName”是您的操作方法。

我没有原谅这个。检查并修复任何编译问题。

更新: -   根据苹果文档,上述解决方案可能无效。   请改用以下解决方案,

UIButton *btn =  [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, 70, 40)
[btn addTarget:self action:@selector(mySelectorName) forControlEvents:UIControlEventTouchUpInside];
containView.addSubview(btn)

答案 1 :(得分:1)

替换UIButton而不是UIView。喜欢

let containView = UIButton(type: UIButtonType.Custom)
containView.frame = CGRectMake(0, 0, 70, 40)
containView.addTarget(self, action: "yourButtonEvent", forControlEvents: UIControlEvents.TouchUpInside)
//...........
// ........ Add all your content

并实现方法

func yourButtonEvent() {

    self.performSegueWithIdentifier("YourDestControllerIdentifier", sender: nil)
}

答案 2 :(得分:0)

当你有UIBarButtonItem的自定义视图时,在containsView上实现一个轻击手势,并在方法结尾处放置一个轻击动作/方法

IPython 3.2.1
如果需要在第二个视图控制器上初始化任何属性,

覆盖下面的方法(注意:导入第二个视图控制器头文件)

performSegueWithIdentifier("mySegueID", sender: nil) // replace segue id from storyboard

答案 3 :(得分:-1)

在您的操作中,只需使用 Sub Button1_Click() Dim RangeArea As Range Dim ws As Worksheet, sh As Worksheet Set ws = Sheets("Input") Set sh = Sheets("Output") Application.DisplayAlerts = 0 Application.ScreenUpdating = 0 With ws .Columns("A:A").TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _ TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _ Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _ :=":", FieldInfo:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True .Range(.Range("A1"), .Range("A1").End(xlDown)).Copy sh.Range("A1").PasteSpecial xlPasteValues, Transpose:=True For Each RangeArea In .Columns("A").SpecialCells(xlCellTypeConstants, 23).Areas RangeArea.Offset(, 1).Copy sh.Cells(sh.Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues, Transpose:=True Next RangeArea End With Application.CutCopyMode = 0 End Sub

即可