这是将数据传递到自定义视图然后执行功能的好方法吗?

时间:2019-04-13 10:47:58

标签: ios swift swift-protocols

我创建了一个自定义输入附件视图,它是“提交”按钮。 但是,我需要将数据传递到自定义视图,然后执行其他功能。这是个好方法吗?

class SignUpViewController: UIViewController {

    @IBOutlet weak var phoneTF: SignLogTextField!
    @IBOutlet weak var EmailTF: SignLogTextField!
    @IBOutlet weak var PasswordTF: SignLogTextField!
    @IBOutlet weak var FBBtn: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        textFieldPreparation()
    }

    func textFieldPreparation(){
        EmailTF.inputAccessoryView = Bundle.main.loadNibNamed("SignSubmitBTN", owner: self, options: nil)?.first as! SignSubmitBTN

        phoneTF.inputAccessoryView = Bundle.main.loadNibNamed("SignSubmitBTN", owner: self, options: nil)?.first as! SignSubmitBTN

        PasswordTF.inputAccessoryView = Bundle.main.loadNibNamed("SignSubmitBTN", owner: self, options: nil)?.first as! SignSubmitBTN
    }
}

enter image description here

我不确定如何将数据传递到自定义视图,还是应该在sign up中执行Outlet Action

这是我的自定义视图

import UIKit

class SignSubmitBTN: UIView {
    @IBAction func submitAction(_ sender: Any) {
    }

    @IBOutlet weak var subBTN: UIButton!

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

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

    func setup(){}
}

如果我必须将数据传递到自定义视图,应该使用协议吗?如果我应该使用该协议如何使用它?

3 个答案:

答案 0 :(得分:1)

好...

我认为您是从错误的方向进行操作。按钮的职责应该是告诉您用户已点击它,仅此而已。该按钮不应用于登录。

但是...您到这里的路已经90%。再增加几位。

您可以更新提交按钮以包括一个委托,并在按钮操作中使用该委托...

import UIKit

// protocol
protocol SignInButtonDelegate: class {
    func signIn()
}

class SignSubmitBTN: UIView {
    // property for delegate
    weak var delegate: SignInButtonDelegate?        

    @IBAction func submitAction(_ sender: Any) {
        // this tells the delegate to sign in
        // it doesn't need to know how that happens
        delegate?.signIn()
    }

    @IBOutlet weak var subBTN: UIButton!

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

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

    func setup() {}
}

然后在您的视图控制器中,您遵循委托协议...

extension SignUpViewController: SignInButtonDelegate {
    func signIn() {
        // here you already have access to all the data you need to sign in.
        // you are in the view controller here so just get the text from the username, password, etc...
    }
}

然后将视图控制器设置为委托...

func textFieldPreparation() {
    let signInButton = Bundle.main.loadNibNamed("SignSubmitBTN", owner: self, options: nil)?.first as! SignSubmitBTN
    signInButton.delegate = self

    // these are properties... they should begin with a lowercase letter
    emailTF.inputAccessoryView = signInButton 
    phoneTF.inputAccessoryView = signInButton 
    passwordTF.inputAccessoryView = signInButton 
}

答案 1 :(得分:1)

尝试

    func loadFromNib() -> SignSubmitBTN {


 let bundle  = Bundle.main.loadNibNamed("SignSubmitBTN", owner: self, options: nil)?.first as! SignSubmitBTN
                return bundle
            }

在您的viewcontroller调用中,如下所示:

    let customObj = loadFromNib()
    customObj.dataToGet = "Data to pass"
customObj.delegate = self
    EmailTF.inputAccessoryView = customObj

如果要从自定义类传递数据,则需要使用@Fogmeister建议的委托协议。

如果要委托选项

    public protocol menuOpen:  class {
        func openMenuAction(selectedValue : String)
    }
    class SignSubmitBTN: UIView {
    open var delegate:menuOpen?
 var dataToGet = ""

    @IBAction func submitAction(_ sender: Any) {

self.delegate.openMenuAction("test")

    }
    }

然后在VC中添加委托方法

class SignUpViewController: UIViewController,menuOpen{ 

    func openMenuAction(selectedValue : String) {
    //get your selected value here, you would better pass parameter in this method
    }
}

答案 2 :(得分:1)

您的CustomView最后只是一个类,因此您可以在面向对象的过程中进行此操作,为此,请在customView中编写一个函数以在其中传递数据。喜欢

   class SignSubmitBTN: UIView {
       var data: String!;
       public func setData(data: String) {
             self.data = data;
       }

       /// Other code
    }

要在初始化CustomView后设置数据,请调用setData(params)函数在其中设置数据。