简体   繁体   English

如何将数据从视图控制器传递到我的弹出视图控制器 (swift/ios)

[英]How do I pass data from a View controller into my pop up view controller (swift/ios)

I'm quite new with Swift and I'm making this mini game type app that counts the score and updates the label in the view controller.我对 Swift 很陌生,我正在制作这个迷你游戏类型的应用程序,它可以计算分数并更新视图控制器中的标签。 I want to pass that score from a view controller into another external pop up view controller I created.我想将该分数从视图控制器传递到我创建的另一个外部弹出视图控制器。

@IBAction func Button7Tapped(_ sender: AnyObject)
    {
        if Index == 13 {
            game.score += 1
        } else {
            let scorepopVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "finalScorePop") as! finalScoreViewController

            self.addChildViewController(scorepopVC)
            scorepopVC.view.frame = self.view.frame
            self.view.addSubview(scorepopVC.view)
            scorepopVC.didMove(toParentViewController: self)
        }
        updateGame()
    }

Above is my code for the external pop up view controller I created, which also has a separated .swift file.上面是我创建的外部弹出视图控制器的代码,它也有一个单独的.swift文件。 How would I go about taking my game.score and passing that into my Popup view controller?我将如何获取我的game.score并将其传递到我的 Popup 视图控制器中?

In your finalScoreViewController swift file add a new property.在您的finalScoreViewController swift 文件中添加一个新属性。

final class FinalScoreViewController: UIViewController {
    var score: Int?


}

And then just assign it when you're instantiating it.然后在实例化它时分配它。

@IBAction func Button7Tapped(_ sender: AnyObject) { 
    if Index == 13 {
        game.score += 1
    } else { 
        let scorepopVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "finalScorePop") as! finalScoreViewController

        scorepopVC.score = game.score //THIS LINE

        self.addChildViewController(scorepopVC)
        scorepopVC.view.frame = self.view.frame
        self.view.addSubview(scorepopVC.view)
        scorepopVC.didMove(toParentViewController: self)
    }
    updateGame()
}

It is better to use storyboard to open the ViewController.最好使用storyboard打开ViewController。 In storyboard, right click and drag from you button to the second view controller (the one that you wish to open).在故事板中,右键单击并从您的按钮拖动到第二个视图控制器(您希望打开的那个)。

Choose the segue type that you wish to use.选择您要使用的转场类型。 In your case, I think Present Modally will work fine.在您的情况下,我认为 Present Modally 会正常工作。

在此处输入图片说明

You will see a line between the two UIViewControllers in storyboard.您将在故事板中看到两个 UIViewController 之间的一条线。 That is the segue.这就是赛格。 Tap on it.点击它。 In the Attributes inspector give the segue an identifier.在属性检查器中给 segue 一个标识符。 For instance "myFirstSegue".例如“myFirstSegue”。

在此处输入图片说明

Then in the code of the UIViewController that contains your button override prepare(for:sender:).然后在包含您的按钮的 UIViewController 代码中覆盖 prepare(for:sender:)。 This method is called when preparing for the segue to happen.在准备发生转场时调用此方法。 Iow when you tap on the button.当你点击按钮时。 You have access to the destination UIViewController and can therefor access and set the properties on it.您可以访问目标 UIViewController,因此可以访问和设置它的属性。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "myFirstSegue" {
        if let vc = segue.destination as? MyViewController {
            //here you set your data on the destination view controller
            vc.myString = "Hello World"
        }
    }
}

Note that we check the identifier, because all segues that go from this ViewController to other ViewControllers will call prepare(for:sender:)请注意,我们检查了标识符,因为从这个 ViewController 到其他 ViewController 的所有 segue 都会调用 prepare(for:sender:)

It's quite simple, Just add a property in your finalScoreViewController (if you are not already done this) and -for example- call it score:这很简单,只需在您的finalScoreViewController添加一个属性(如果您还没有这样做)并且 - 例如 - 将其称为分数:

class finalScoreViewController: UIViewController {

    var score: String?

    // ...

Add this line to the Button7Tapped action (where you set a value for finalScoreViewController's score):将此行添加到Button7Tapped操作(您在其中为 finalScoreViewController 的分数设置一个值):

let scorepopVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "finalScorePop") as! finalScoreViewController

        // add this line:
        scorepopVC.score = "My score"


        self.addChildViewController(scorepopVC)
        scorepopVC.view.frame = self.view.frame
        self.view.addSubview(scorepopVC.view)
        scorepopVC.didMove(toParentViewController: self)

Finally, in finalScoreViewController :最后,在finalScoreViewController

override func viewDidLoad() {
    super.viewDidLoad()

    if let scr = score {
        print(scr)
    }
}

Hope that helped.希望有所帮助。

You do not actually have to pass the variable to the next view controller.您实际上不必将变量传递给下一个视图控制器。 All you have to do is create a variable outside of the View Controller class, and voila, you can access your variable from anywhere, in any swift file.你所要做的就是在 View Controller 类之外创建一个变量,瞧,你可以从任何地方的任何 swift 文件中访问你的变量。 For example:例如:

var score = 0

class ViewController: UIViewController {

    override func viewDidLoad(){
        super.viewDidLoad()
        
    }
    @IBAction func Button7Tapped(_ sender: AnyObject){
        score += 1
    }
}

And then in the other View Controller, you would have something like this:然后在另一个 View Controller 中,您将拥有如下内容:

@IBOutlet weak var scoreLabel: UILabel!

class ViewController: UIViewController {
    override func viewDidLoad(){
        super.viewDidLoad()
        var timer1 = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(updateScore), userInfo: nil, repeats: true)
    }

@objc func updateScore() {
    scoreLabel.text = "You have \(score) points!"
}    

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从另一个视图 controller 访问/传递变量? 不是下一个视图controller,而是Swift中下一个视图之后的视图controller - How do i access/pass variable from another view controller? It's not the next view controller, but the view controller after the next one in Swift 如何在 Swift 中将数据从父视图控制器传递到嵌入式视图控制器? - How can I pass data from a parent view controller to an embedded view controller in Swift? 如何将值从弹出视图控制器传递到上一个视图控制器? - How to pass values from a Pop Up View Controller to the Previous View Controller? 如何使用Swift 4将数据从弹出子视图传递到View Controller - How to Pass Data From Pop-Over Subview to View Controller with Swift 4 如何将数据从视图 Controller 传递到 swift 中的容器视图 - How to pass data from View Controller to Container View in swift 如何将数据从异步任务传递到视图控制器? - How do I pass data from an asynchronous task to a view controller? 如何将数据从第三视图控制器传递回第一视图控制器? - How Do I Pass Data from Third View Controller Back to First View Controller? 在iOS Swift 2中将数据从视图控制器传递到表视图控制器 - Passing data from view controller to table view controller in ios swift 2 基于故事板的iOS项目中Swift类的弹出视图控制器 - Pop view controller from Swift class in storyboard based iOS project 如何在swift的第二个视图控制器中将信息传递给Uilabel? - How do i pass information to a Uilabel in the second view controller in swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM