不能在Swift类中使用Selector的参数

时间:2017-12-28 23:50:12

标签: ios swift function

我实际上并没有收到错误,但是当按下按钮时按钮不会调用该函数。我创建了一个类来使我的按钮更容易,但选择器存在问题。现在所有这些代码都在我的viewController中。

class NavButton {
    var button: UIButton = UIButton(type: UIButtonType.custom)

    init(iconName: String, selector: Selector ){
        self.button.setImage(UIImage(named: iconName), for: UIControlState.normal)
        self.button.addTarget(self, action: selector, for: .touchUpInside)
        self.button.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
        self.button.imageView?.contentMode = UIViewContentMode.scaleAspectFit
        self.button.widthAnchor.constraint(equalToConstant: 20).isActive = true
        self.button.heightAnchor.constraint(equalToConstant: 20).isActive = true
    }

    func construct() -> UIButton {
        return self.button
    }

}


private func setupNavBar(){

    let searchButton = NavButton(iconName:"search_icon", selector: #selector(handleSearch))
    let moreButton = NavButton(iconName:"nav_more_icon", selector: #selector(handleMore))
    let searchBarButtonItem = UIBarButtonItem(customView: searchButton.construct())
    let moreBarButtonItem = UIBarButtonItem(customView: moreButton.construct())

    navigationItem.rightBarButtonItems = [
        moreBarButtonItem,
        searchBarButtonItem
    ]
}

@objc func handleSearch(){
    print("search click")
}

@objc func handleMore(){
    print("more click")
}

2 个答案:

答案 0 :(得分:2)

Lines <- "col1 col2 col3 col4 dog cat NA NA NA cat foo bar dog NA NA NA NA cat NA NA NA NA NA NA" DF <- read.table(text = Lines, header = TRUE, as.is = TRUE) 仅表示方法,但它们不表示调用方法的对象,即目标

在构造函数中,您将目标设置为Selector

self

这意味着按下按钮时会尝试调用self.button.addTarget(self, action: selector, for: .touchUpInside) ^^^^^ 类中的实例方法。但实际上你想在其他类中调用NavButton方法!

要解决此问题,请在构造函数中添加目标参数:

handleSearch

然后像这样调用构造函数:

init(iconName: String, target: Any?, selector: Selector ){
    self.button.setImage(UIImage(named: iconName), for: UIControlState.normal)
    // note the change to the below line
    self.button.addTarget(target, action: selector, for: .touchUpInside)
    self.button.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
    self.button.imageView?.contentMode = UIViewContentMode.scaleAspectFit
    self.button.widthAnchor.constraint(equalToConstant: 20).isActive = true
    self.button.heightAnchor.constraint(equalToConstant: 20).isActive = true
}

答案 1 :(得分:1)

将目标作为参数传递给NavButton类。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout    xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.balance.MainActivity">

<Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="Take Screenshot"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.055" />

<ImageView
    android:id="@+id/imageView"
    android:layout_width="355dp"
    android:layout_height="485dp"
    android:layout_marginTop="8dp"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@android:drawable/menuitem_background"
    tools:layout_editor_absoluteX="17dp" />


  </android.support.constraint.ConstraintLayout>    

目标应该是定义操作选择器的对象。