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

问题描述 投票:0回答:3

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

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(){}
}

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

ios swift swift-protocols
3个回答
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
投票

您的自定义视图最后只是一个类,因此您可以在面向对象的范例中执行此操作,因为您可以在customView中编写一个函数来传递数据。喜欢

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

       /// Other code
    }

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


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
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.