为什么委托值没有进入 swift 中的文本字段?
Why delegate value is not coming to textfield in swift?
我正在使用委托获取 phone 号码国家代码。
这里我创建了委托:
public protocol CountryListDelegate: class {
func selectedCountry(country: Country)
}
public class CountryList: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating {
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! CountryCell
let country = cell.country!
self.searchController?.isActive = false
self.delegate?.selectedCountry(country: country)
tableView.deselectRow(at: indexPath, animated: true)
self.tableView.reloadData()
self.dismiss(animated: true, completion: nil)
}
}
这里我没有得到 countryCodeTextfield 中的值:
class ViewController: UIViewController, CountryListDelegate, UITextFieldDelegate {
@IBOutlet weak var countryCodeTextfield: UITextField!
@IBOutlet weak var counrtryCodeBtn: UIButton!
@IBOutlet weak var mobileNumTextfield: UITextField!
var countryList = CountryList()
@IBAction func submitBtnAction(_ sender: UIButton) {
print("in submit")
if sender.tag == 1 {
if (mobileNumTextfield.text?.count)!<1 {
Constants.showAlertView(alertViewTitle: "", Message: "Please Enter Mobile Number", on: self)
return
}
self.loginService()
} else {
let navController = UINavigationController(rootViewController: countryList)
self.present(navController, animated: true, completion: nil)
}
}
func selectedCountry(country: Country) {
print("in country")
self.countryCodeTextfield.text="+\(country.phoneExtension)"
print("\(country.flag!) \(country.name!), \(country.countryCode), \(country.phoneExtension)")
}
上面的 tableview 正在显示,但 selectedCountry
没有调用 print stament 也没有出现。
请帮我写代码。
设置委托
countryList.delegate = self
let navController = UINavigationController(rootViewController: countryList)
将CountryList委托给自己,自己就是委托的ViewController!
//MARK: Create a delegate property in Country List.
weak var delegate: CountryListDelegate?
创建 CountryList 实例并将其委托分配给自身后
var countryList = CountryList()
countryList.delegate = self
我正在使用委托获取 phone 号码国家代码。
这里我创建了委托:
public protocol CountryListDelegate: class {
func selectedCountry(country: Country)
}
public class CountryList: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating {
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! CountryCell
let country = cell.country!
self.searchController?.isActive = false
self.delegate?.selectedCountry(country: country)
tableView.deselectRow(at: indexPath, animated: true)
self.tableView.reloadData()
self.dismiss(animated: true, completion: nil)
}
}
这里我没有得到 countryCodeTextfield 中的值:
class ViewController: UIViewController, CountryListDelegate, UITextFieldDelegate {
@IBOutlet weak var countryCodeTextfield: UITextField!
@IBOutlet weak var counrtryCodeBtn: UIButton!
@IBOutlet weak var mobileNumTextfield: UITextField!
var countryList = CountryList()
@IBAction func submitBtnAction(_ sender: UIButton) {
print("in submit")
if sender.tag == 1 {
if (mobileNumTextfield.text?.count)!<1 {
Constants.showAlertView(alertViewTitle: "", Message: "Please Enter Mobile Number", on: self)
return
}
self.loginService()
} else {
let navController = UINavigationController(rootViewController: countryList)
self.present(navController, animated: true, completion: nil)
}
}
func selectedCountry(country: Country) {
print("in country")
self.countryCodeTextfield.text="+\(country.phoneExtension)"
print("\(country.flag!) \(country.name!), \(country.countryCode), \(country.phoneExtension)")
}
上面的 tableview 正在显示,但 selectedCountry
没有调用 print stament 也没有出现。
请帮我写代码。
设置委托
countryList.delegate = self
let navController = UINavigationController(rootViewController: countryList)
将CountryList委托给自己,自己就是委托的ViewController!
//MARK: Create a delegate property in Country List.
weak var delegate: CountryListDelegate?
创建 CountryList 实例并将其委托分配给自身后
var countryList = CountryList()
countryList.delegate = self