从另一个文件快速访问变量 [英] swift accessing variable from another file

查看:23
本文介绍了从另一个文件快速访问变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个 swift 程序员新手,我被要求编写一个应用程序,允许您输入一个单词,然后生成一个随机俳句这是一个选项卡式应用程序,带有两个 ViewController.(诗)根据那个词.所以在 FirstViewController 中我有数据,我想在 SecondViewController 中以一种很好的方式显示该数据.我有所有的诗行和所有在 FirstViewController 中,但我想在 SecondViewController 中访问这些变量.我尝试创建一个函数,它只返回它们,然后在 SecondViewController 中调用该函数,但没有任何结果,因为该函数只是返回 nil.如果你们中的任何人可以提供帮助,我会很高兴谢谢!

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!

这是 FirstViewController:

Here is the 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"是另一个文件,但只包含单词.

Ps, the "Dict" is another file, but only containing words.

第二个视图控制器是空白的.

The second view controller is just blank.

推荐答案

你需要在第二个视图控制器中传递这样的实例:

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

(例如在 awakeFromNib 中)假设它们在主实例中是已知的,如

(e.g. in awakeFromNib) assuming that they are known in the master instance like

let firstViewController = FirstViewController()
let secondViewController = SecondViewController()

最后在 SecondViewController 中,您可以访问第一个:

Finally in SecondViewController you can access the first:

firstViewController?.generateLine....

这篇关于从另一个文件快速访问变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆