Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 按下按钮时,准备继续不发送信息_Ios_Swift_Segue - Fatal编程技术网

Ios 按下按钮时,准备继续不发送信息

Ios 按下按钮时,准备继续不发送信息,ios,swift,segue,Ios,Swift,Segue,我正在学习按下按钮时如何在控制器之间传递数据,因此我遵循以下答案:,但我无法使其工作,我有3个按钮,我想知道按下了哪个按钮,因此HomeView中的代码是: var buttonPressed: String? @IBAction func newsTapped(_ sender: Any) { buttonPressed = "news" self.performSegue(withIdentifier: "showNext", sender:

我正在学习按下按钮时如何在控制器之间传递数据,因此我遵循以下答案:,但我无法使其工作,我有3个按钮,我想知道按下了哪个按钮,因此HomeView中的代码是:

   var buttonPressed: String?

    @IBAction func newsTapped(_ sender: Any) {

        buttonPressed = "news"
        self.performSegue(withIdentifier: "showNext", sender: buttonPressed)
    }


    @IBAction func tipsTapped(_ sender: Any) {
        buttonPressed = "tips"
        self.performSegue(withIdentifier: "showNext", sender: buttonPressed)
    }


    @IBAction func otherTapped(_ sender: Any) {
        buttonPressed = "other"
        self.performSegue(withIdentifier: "showNext", sender: buttonPressed)
    }


    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let data = buttonPressed
        if let destinationViewController = segue.destination as? TableViewController {
                destinationViewController.origin = data
        }
destinationView(TableViewController)中的代码为:

我添加了If-let,因为它在语句为:
If-let-dataRecived=origin
时崩溃,现在它永远不会进入If-let语句。如果我显示变量的内容(将鼠标放在xCode中的变量上),它会显示:


知道它为什么不起作用吗?不确定它是否有什么作用,但HomeView是一个ViewController,链接到一个具有TableView的导航控制器(这是destinationView。感谢您的帮助,

看起来您的行为不正确,因为您正在运行
viewDidLoad
函数中的switch语句,该语句发生在上一个视图控制器的数据发送之前

尝试将
didSet
方法添加到
origin
属性,以如下方式加载页面:

class tableVC {
  var origin: String? {
     didSet {
         if let dataRecived = origin
           switch dataRecived {
            case "news":
                print("News")
            case "tips":
                print("Tips")
            case "other":
                print("Other") 
          }
      }
  }

  override func viewDidLoad() {
     super.viewDidLoad()
  }
}

或者您可以在
视图中运行该开关将出现
方法

我认为您遇到的问题在于:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let data = buttonPressed
        if let destinationViewController = segue.destination as? TableViewController {
                destinationViewController.origin = data
        }
如果已将UITableViewCotroller子类化,则必须将目标视图控制器强制转换为该类

`destinationViewController作为myCustomTableViewController:

if let destinationViewController = segue.destination as? myCustomTableViewController {
                destinationViewController.origin = data
        }

你检查过情节提要中的按钮动作输出吗?有时它们会随机分离。当你说它不起作用时,更具体一点。它在做什么/不在做什么?你收到错误消息了吗?@Dustinsbengler,是的,它们是正确的connected@Shades它会将我带到TableViewController,但如果我使用它,该变量没有任何信息在xCode窗口中选中它会显示它是“”,您必须调用
super.viewDidLoad()
既然您通过
sender
参数传递字符串,实际上您不需要
buttonPressed
变量。为什么您要将
origin
声明为可选?非可选的不会崩溃。在上面的一条评论中,Enrique提到,点击按钮会将他带到正确的视图,但从来不会崩溃更新所需的信息。如果他错误地设置了下一个视图控制器,我认为它会崩溃。没有Dustin。这是不正确的,因为prepare会启动,但无法以静默方式执行,并且值不会传递到目标视图控制器。但是会发生这种情况。现在来看,你是对的,因为它们已安全地取消包装destinationViewController。在本例中,您的答案很可能是正确的。
if let destinationViewController = segue.destination as? myCustomTableViewController {
                destinationViewController.origin = data
        }