在Swift中启动并将字符串传递给Destination View Controller

时间:2018-10-21 16:24:25

标签: ios swift parameter-passing

我正在尝试执行看似简单的任务,即将字符串传递给代码中带有Storyboardid的ViewController,但遇到编译器错误:“ View Controller没有成员“ myString”“

目标视图控制器具有故事板ID:myVC,这是我启动它的代码:

  let storyboard = UIStoryboard(name: "secondSB", bundle: nil)
        let destvc = storyboard.instantiateViewController(withIdentifier: "myVC")
        destvc.myString = "mapme"

viewcontroller被分配给如下所示的类:

class myMapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
    @IBOutlet weak var mapView: MKMapView!
    var myString:String = ""
   }   

这似乎非常简单,但是我还是Swift的新手。如果我删除错误行btw,则VC确实可以正常启动。我可能遗漏什么导致此错误?

1 个答案:

答案 0 :(得分:1)

#include <stdio.h> int main(void) { int x; while (printf("Write a number: "), scanf("%d", &x) != 1) // when the number of successful conversion wasn't 1 { fputs("Not valid!\n", stderr); int ch; while ((ch = getchar()) != EOF && ch != '\n'); // clear stdin from garbage } // that might be left. } 返回基类instantiateViewController,您必须将类型强制转换为子类。

请使用首字母大写来命名类,结构和枚举

UIViewController