似乎无法获得传递给另一个控制器的特定值

时间:2017-04-22 17:33:16

标签: ios swift uiviewcontroller

所以我有3个控制器,比方说ChatVCMenuVCInviteVC

ChatVC开始,我打开菜单,点按一个按钮,将菜单关闭回ChatVC,然后转到InviteVC

ChatVCMenuVC都有我需要传递给InviteVCcurrentRoomID)的值。然而,这似乎是将问题转移到InviteVC的问题。 currentRoomID在InviteVC上初始化为空字符串。

这是当我点按菜单中的按钮转到InviteVC时执行的操作:

@IBAction func invitePressed(_ sender: Any) {
    weak var pvc = self.presentingViewController
    self.dismiss(animated: true) {
        pvc?.performSegue(withIdentifier: "chatInvite", sender: nil)
    }
}

我尝试在解雇闭包中以及viewDidLoadMenuVC的{​​{1}}中添加此内容:

ChatVC

我尝试在let inviteVC = InviteVipViewController() inviteVC.currentRoomId = self.currentRoomID 的prepareForSegue:

中传递它
ChatVC

当我到达override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if let destinationViewController = segue.destination as? MenuViewController { destinationViewController.transitioningDelegate = self destinationViewController.interactor = interactor destinationViewController.currentRoomID = self.currentRoomID } else if segue.identifier == "chatInvite" { let inviteVC = InviteVipViewController() inviteVC.currentRoomID = self.currentRoomID } } 时,currentUserID每次都是空字符串。当我从InviteVC直接InviteVC时,它似乎没有问题,但是因为我将其更改为当前转换(菜单后退回MenuVC,然后segues到ChatVC),似乎无法通过这个价值。

这令人非常沮丧,所以如果有人能帮助我尝试一些我没有尝试过的东西,我将非常感激!

3 个答案:

答案 0 :(得分:1)

你在prepareForSegue中遇到问题

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationViewController = segue.destination as? MenuViewController {
    destinationViewController.transitioningDelegate = self
    destinationViewController.interactor = interactor
    destinationViewController.currentRoomID = self.currentRoomID
  // if identifier equals to chatInvite then you get your InviteViewController from segues Destination not by creating one
 } else if segue.identifier == "chatInvite" {
    let inviteVC = segue.destination as? InviteVipViewController
    inviteVC.currentRoomID = self.currentRoomID
  }
}

注意:我可以看到您试图从已经解散的ViewController中删除。所以我认为你必须将这个逻辑移到你的MainViewController,我认为它是 MenuViewController

你也不应该像这样实例化你的InviteVIPViewController

let inviteVC = InviteVipViewController()
inviteVC.currentRoomId = self.currentRoomID

segue会为你做这件事

您可以使用UserDefaults传递currentRoomID,例如

@IBAction func invitePressed(_ sender: Any) {

    let userDefaults = UserDefaults.standard
    userDefaults.set(currentRoomID, forKey: "roomID")
    // call the synchronise so it sync force the user defaults to save
    userDefaults.synchronize()

weak var pvc = self.presentingViewController
self.dismiss(animated: true) {
    pvc?.performSegue(withIdentifier: "chatInvite", sender: nil)
  }
}

现在访问inviteViewController viewDidLoad()中的currentRoomID 我还假设roomID为int

 let userDefaults = UserDefaults.standard
 currentRoomID = userDefaults.integer(forKey: "roomID")
 // if string then you can use object and cast it to string like this
 currentRoomID = userDefaults.object(forKey: "roomID") as! String

答案 1 :(得分:0)

这一行:

let inviteVC = InviteVipViewController()

创建一个全新的InviteVipViewController实例,与您将要实现的实例完全无关。您应该使用segue.destination(这是您即将要进行的实例)。唯一的附带条件是您应该测试以确认目的地的类型是否正确(就像您对MenuViewController的segue所做的那样):

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let destinationViewController = segue.destination as? MenuViewController {
        destinationViewController.transitioningDelegate = self
        destinationViewController.interactor = interactor
        destinationViewController.currentRoomID = self.currentRoomID
    } else if segue.identifier == "chatInvite" {
        if let inviteVC = segue.destination as? InviteVipViewController {
            inviteVC.currentRoomID = self.currentRoomID
        }
    }
}

答案 2 :(得分:-1)

我曾经使用segues,发现它们不必要地复杂化。这是一个解决方法......

在您导入UIKit的代码部分(在顶部),为您要传递的任何内容添加变量,在这种情况下,假设我想传递分数。

//view controller one
import UIKit
var score = 25

然后进入视图控制器,在那里你想要接收 - 在这个案例得分中,你可以轻松地访问它,就像它是你自己的视图控制器中的变量一样。

//Viewcontrollertwo
super.viewDidLoad()
score += 2
//score is now = 27, requires no segue
相关问题