将数据传递给自定义视图然后执行函数是一种好方法吗?
Is it a good way to pass data to custom view then execute the function?
我创建了一个自定义输入附件视图,它是提交按钮。
但是,我需要将数据传递给自定义视图,然后执行进一步的功能。这是一个很好的方法吗?
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
}
}
我不确定如何将数据传递到自定义视图,或者我应该在 Outlet Action
中执行 sign up
吗?
这是我的自定义视图
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(){}
}
如果我必须将数据传递到自定义视图,我应该使用协议吗?如果我应该使用协议怎么用呢?
好的...
我认为你是从错误的方向来处理这个问题的。按钮的职责应该是告诉您用户点击了它,仅此而已。该按钮不应处理登录。
但是...你已经完成了这里的 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
}
试试这个
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
如果您想从自定义 class 传递数据,您需要按照@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
}
}
你的 CustomView 只是一个 class 最后,所以你可以在面向对象的参数中做到这一点,为此在你的 customView 中编写一个函数来传递数据。喜欢
class SignSubmitBTN: UIView {
var data: String!;
public func setData(data: String) {
self.data = data;
}
/// Other code
}
并在初始化 CustomView 后设置数据,调用 setData(params) 函数在其中设置数据。
我创建了一个自定义输入附件视图,它是提交按钮。 但是,我需要将数据传递给自定义视图,然后执行进一步的功能。这是一个很好的方法吗?
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
}
}
我不确定如何将数据传递到自定义视图,或者我应该在 Outlet Action
中执行 sign up
吗?
这是我的自定义视图
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(){}
}
如果我必须将数据传递到自定义视图,我应该使用协议吗?如果我应该使用协议怎么用呢?
好的...
我认为你是从错误的方向来处理这个问题的。按钮的职责应该是告诉您用户点击了它,仅此而已。该按钮不应处理登录。
但是...你已经完成了这里的 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
}
试试这个
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
如果您想从自定义 class 传递数据,您需要按照@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
}
}
你的 CustomView 只是一个 class 最后,所以你可以在面向对象的参数中做到这一点,为此在你的 customView 中编写一个函数来传递数据。喜欢
class SignSubmitBTN: UIView {
var data: String!;
public func setData(data: String) {
self.data = data;
}
/// Other code
}
并在初始化 CustomView 后设置数据,调用 setData(params) 函数在其中设置数据。