【问题标题】:How to pass value from one view controller to another swift?如何将值从一个视图控制器传递到另一个 swift?
【发布时间】:2014-12-30 10:12:10
【问题描述】:

我正在使用故事板构建一个 iOS 应用程序。我已经在我的应用程序中使用 swift 实现了谷歌地图。我在标签中获取地址值。我在将该标签值传递给另一个类时遇到问题(首先视图控制器)用目标 c 编写。

现在在第一个视图控制器的情节提要中发生了什么我有一个按钮,然后单击该模态 segue 执行打开的第二个视图控制器包含地图并且此地图视图控制器包含完成按钮以关闭视图控制器并返回第一个视图控制器。

这是故事板图片:

这是我的代码:

快速映射视图控制器代码:

func reverseGeocodeCoordinate(coordinate: CLLocationCoordinate2D) {
    let geocoder = GMSGeocoder()
    geocoder.reverseGeocodeCoordinate(coordinate) { response , error in

      //Add this line
      self.addressLabel.unlock()
      if var address = response?.firstResult() {
        println("label657656558568===\(address)");

        //  self.addressLabel.text=address.valueForKey ("GMSAddress") as? NSString;
        self.addressLabel.text=address.valueForKey ("subLocality") as? NSString;

        println("label===\(self.addressLabel.text)");
        let labelHeight = self.addressLabel.intrinsicContentSize().height
        self.mapView.padding = UIEdgeInsets(top: self.topLayoutGuide.length, left: 0, bottom: labelHeight, right: 0)
        UIView.animateWithDuration(0.25) {
             self.pinImageVerticalConstraint.constant = ((labelHeight - self.topLayoutGuide.length) * 0.5)
          self.view.layoutIfNeeded()
        }
      }
    }
  }
@IBAction func doTap(x:UIButton) {

     self.dismissViewControllerAnimated(true, completion: {});//This is intended to dismiss the Info sceen.
        println("pressed")

    }

【问题讨论】:

  • 现在我正在使用自定义 segue 而不是模态 segue 并在第一个视图控制器中获取 location 的值,但现在我的标签 iboutlet 隐藏在 ui 中并且不显示该值位置,但是当我在控制台中打印时,我得到了标签值。 @爱德华
  • 好吧,在你的类中你可以创建变量'var yourViewController',在'viewDidLoad()'中将它设置为infoViewController。单击按钮时,您将该控制器分配给 presentViewController,在您关闭它后,您可以检查值。因此,您将像这样呈现它:'self.presentViewController(yourViewController, animated: true, completion: nil)' 并且您可以使用 label 作为 yourViewController.yourLabel 因为,我假设它是您的 infoViewController 的属性

标签: ios swift


【解决方案1】:

您可以在此处使用delegate protocols 将值从 MapViewController 传递给 FirstViewController。这是link的示例

【讨论】:

  • 现在我正在使用自定义 segue 而不是模态 segue 并在第一个视图控制器中获取 location 的值,但现在我的标签 iboutlet 隐藏在 ui 中并且不显示该值位置,但是当我在控制台中打印时,我得到了标签值。 @suhit
  • 为什么要投反对票,这是将值传递给呈现视图控制器的一种方式,将委托用于此目的是一种命令,请解释一下?
  • 绝对有用!非常感谢 Suhit。
猜你喜欢
  • 2014-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多