如何使用Swift协议功能,例如Android接口侦听器实现?

时间:2019-01-11 05:48:26

标签: swift protocols snapkit

我是韩国的android开发人员,是Swift的新手。 我正在将Android应用程序迁移到ios,但遇到界面和监听器问题。我不知道如何实现侦听器以在自定义视图和视图控制器之间进行通信。

我有一个自定义视图(即MyView),它有两个按钮,每个按钮都有自己的功能。 在Android(带有Java)中,我通常在MyView中创建一个侦听器接口,并在其中分配两个函数,例如void func1(String val1)和func2 ...

public class MyView extends RelativeLayout {
private Button b1, b2;
private String val1, val2;

public interface OnButtonListener {
    void onFunc1(String val1);

    void onFunc2(String val2);
}

private OnButtonListener onButtonListener;

public void setOnButtonListener( OnButtonListener onButtonListener ) {
    this.onButtonListener = onButtonListener;
}

  public MyView( Context context, AttributeSet attrs ) {
    super( context, attrs );
    b1.setOnClickListener( view -> {
        if (onButtonListener != null){
            onButtonListener.onFunc1( val1 );
        }
    } );
    b2.setOnClickListener( view -> {
        if (onButtonListener != null){
            onButtonListener.onFunc1( val2 );
        }
    } );
}
}

 public class MyActivity extends Activity {
    MyView myView1, myView2;

@Override
protected void onCreate( @Nullable Bundle savedInstanceState ) {
    super.onCreate( savedInstanceState );
    myView1.setOnButtonListener( new MyView.OnButtonListener() {
        @Override
        public void onFunc1( String val1 ) {
            Log.d(TAG, val1);
        }

        @Override
        public void onFunc2( String val2 ) {
            Log.d(TAG, val2);
        }
    } );
    myView2.setOnButtonListener( new MyView.OnButtonListener() {
        @Override
        public void onFunc1( String val1 ) {
            // do something1
        }

        @Override
        public void onFunc2( String val2 ) {
            // do something2
        }
    } );
}
}

此代码可以按照我的要求完美运行。因此,我尝试将相同的模式应用于Swift,但是找不到任何方法。

下面是快速4

import Foundation
import UIKit
import SnapKit

protocol OnButtonListener {
    func onButton1( _ val1: String )
     func onButton2( _ val2: String )
}

class MyView: UIView {
var onButtonListener: OnButtonListener?
var val1 = "abc"
var val2 = "123"

override init( frame: CGRect ) {
    super.init( frame: frame )
    let b1 = UIButton()
    let b2 = UIButton() // i'm using snapkit
    b1.addTarget(self, action: #selector(onB1), for: .touchUpInside)
    b2.addTarget(self, action: #selector(onB2), for: .touchUpInside)
}

@objc func onB1() {
    if onButtonListener != nil {
        onButtonListener!.onButton1(val1 )
    }
}

@objc func onB2() {
    if onButtonListener != nil {
        onButtonListener!.onButton2(val2 )
    }
}
}

class MyVC : UIViewController {
   override func viewDidLoad() {
    super.viewDidLoad()
    let myView1 = MyView()
    let myView2 = MyView()

    myView1.onButtonListener = {
        // ???
    }
    myView2.onButtonListener = {
        // ???
    }
}
}

我不知道如何在ViewContorller中实现侦听器。我尝试过的方法与Kotlin相同,但我也没有工作。谢谢您的阅读。

2 个答案:

答案 0 :(得分:3)

您必须在viewcontroller中设置委托并在viewcontroller中实现协议方法

class MyVC : UIViewController, OnButtonListener {
    override func viewDidLoad() {
         super.viewDidLoad()
         let myView1 = MyView()
           myView1. onButtonListener = self
         let myView2 = MyView()
           myView2. onButtonListener = self


      }

   func onButton1( _ val1: String ) {
         print(val1)
   }

   func onButton2( _ val2: String ) {
          print(val2)
   }


}

**方法2:**您也可以使用块

class MyView: UIView { 
   var buttonAction : ((_ value : String) -> Void)? = nil
  //.... your code 

  @objc func onB1() {
       if let action = buttonAction {
           action("abc")
        }
  }

  @objc func onB2() {
    if let action = buttonAction {
           action("xyz")
        }
  }

}

在您的ViewController中

  override func viewDidLoad() {
     super.viewDidLoad()
     let myView1 = MyView()
       myView1.buttonAction = { value in 
         print(value)
       } 
  }

答案 1 :(得分:2)

如下更新视图控制器代码:

首先确认您与UIViewController的OnButtonListener协议,然后在视图控制器中实现协议方法。

class MyVC : UIViewController, OnButtonListener {
    override func viewDidLoad() {
        super.viewDidLoad()

        let myView1 = MyView()

        // Confirm protocol implementation in current view controller
        myView1.onButtonListener = self 

        let myView2 = MyView()

        // Confirm protocol implementation in current view controller
        myView2.onButtonListener = self
    }

    func onButton1( _ val1: String) {
        // your code
    }

    func onButton2( _ val2: String) {
        // your code
    }
}