【问题标题】:how to update label of other viewController in swift如何在swift中更新其他viewController的标签
【发布时间】:2021-05-09 13:31:48
【问题描述】:

当我在 SecondVC 的 tableViewCell 中选择 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
        
    }
}

【问题讨论】:

  • 使用委托可以实现这一目标
  • 您可以使用 NotificationCenter 来实现,请参考 learnappmaking.com/… 在 ViewControllers 之间传递数据的教程
  • 我回答了您的问题,但您能否展示一下您是如何展示 FirstVC 的 SecondVC 的?我需要一个 SecondVC 的实例来设置委托。
  • 我在第一个和第二个 vc 之间有一个导航控制器。
  • 要呈现第二个 vc,我应该点击 uibutton,而不是点击其他导航控制器中的其他按钮,而不是呈现第二个 vc

标签: ios swift pass-data


【解决方案1】:

您可以使用委托。这些是必需的步骤:

  1. 创建一个委托协议,定义发送给委托的消息。
  2. 在委托类中创建一个委托属性以跟踪委托。
  3. 在委托类中采用并实现委托协议。
  4. 从委托对象调用委托。

第二个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
        }
    }
}

【讨论】:

    【解决方案2】:
    1. 您可以在 secVC 操作时使用委托或阻止或通知来更改您需要的 firstVC 值。
    2. 或者您在 secVC 中设置一个属性并在 firstVC 中传递您要更改的值,这样您就可以在 secVC 中更改从 firstVC 传递的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多