我试图通过segue将var projectNumber
从ViewController传递给Project1ViewController,并将其保存为var projectNum
。然后使用projectNum
更新Project1ViewController中的标签。 segue和action / outlet都已设置完毕。该应用程序运行流畅(没有错误),但是,Project1ViewController中的projectNum
未获取projectNumber
值并使其成为projectNum
。
如何将projectNumber
值从ViewController传递给Project1ViewController并将其保存在projectNum
中?我已经看到了描述通过代表执行的类似操作的教程,但我不确定代表是否适用于我的问题。任何反馈都有帮助。
的ViewController
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
}
}
}
}
}
Project1ViewController
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.
}
}
谢谢!
答案 0 :(得分:0)
您正在里面定义按钮动作功能中的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)
}
}
注意我只是设置了title
的{{1}}属性,以便在导航控制器中显示Project1ViewController
作为标题。我假设您的控制器嵌入在导航控制器中,projectNum
的按钮ViewController
已经移位到show
。