如何更新 swift 中其他 viewController 的标签
how to update label of other viewController in swift
当它在 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
}
}
您可以使用委派。这些是必需的步骤:
- 创建一个委托协议来定义发送给委托的消息。
- 在委托 class 中创建委托 属性 以跟踪委托。
- 在委托中采用并实施委托协议class。
- 从委托对象调用委托。
第二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
}
}
主视图控制器
更新:由于您在 UINavigationController 中嵌入了另一个 VC,主页和 Select 区域页面都必须符合委托,并且您必须在 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 传递的值。
当它在 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
}
}
您可以使用委派。这些是必需的步骤:
- 创建一个委托协议来定义发送给委托的消息。
- 在委托 class 中创建委托 属性 以跟踪委托。
- 在委托中采用并实施委托协议class。
- 从委托对象调用委托。
第二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
}
}
主视图控制器 更新:由于您在 UINavigationController 中嵌入了另一个 VC,主页和 Select 区域页面都必须符合委托,并且您必须在 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 传递的值。