如何从另一个 Class 导入或访问 UITextField
How to import or access UITextField from another Class
我在主 VC 中有 UITextField 的出口,我想从另一个 class 访问它的文本...有关更多说明,请参阅下面的代码:
class ProductManagement : UIViewController, UITextFieldDelegate{
@IBOutlet weak var ProductName: UITextField!
}
我想阅读下面的文字class
import Firebase
import FirebaseStorage
class XUpload {
static func UploadImage(Image : UIImage, Completion : @escaping (_ url : String)->()) {
guard let imageData = Image.pngData() else { return }
let storage = Storage.storage().reference()
let pathRef = storage.child("Images/Products")
let imageRef = pathRef.child( // I want the product name to be written here! )
imageRef.putData(imageData, metadata: nil) { (meta, error) in
imageRef.downloadURL { (url, error) in
print (url as Any)
}
}
}
}
我试图创建一个自定义协议来委托 UITextField,但问题是我无法在 XUpload 中符合它 class !!
请有人为我写下我的代码应该如何,因为我是初学者并且是 Swift 语言的新手。
提前致谢。
编辑:
下面应用的 Elia 答案:
产品管理Class + 扩展
class ProductManagement : UIViewController, UITextFieldDelegate{
override func viewDidLoad() {
super.viewDidLoad()
self.ProductName.delegate = self
}
@IBOutlet weak var ProductName: UITextField!
@objc private func textFieldDidChange(_ sender: UITextField) {
NotificationCenter.default.post(name: .PnameInputText, object: nil, userInfo: ["text": sender.text!])
}
}
extension Notification.Name {
public static var PnameInputText = Notification.Name("PnameInputText")
}
X上传Class
import Firebase
import FirebaseStorage
class XUpload {
private init() {
NotificationCenter.default.addObserver(self, selector: #selector(handle(_:)), name: .PnameInputText, object: nil)
}
public static var shared = XUpload()
var textFieldText : String = "" {
didSet {print("updated value > ", textFieldText)}
}
@objc private func handle(_ sender: Notification) {
if let userInfo = sender.userInfo as NSDictionary?, let text = userInfo["text"] as? String {
textFieldText = text
}
}
static func UploadImage(Image : UIImage, Completion : @escaping (_ url : String)->()) {
guard let imageData = Image.pngData() else { return }
let storage = Storage.storage().reference()
let pathRef = storage.child("Images/Products")
let imageRef = pathRef.child("img_"+shared.textFieldText)
print("var of textFieldText value: " + shared.textFieldText) //print nothing... empty value!!
imageRef.putData(imageData, metadata: nil) { (meta, error) in
imageRef.downloadURL { (url, error) in
print ("Image uploaded to this link >> " + url!.description)
guard let str = url?.absoluteString else { return }
Completion (str)
}
}
}
}
extension UIImage {
func upload(completion : @escaping (_ url : String) ->()) {
XUpload.UploadImage(Image: self) { (ImageURL) in completion (ImageURL)
}
}
}
如您所见,通知中心没有为 var textFieldText 赋值!为什么??
class ProductManagement : UIViewController, UITextFieldDelegate{
var productInputText: String = ""
@IBOutlet weak var ProductName: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
ProductName.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
}
@objc private func textFieldDidChange(_ sender: UITextField) {
productInputText = sender.text ?? ""
}
}
这会将您的输入文本存储到一个基本上带有监听编辑更改的变量中。
如果我没记错的话,你在 ProductManagement
class 中使用 UploadImage
。将函数 属性 添加到 UploadImage
如下
class XUpload {
static func UploadImage(Image : UIImage, productName: String, Completion : @escaping (_ url : String)->()) {
guard let imageData = Image.pngData() else { return }
let storage = Storage.storage().reference()
let pathRef = storage.child("Images/Products")
let imageRef = pathRef.child( // I want the product name to be written here! )
imageRef.putData(imageData, metadata: nil) { (meta, error) in
imageRef.downloadURL { (url, error) in
print (url as Any)
}
}
}
}
并在 ProductManagement
class 中调用它,如下所示。
XUpload.UploadImage(image: myImage, productName: productInputText, Completion: {
}
编辑:
收到您的评论后,我决定最好的方法是使用 Notification
,您在 ProductManagement class 中使用 UITextField
,因此无需处理 delegate
他们在 XUpload
中的方法
描述通知名称
extension Notification.Name {
public static var TextChange = Notification.Name("TextChange")
}
在 ProductManagement
中,textDidChange
方法 post 将文本发送到 Notification
@objc private func textFieldDidChange(_ sender: UITextField) {
NotificationCenter.default.post(name: .TextChange, object: nil, userInfo: ["text": sender.text])
}
我将你的 XUpload
class 转换为 Singleton
class.
class XUpload {
private init() {
NotificationCenter.default.addObserver(self, selector: #selector(handle(_:)), name: .TextChange, object: nil)
}
public static var Shared = XUpload()
@objc private func handle(_ sender: Notification) {
if let userInfo = sender.userInfo as NSDictionary?, let text = userInfo["text"] as? String {
textFieldText = text
}
}
var textFieldText: String = "" {
didSet {
print("updated value > " , textFieldText)
}
}
static func uploadImage() {
// use updated textfield text with textFieldText
}
}
然后在 ProductManagement
class 中存储一个变量作为单例对象,它会为你工作。文本字段中的文本在 XUpload
class.
中更新每个更改
class ProductManagement : UIViewController, UITextFieldDelegate{
var staticVariable = XUpload.Shared
}
我在主 VC 中有 UITextField 的出口,我想从另一个 class 访问它的文本...有关更多说明,请参阅下面的代码:
class ProductManagement : UIViewController, UITextFieldDelegate{
@IBOutlet weak var ProductName: UITextField!
}
我想阅读下面的文字class
import Firebase
import FirebaseStorage
class XUpload {
static func UploadImage(Image : UIImage, Completion : @escaping (_ url : String)->()) {
guard let imageData = Image.pngData() else { return }
let storage = Storage.storage().reference()
let pathRef = storage.child("Images/Products")
let imageRef = pathRef.child( // I want the product name to be written here! )
imageRef.putData(imageData, metadata: nil) { (meta, error) in
imageRef.downloadURL { (url, error) in
print (url as Any)
}
}
}
}
我试图创建一个自定义协议来委托 UITextField,但问题是我无法在 XUpload 中符合它 class !!
请有人为我写下我的代码应该如何,因为我是初学者并且是 Swift 语言的新手。
提前致谢。
编辑:
下面应用的 Elia 答案:
产品管理Class + 扩展
class ProductManagement : UIViewController, UITextFieldDelegate{
override func viewDidLoad() {
super.viewDidLoad()
self.ProductName.delegate = self
}
@IBOutlet weak var ProductName: UITextField!
@objc private func textFieldDidChange(_ sender: UITextField) {
NotificationCenter.default.post(name: .PnameInputText, object: nil, userInfo: ["text": sender.text!])
}
}
extension Notification.Name {
public static var PnameInputText = Notification.Name("PnameInputText")
}
X上传Class
import Firebase
import FirebaseStorage
class XUpload {
private init() {
NotificationCenter.default.addObserver(self, selector: #selector(handle(_:)), name: .PnameInputText, object: nil)
}
public static var shared = XUpload()
var textFieldText : String = "" {
didSet {print("updated value > ", textFieldText)}
}
@objc private func handle(_ sender: Notification) {
if let userInfo = sender.userInfo as NSDictionary?, let text = userInfo["text"] as? String {
textFieldText = text
}
}
static func UploadImage(Image : UIImage, Completion : @escaping (_ url : String)->()) {
guard let imageData = Image.pngData() else { return }
let storage = Storage.storage().reference()
let pathRef = storage.child("Images/Products")
let imageRef = pathRef.child("img_"+shared.textFieldText)
print("var of textFieldText value: " + shared.textFieldText) //print nothing... empty value!!
imageRef.putData(imageData, metadata: nil) { (meta, error) in
imageRef.downloadURL { (url, error) in
print ("Image uploaded to this link >> " + url!.description)
guard let str = url?.absoluteString else { return }
Completion (str)
}
}
}
}
extension UIImage {
func upload(completion : @escaping (_ url : String) ->()) {
XUpload.UploadImage(Image: self) { (ImageURL) in completion (ImageURL)
}
}
}
如您所见,通知中心没有为 var textFieldText 赋值!为什么??
class ProductManagement : UIViewController, UITextFieldDelegate{
var productInputText: String = ""
@IBOutlet weak var ProductName: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
ProductName.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
}
@objc private func textFieldDidChange(_ sender: UITextField) {
productInputText = sender.text ?? ""
}
}
这会将您的输入文本存储到一个基本上带有监听编辑更改的变量中。
如果我没记错的话,你在 ProductManagement
class 中使用 UploadImage
。将函数 属性 添加到 UploadImage
如下
class XUpload {
static func UploadImage(Image : UIImage, productName: String, Completion : @escaping (_ url : String)->()) {
guard let imageData = Image.pngData() else { return }
let storage = Storage.storage().reference()
let pathRef = storage.child("Images/Products")
let imageRef = pathRef.child( // I want the product name to be written here! )
imageRef.putData(imageData, metadata: nil) { (meta, error) in
imageRef.downloadURL { (url, error) in
print (url as Any)
}
}
}
}
并在 ProductManagement
class 中调用它,如下所示。
XUpload.UploadImage(image: myImage, productName: productInputText, Completion: {
}
编辑:
收到您的评论后,我决定最好的方法是使用 Notification
,您在 ProductManagement class 中使用 UITextField
,因此无需处理 delegate
他们在 XUpload
描述通知名称
extension Notification.Name {
public static var TextChange = Notification.Name("TextChange")
}
在 ProductManagement
中,textDidChange
方法 post 将文本发送到 Notification
@objc private func textFieldDidChange(_ sender: UITextField) {
NotificationCenter.default.post(name: .TextChange, object: nil, userInfo: ["text": sender.text])
}
我将你的 XUpload
class 转换为 Singleton
class.
class XUpload {
private init() {
NotificationCenter.default.addObserver(self, selector: #selector(handle(_:)), name: .TextChange, object: nil)
}
public static var Shared = XUpload()
@objc private func handle(_ sender: Notification) {
if let userInfo = sender.userInfo as NSDictionary?, let text = userInfo["text"] as? String {
textFieldText = text
}
}
var textFieldText: String = "" {
didSet {
print("updated value > " , textFieldText)
}
}
static func uploadImage() {
// use updated textfield text with textFieldText
}
}
然后在 ProductManagement
class 中存储一个变量作为单例对象,它会为你工作。文本字段中的文本在 XUpload
class.
class ProductManagement : UIViewController, UITextFieldDelegate{
var staticVariable = XUpload.Shared
}