简体   繁体   English

如何将一个视图 controller 值发送到 swift 中的另一个视图 controller 表视图?

[英]How to send one view controller values to another view controller table view in swift?

I have three view controllers like below我有三个视图控制器,如下所示

Add address Table view this is AddAddressViewcontroller, if i select addaddress button it goes to profileviewcontroller if i tap continue it goes to ZoomMapviewcontroller here is i confirm then i need to show that below label text in AddAddressViewcontroller Add address Table view how to do that.添加地址表视图这是 AddAddressViewcontroller,如果我 select addaddress 按钮转到profileviewcontroller如果我点击继续它转到ZoomMapviewcontroller这是我确认然后我需要在AddAddressViewcontroller添加地址表视图下面显示 label 文本如何做到这一点。

ZoomMapviewcontroller code: ZoomMapview控制器代码:

protocol DataEnteredDelegate: class {
func userDidEnterInformation(info: DataEnteredModelSave)
}

class NewZoomAddressViewController: UIViewController {
weak var delegate: DataEnteredDelegate? = nil

var zipName: String?
var localityName: String?
var sublocalityName: String?

@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var addressLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
}
@IBAction func confirmBtn(_ sender: Any) {

    let viewController = self.storyboard?.instantiateViewController(withIdentifier: "AddAddressViewController") as! AddAddressViewController
   guard
        let zipName = zipName,
        let sublocalityName = sublocalityName,
        let localityName = localityName
        else { return }
    let enteredData = DataEnteredModelSave(pinCode: zipName, streetField: sublocalityName, cityField: localityName)
    delegate?.userDidEnterInformation(info: enteredData)
    self.navigationController?.pushViewController(viewController, animated: true)

}
}

here how to send zipName, locality, subLocality valuew to Addaddressviewcontroller tableview这里如何将 zipName、locality、subLocality valuew 发送到 Addaddressviewcontroller tableview

AddAddressViewcontroller code: AddAddressViewcontroller代码:

 class AddAddressViewController: UIViewController,DataEnteredDelegate {
@IBOutlet var addressEDITTableview: UITableView!

var city: String?
var pincode: String?
var locality: String?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell: EditAddressTableViewCell = tableView.dequeueReusableCell(withIdentifier: "EditAddressTableViewCell") as! EditAddressTableViewCell
    cell.nameHeader.text = "header"
    cell.addressLabel.text = "\(city!) \(pincode!) \(locality!)" // here i need ZoomMapviewcontroller valuew in row

}
}

please help me with code, i got stuck here from long time请帮我写代码,我卡在这里很久了

There are 2 approaches here..这里有两种方法..

First method is assuming you have correctly set the DataEnteredDelegate in the AddAddressViewController.第一种方法是假设您在 AddAddressViewController 中正确设置了 DataEnteredDelegate。 Your mistake here is, you are calling a delegate method in an already existing AddAddressViewcontroller instance in the navigationController stack and pushing a new AddAddressViewcontroller into that stack again.您的错误是,您在 navigationController 堆栈中已经存在的 AddAddressViewcontroller 实例中调用委托方法,并再次将新的 AddAddressViewcontroller 推入该堆栈。

Second method, if you really want to push (which is unnecessary as per me) a new AddAddressViewController instance to the navigationController stack again.第二种方法,如果你真的想再次将一个新的 AddAddressViewController 实例推送到 navigationController 堆栈(这对我来说是不必要的)。

First approach :- You've already an AddAddressViewController instance in your navigationController stack.第一种方法:-您的 navigationController 堆栈中已经有一个 AddAddressViewController 实例。 So you just need to pop back to that view controller.所以你只需要回到那个视图 controller。 So your confirmBtn action in NewZoomAddressViewController will be:-因此,您在 NewZoomAddressViewController 中的 confirmBtn 操作将是:-

@IBAction func confirmBtn(_ sender: Any) {

    guard
        let zipName = zipName,
        let sublocalityName = sublocalityName,
        let localityName = localityName
    else { return }

    if let viewControllers = self.navigationController?.viewControllers {
        for aViewController in viewControllers {
            if aViewController.isKind(of: AddAddressViewController.self) {
                let addAddressVC = aViewController as? AddAddressViewController
                let enteredData = DataEnteredModelSave(pinCode: zipName, streetField: sublocalityName, cityField: localityName)
                delegate?.userDidEnterInformation(info: enteredData)
                self.navigationController?.popToViewController(aViewController, animated: true)
                break
            }
        }
    }
}

Second Approach :- In this case there is no need for delegate.第二种方法:- 在这种情况下,不需要委托。 You can directly pass the zipName, sublocalityName and localityName to the new AddAddressViewController.您可以直接将 zipName、sublocalityName 和 localityName 传递给新的 AddAddressViewController。

@IBAction func confirmBtn(_ sender: Any) {

    let viewController = self.storyboard?.instantiateViewController(withIdentifier: "AddAddressViewController") as! AddAddressViewController
    guard
        let zipName = zipName,
        let sublocalityName = sublocalityName,
        let localityName = localityName
        else { return }
    viewController.city = localityName
    viewController.pincode = zipName
    viewController.locality = localityName
    self.navigationController?.pushViewController(viewController, animated: true)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将值发送到Swift中的父视图控制器 - How to send values to a parent view controller in Swift 一个视图控制器中的两个表视图,快速 - two table view in one view controller, swift 如何在 Swift 中以编程方式从一个控制器导航到另一个视图控制器? - How to navigate from one controller to another View Controller programmatically in Swift? Swift为segue做准备-将数据从一个视图控制器发送到另一个 - Swift prepare for segue - Send data from one view controller to another 如何通过使用 swift 5 的动态集合视图从一个视图 controller 移动到另一个视图 controller? - How to move from one view controller to another view controller though dynamic collection view using swift 5? 在 Swift 中将值从一个视图控制器传递到另一个视图控制器 - Passing values from one view controller to another in Swift 使用另一个视图控制器 swift 更改视图控制器 - Change View Controller with another view Controller swift 如何将信息从一个视图控制器中的表视图单元传递到另一视图控制器? - How do I pass information from a table view cell in one view controller to another view controller? 快速从一个视图控制器转到另一个视图控制器 - Going from one View controller to another in swift 如何从另一个视图 controller 访问/传递变量? 不是下一个视图controller,而是Swift中下一个视图之后的视图controller - How do i access/pass variable from another view controller? It's not the next view controller, but the view controller after the next one in Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM