使用Segue将数据从一个视图传递到Swift中的另一个视图

时间:2018-03-22 02:08:31

标签: ios swift

我正在尝试使用Swift4将数据从一个视图传递到另一个视图。但是,我在尝试打印时遇到此问题。 以下是我收到的消息:

  

2018-03-21 21:47:03.542578-0400数独[64427:1070575]< UIView:   0x7fd28bc0b4c0; frame =(0 0; 414 736); autoresize = W + H; layer =   < CALayer:0x604000038500>>'窗口不等于   < Sudoku.setupSudoku:0x7fd28bf256c0>' s视窗的窗口!

这是我的观点的图片。 segue的标识符:setupScreen

enter image description here

以下是setPlayer中的代码:

class setPlayer: UIViewController {
    var singleUser:playerInfor=playerInfor()
    var randomGame:Int = -1
    var playerName:String=""
    @IBOutlet weak var txtPlayerName: UITextField!
    var sizeOfSudoku:Int = 9 //for project 1, let take one size of sudoku

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor(patternImage: UIImage(named: "Communication-Network-Vector-Illustration.jpg")!)
        self.txtPlayerName.layer.borderWidth=2.0
        self.txtPlayerName.layer.borderColor=UIColor.blue.cgColor
        // Do any additional setup after loading the view.
    }

    func randomNumber(){
        self.randomGame=Int(arc4random_uniform(3)+1)
    }

    func initialize(){
        //if(size)

        if txtPlayerName.text?.isEmpty ?? true {
            self.playerName="Player1"
        } else {
            self.playerName=self.txtPlayerName.text!
        }
        self.singleUser=playerInfor(playerName:self.playerName,size:self.sizeOfSudoku,time:0, randomSudoku:self.randomGame)
        //print (self.singleUser.size)
    }

    @IBAction func startButton(_ sender: Any) {
        initialize()
        performSegue(withIdentifier: "setupScreen", sender: self.singleUser)

    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "setupScreen"{
            if let destination=segue.destination as?setupSudoku{
                destination.objectPlayer=sender as?playerInfor
                print(singleUser.playerName)
            }
        }
    }

    @IBAction func size9(_ sender: Any) {
        self.sizeOfSudoku=9
    }

    @IBAction func Size6(_ sender: Any) {
        self.sizeOfSudoku=9
    }

这是playerInfor

import Foundation
import UIKit

class playerInfor{
    var playerName:String=""
    var size:Int=0
    var time:Int=var randomSudoku:Int=0

    init(playerName:String,size:Int,time:Int,randomSudoku:Int) {
        self.playerName=playerName
        self.size=size
        self.time=time
        self.randomSudoku=randomSudoku
    }
}

这是setupSudoku

class setupSudoku: UIViewController , UICollectionViewDelegate, UICollectionViewDataSource{
    @IBOutlet weak var stackViewButtons: UIStackView!
    @IBOutlet weak var myCollectionView: UICollectionView!
    var newNumber=0
    var objectPlayer:playerInfor!

    override func viewDidLoad() {
        super.viewDidLoad()
        print(self.objectPlayer?.time)

    }
}

1 个答案:

答案 0 :(得分:3)

您已在故事板中错误地设置了segue,并且它正在尝试两次。要确认是这种情况,请在故事板中选择您的segue,并检查您的按钮是否突出显示,如下所示:

Highlighted button for selected segue

要解决此问题,请删除此segue并创建一个新的segue,方法是从视图控制器(而不是按钮)控制拖动到目的地,如下所示:

View controller segue dragging

确保将新的segues标识重置为setupScreen。那你应该好好去!