xcode 从另一个文件快速访问变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28002847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
swift accessing variable from another file
提问by David Wolters
I am a newbie swift programmer, and I have been asked to write an app that allows you to type in a word, and then generates a random Haiku This is a tabbed application, with two ViewControllers. (poem) based on that word. So in the FirstViewController I have the data, and I want to display that data in a nice way, in the SecondViewController. I have all the poem lines and all in the FirstViewController, but I would like to access these variables in the SecondViewController. I have tried creating a function, that does nothing but returning them, and then in the SecondViewController calling that function, but without any result, since the function simply returned nil. Would be pleased if any of you could help Thank you!
我是一个新手 swift 程序员,我被要求编写一个应用程序,允许你输入一个单词,然后生成一个随机的俳句 这是一个选项卡式应用程序,有两个 ViewController。(诗)根据那个词。所以在 FirstViewController 中我有数据,我想在 SecondViewController 中以一种很好的方式显示该数据。我有所有的诗行和所有在 FirstViewController 中,但我想在 SecondViewController 中访问这些变量。我尝试创建一个函数,它只返回它们,然后在 SecondViewController 中调用该函数,但没有任何结果,因为该函数只是返回 nil。如果你们中的任何人能提供帮助,我会很高兴谢谢!
Here is the FirstViewController:
这是 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, the "Dict" is another file, but only containing words.
Ps,“字典”是另一个文件,但只包含单词。
The second view controller is just blank.
第二个视图控制器只是空白。
回答by White Phantom
Or you could make it a global variable so any file can access it.
或者您可以将其设为全局变量,以便任何文件都可以访问它。
struct structname {
static var yourvariable = value
}
When calling it, you enter
调用它时,您输入
filename.structname.yourvariable
回答by qwerty_so
You need to pass the instances like this in second view controller:
您需要在第二个视图控制器中传递这样的实例:
var firstViewController: FirstViewController?
Then in the master instance which knows both:
然后在知道两者的主实例中:
secondViewController.firstViewController = firstViewController
(e.g. in awakeFromNib
) assuming that they are known in the master instance like
(例如awakeFromNib
)假设它们在主实例中是已知的,例如
let firstViewController = FirstViewController()
let secondViewController = SecondViewController()
Finally in SecondViewController you can access the first:
最后在 SecondViewController 中,您可以访问第一个:
firstViewController?.generateLine....