我是一名新手快速程序员,我被要求编写一个应用程序,允许您输入一个单词,然后生成一个随机的Haiku 这是一个带选项卡的应用程序,带有两个ViewControllers。 (诗)基于这个词。所以在FirstViewController中我有数据,我想在SecondViewController中以一种很好的方式显示那些数据。 我有所有的诗行和所有在FirstViewController中,但我想在SecondViewController中访问这些变量。我已经尝试创建一个函数,除了返回它们之外什么都不做,然后在调用该函数的SecondViewController中,但没有任何结果,因为函数只返回nil。如果你们中的任何人能提供帮助,我们会很高 谢谢!
这是FirstViewController:
import UIKit
import Foundation
class FirstViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var keyWordTextField: UITextField!
@IBOutlet weak var syllableSlider: UISlider!
@IBOutlet weak var syllableSliderLabel: UILabel!
var syllableSliderValue = 1
@IBOutlet weak var lineOneTextField: UITextField!
@IBOutlet weak var lineTwoTextField: UITextField!
@IBOutlet weak var lineThreeTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
lineOneTextField.text = "Rad 1"
lineTwoTextField.text = "Rad 2"
lineThreeTextField.text = "Rad 3"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func syllableValueChanged(sender: UISlider) {
syllableSliderValue = Int((sender.value))
syllableSliderLabel.text = "Ordet har: \(syllableSliderValue) stavelser"
}
@IBAction func getNewHaiku() {
if keyWordTextField.text != "" {
let keyWord = keyWordTextField.text
let lineOne = generateLine(keyWord: keyWord, syllables: syllableSliderValue, lineSyllableLenght: 5)
let lineTwo = generateLine(keyWord: keyWord, syllables: syllableSliderValue, lineSyllableLenght: 7)
let lineThree = generateLine(keyWord: keyWord, syllables: syllableSliderValue, lineSyllableLenght: 5)
lineOneTextField.text! = lineOne
lineTwoTextField.text! = lineTwo
lineThreeTextField.text! = lineThree
}
}
func generateLine(#keyWord: String, syllables : Int, lineSyllableLenght : Int) -> String {
let oneSyllables = Dict().oneSyllables
let twoSyllables = Dict().twoSyllables
let threeSyllables = Dict().threeSyllables
let fourSyllables = Dict().fourSyllables
let randomOneSyllableWordNumber = Int(arc4random_uniform(UInt32(oneSyllables.count)))
let randomTwoSyllableWordNumber = Int(arc4random_uniform(UInt32(twoSyllables.count)))
let randomThreeSyllableWordNumber = Int(arc4random_uniform(UInt32(threeSyllables.count)))
let randomFourSyllableWordNumber = Int(arc4random_uniform(UInt32(fourSyllables.count)))
var lineArray : [String] = []
var line = ""
lineArray.append(keyWord)
if syllables == 1 {
let randomWordMethod = Int(arc4random_uniform(2))
if randomWordMethod == 0 {
lineArray.append(fourSyllables[randomFourSyllableWordNumber])
} else if randomWordMethod == 1 {
lineArray.append(threeSyllables[randomThreeSyllableWordNumber])
lineArray.append(oneSyllables[randomOneSyllableWordNumber])
} else if randomWordMethod == 2 {
lineArray.append(oneSyllables[randomOneSyllableWordNumber])
lineArray.append(twoSyllables[randomOneSyllableWordNumber])
lineArray.append(oneSyllables[randomOneSyllableWordNumber])
}
} else if syllables == 2 {
let randomWordMethod = Int(arc4random_uniform(2))
if randomWordMethod == 0 {
lineArray.append(twoSyllables[randomOneSyllableWordNumber])
lineArray.append(oneSyllables[randomTwoSyllableWordNumber])
} else if randomWordMethod == 1 {
lineArray.append(threeSyllables[randomThreeSyllableWordNumber])
} else if randomWordMethod == 2 {
lineArray.append(twoSyllables[randomTwoSyllableWordNumber])
lineArray.append(oneSyllables[randomOneSyllableWordNumber])
}
} else if syllables == 3 {
let randomWordMethod = Int(arc4random_uniform(1))
if randomWordMethod == 0 {
lineArray.append(twoSyllables[randomTwoSyllableWordNumber])
} else if randomWordMethod == 1 {
lineArray.append(oneSyllables[randomOneSyllableWordNumber])
lineArray.append(oneSyllables[randomOneSyllableWordNumber])
}
} else if syllables == 4 {
lineArray.append(oneSyllables[randomOneSyllableWordNumber])
}
if lineSyllableLenght == 7 {
let randomWordMethod = Int(arc4random_uniform(1))
if randomWordMethod == 0 {
lineArray.append(oneSyllables[randomOneSyllableWordNumber])
lineArray.append(oneSyllables[randomOneSyllableWordNumber])
} else if randomWordMethod == 1 {
lineArray.append(twoSyllables[randomTwoSyllableWordNumber])
}
}
for word in lineArray {
line += " \(word)"
}
line += ","
return line
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
self.view.endEditing(true)
}
func getData() -> (line2: String, line3: String) {
return (lineTwoTextField.text, lineThreeTextField.text)
}
}
Ps," Dict"是另一个文件,但只包含单词。
第二个视图控制器只是空白。
答案 0 :(得分:4)
您需要在第二个视图控制器中传递这样的实例:
var firstViewController: FirstViewController?
然后在知道两者的主实例中:
secondViewController.firstViewController = firstViewController
(例如在awakeFromNib
中)假设它们在主实例中已知,如
let firstViewController = FirstViewController()
let secondViewController = SecondViewController()
最后在SecondViewController中,您可以访问第一个:
firstViewController?.generateLine....
答案 1 :(得分:3)
或者您可以将其设为全局变量,以便任何文件都可以访问它。
struct structname {
static var yourvariable = value
}
调用时,请输入
filename.structname.yourvariable