我试图通过segue将var projectNumber从ViewController传递到Project1ViewController并将其保存为var projectNum。然后使用projectNum更新Project1ViewController中的标签。segue和action/outlets都设置好了。应用程序运行流畅(没有错误),但是,Project1ViewController中的projectNum没有得到projectNumber值并使其成为projectNum
如何将projectNumber值从ViewController传递到Project1ViewController并将其保存到projectNum中?我看过描述通过委托执行类似操作的教程,但我不确定委托是否适用于我的问题。任何反馈都有帮助。
视图控制器

import UIKit

class ViewController: UIViewController{

var projectNumber:String = ""
@IBOutlet var projectButton1 : UIButton!
@IBOutlet var projectButton2 : UIButton!
@IBOutlet var projectButton3 : UIButton!
@IBOutlet var projectButton4 : UIButton!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func updateLabels(sender: UIButton!) {

    projectNumber = String(sender.tag)
    println(projectNumber)
    sender.setTitle(String(projectNumber), forState: UIControlState.Normal)

    func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
            //if user presses Project 3921 button
        if segue.identifier == "Project3921Segue"
        {
            if var destinationVC = segue.destinationViewController as? Project1ViewController{
                destinationVC.projectNum = projectNumber
            }
        }
            //if user presses Project 3922 button
        else if segue.identifier == "Project3922Segue"
        {
            if var destinationVC = segue.destinationViewController as? Project1ViewController{
                destinationVC.projectNum = projectNumber
            }
        }
            //if user presses Project 3923 button
        else if segue.identifier == "Project3923Segue"
        {
            if var destinationVC = segue.destinationViewController as? Project1ViewController{
                destinationVC.projectNum = projectNumber
            }
        }
            //otherwise user presses Project 3924 button
        else if segue.identifier == "Project3924Segue"
        {
            if var destinationVC = segue.destinationViewController as? Project1ViewController{
                destinationVC.projectNum = projectNumber
            }
        }
    }
}
}

项目1视图控制器
import UIKit

class Project1ViewController: UIViewController {

var projectNum:String!
@IBOutlet var projectNumLabel: UILabel!
@IBOutlet var projectNumTitle: UINavigationItem!

override func viewDidLoad() {
    super.viewDidLoad()

    projectNumLabel.text = projectNum
    projectNumTitle.title = projectNum
    println(projectNum)

    // Do any additional setup after loading the view.
}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

谢谢!

最佳答案

您正在button action函数中定义prepareForSegue。它是一个要重写的ViewController函数。
您的解决方案可以简单得多:

class ViewController: UIViewController{


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if var destinationVC = segue.destinationViewController as? Project1ViewController{
            destinationVC.projectNum = segue.identifier
        }
    }
}

注意,您不需要按钮插座,因为您只需在脚本中使用正确的segue标识符配置它们。除非你需要一些其他的自定义动作被按钮触发。
以及:
class Project1ViewController: UIViewController {

    var projectNum:String!
    @IBOutlet var projectNumLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        projectNumLabel.text = projectNum
        self.title = projectNum
        println(projectNum)

    }
}

注意,我只是设置了titleProject1ViewController属性,以便将projectNum作为标题显示在导航控制器中。我假设您的控制器嵌入到导航控制器中,并且ViewController的按钮有show段到Project1ViewController

关于xcode - 通过Segue在2个 View Controller 之间传递数据并更新标签,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28379114/

10-10 19:15