使用Segue从先前的View Controller传递到另一个View Controller时,数据变为Nil

时间:2018-08-29 01:52:54

标签: ios swift segue uistoryboardsegue

我即将使用segue将数据从 ViewController 传递到另一个 ViewController 。在 1st View Controller 中通过断点从变量检查数据(事件)时,数据(事件)不是nil。但是当我检查第二视图控制器时,数据(事件)为 nil 。无论是原因是代码错误还是由于控制台中显示错误无法插入COPY_SEND ,我都感到困惑。希望我能从您那里得到一些帮助。谢谢

First View Controller showing the Event and Passcode have Data

Second View Controller showing the Event and Passcode are NIL

从First View Controller中进行搜索

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "DashBoardViewController" {
        let dashBoardController = segue.destination as! DashBoardViewController
        dashBoardController.self.event = event
        dashBoardController.self.passcode = passcode
    }
}

事件和密码变为零

override func viewDidLoad() {
    super.viewDidLoad()
    guard event != nil, passcode != nil else {
        _ = SCLAlertView(appearance: appearance).showError("No Event Details", subTitle: "There's no event details, please logout and try again")
        return
    }
    showEventDetails()
}

showEventDetails

func showEventDetails() {
    DispatchQueue.main.async{
        self.eventNameLabel.text =  "\(self.event.name.uppercased())"
        self.locationLabel.text =  "\(self.event.location.uppercased())"
        if let dateStringFromDate = getFormattedStringFromDate(date: (self.event.startDateTime), formatString: "MMMM dd, yyyy/ hh:mm a") {
            self.dateTimeLabel.text = dateStringFromDate
        } else {
            self.dateTimeLabel.text = "-"
        }
    }
}

2 个答案:

答案 0 :(得分:1)

我假设您已经通过Interface Builder将链接连接到了SubmitButton上的 DashBoardViewController 上,这意味着当您点击“提交”按钮时,将调用@IBAction func submitButton(_ sender: UIButton) { }检查您的密码是否可以使用,如果可以,您正在调用validateEventPasscode(),后者调用了一个API端点(异步),并且仅在其中填充self.event = event(第187行) ViewController.swift)。

现在,真正发生的是,当您通过IB(界面构建器)从按钮链接一个segue时,内部将有一个表演segue,我们必须通过覆盖 ViewController.swift < / strong>:source

func shouldPerformSegue(withIdentifier identifier: String, 
                 sender: Any?) -> Bool {
    return false
}

这样,您从第190行开始的呼叫-ViewController.swift:

self.performSegue(withIdentifier: "showEventDashboard", sender: self)

是开火的

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      if segue.identifier == "DashBoardViewController" {
         let dashBoardController = segue.destination as!  DashBoardViewController
         dashBoardController.event = event
         dashBoardController.passcode = passcode
     }
   }

您可以通过在 ViewController.swift 中放置三个断点来检验我的理论:

  1. 第134行,位于SubmitButton IBAction函数的validateEventPasscode()处;
  2. 来自validateEventPasscode()函数的self.performSegue(withIdentifier:“ showEventDashboard”,sender:self)的第190行;
  3. dashBoardController.event =在prepare(for segue,sender)函数中的事件的第108行;

错误的执行顺序:1、3、2-目前,如果我的理论是正确的,我可以期待; 所需的执行顺序:1、2、3。

长话短说,在self.event之后填充perfomSegue并进入下一个屏幕,这就是event nil < / strong>在下一个 VC 中。

我将您的仓库ViewController.swift

中的ViewController.swift文件用作参考

希望它会有所帮助,加油!

答案 1 :(得分:0)

以此替换FirstViewController中的prepareForSegue方法

public static void scrollToInvalidInputView(ScrollView scrollView, View view) {
    int vTop = view.getTop();

    while (!(view.getParent() instanceof ScrollView)) {
        view = (View) view.getParent();
        vTop += view.getTop();
    }

    final int scrollPosition = vTop;

    new Handler().post(() -> scrollView.smoothScrollTo(0, scrollPosition));
}

您不需要写

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "DashBoardViewController" {
        let dashBoardController = segue.destination as! DashBoardViewController
        dashBoardController.event = event
        dashBoardController.passcode = passcode
    }
}

只需从上面两行中删除自己