问题描述
当它在 SecondVC 的 tableViewCell 中选择时,我试图将 firstVC 中的 UIbutton 文本设置为 selectedCity 变量。我不能使用 segue 或 tabBarController。 Updated photo of ui
第一VC
import UIKit
class homeView: UIViewController{
@IBOutlet weak var selectRegion: UIButton!
}
第二个VC
import UIKit
class selectCityPage: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var selectedCity: String!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
}
func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0{
selectedCity = "Almaty"
}
if indexPath.row == 1{
selectedCity = "Усть-Каменогорск"
}
dismiss(animated: true,completion: nil)
// when this view controller is dismissed,uibutton's text in next viewcontroller should equal to selectedCity
}
}
解决方法
您可以使用委托。这些是必需的步骤:
- 创建一个委托协议,定义发送给委托的消息。
- 在委托类中创建委托属性以跟踪委托。
- 在委托类中采用并实现委托协议。
- 从委托对象调用委托。
第二个VC
import UIKit
//1. Create a delegate protocol
protocol CitySelectionDelegate {
func pickCity(with selectedCity:String)
}
class SelectCityPage: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var selectedCity: String!
//2. Create a delegate property in the delegating class
var delegate:CitySelectionDelegate?
//other stuff
func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath){
if indexPath.row == 0{
selectedCity = "Almaty"
}
if indexPath.row == 1{
selectedCity = "Усть-Каменогорск"
}
4. Call the delegate from the delegating object.
delegate?.pickCity(with: selectedCity) //call your delegate method
//dismiss(animated: true,completion: nil) when you dismiss is up to you
}
}
HomeViewController 更新:由于您在 UINavigationController 中嵌入了另一个 VC,因此主页和选择区域页面都必须符合委托,并且您必须在 prepareForSegue 方法中设置委托。
// 3. Adopt and implement the delegate protocol
class HomeViewController: UIViewController,CitySelectionDelegate{
@IBOutlet weak var selectRegion: UIButton!
func pickCity(with selectedCity: String) {
self.selectRegion.text = selectedCity
}
/*please pay attention. In this case you must reference the
navigation controller first and the your can get the right
instance of the delegate variable inside your firstVC (I called
firstVC but in your case is Select Region Page*/
override func prepare(for segue: UIStoryboardSegue,sender: Any?) {
// you MUST set the right identifier
if segue.identifier == "showFirst" {
if let navController = segue.destination as? UINavigationController {
if let firstVC = navController.topViewController as? FirstViewController{
firstVC.delegate = self
}
}
}
}
}
因为你有另一个 VC(嵌入在导航控制器中),这个也必须像这样符合委托:
class FirstViewController: UIViewController,CitySelectionDelegate {
var delegate: CitySelectionDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
func pickCity(with selectedCity: String){
// here you simply call the delegate method again and you dismiss the navigation controller
self.delegate?.pickCity(with: selectedCity)
self.navigationController?.dismiss(animated: true,completion: nil)
}
override func prepare(for segue: UIStoryboardSegue,sender: Any?) {
if segue.identifier == "showSecond" {
if let controller = segue.destination as? SelectCityPage {
controller.delegate = self
}
}
}
,
- 当 secVC 操作更改您需要的 firstVC 值时,您可以使用委托或阻止或通知。
- 或者您在 secVC 中设置一个属性并在 firstVC 中传递要更改的值,因此您可以在 secVC 中更改从 firstVC 传递的值。