基本的に、私はUIViewControllerのテキストフィールドの値を必要とし、それを使ってif-elseステートメントを実行しました。UIAlertControllerのテキストフィールド値を取得して他の関数で使用するにはどうすればよいですか? (Swift 3)
私は辞書をループしていて、値(辞書の)がユーザの入力よりも大きいか、または等しいなら、私はテーブルビューでキーを表示します。
アプリの読み込み時に表示されるアラートボックスを作成しました。私は関数を作成し、viewDidLoad()関数の下でそれを呼び出しました。これはここで見ることができます:
func alertBox(){
// create String objects for the title and the message
let title = "Enter your GPA"
let message = "With your help, we can list courses you are most likely eligible for based on the GPA you provide to us."
// create a UIAlertController object
let alert = UIAlertController(title: title,
message: message,
preferredStyle: UIAlertControllerStyle.alert)
// create a UIAlertAction object
//2. Add the text field. You can configure it however you need.
alert.addTextField { (textField) in
textField.text = ""
}
// 3. Grab the value from the text field, and print it when the user clicks OK.
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { [weak alert] (_) in
let textField = alert?.textFields![0] // Force unwrapping because we know it exists.
if let textAlert = textField?.text {
print("Text field: \(textAlert)")
let item = Double(textAlert)
print(item!)
let unwrappedvalue = item!
self.dc.setUserInput(userI: unwrappedvalue)
print(self.dc.getUserInput())
}
}))
// add the UIAlertAction object to the UIAlertController object
let cancelAction = UIAlertAction(title: "Cancel",
style: .cancel,
handler: nil)
alert.addAction(cancelAction)
// display the UIAlertController object
present(alert, animated: true, completion: nil)
}
そして、ここでは私のviewDidLoad()関数です:あなたが見ることができるように
override func viewDidLoad() {
super.viewDidLoad()
alertBox()
}
、私は実際にDataControllerクラスのget/setメソッドをしました。このようにして値を設定し、後の方法で値を取得できます。私はそれを使用しましたが、私の価値を全く保存していないようです。私はまた、グローバル変数を作成し、その後、その変数にテキストフィールドの値を格納しようとしました。
後でalertBox()関数でvar textOfTF : Double = 0.0
そして、すなわち
:ここ
if let textAlert = textField?.text {
print("Text field: \(textAlert)")
let item = Double(textAlert)
print(item!)
self.textOfTF = item!
/*
self.dc.setUserInput(userI: unwrappedvalue)
print(self.dc.getUserInput())
*/
}
は私DataControllerクラスで私の取得/設定機能です:
var userInput : Double = 0.0
func getUserInput() -> Double{
return userInput
}
func setUserInput(userI: Double){
userInput = userI
return
}
それを管理alert.addActionメソッドを実行したときに保存する(これはこれです) :
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { [weak alert] (_) in
let textField = alert?.textFields![0] // Force unwrapping because we know it exists.
if let textAlert = textField?.text {
print("Text field: \(textAlert)")
let item = Double(textAlert)
print(item!)
let unwrappedvalue = item!
self.dc.setUserInput(userI: unwrappedvalue)
print(self.dc.getUserInput())
}
}))
しかし、その後、ちょうど私に0.0値を印刷し、そのメソッドの外に印刷します。
私のコードに間違いはありますか?お知らせ下さい。
P.S. self.dc.setUserInput(..)のdcは、私がDataControllerクラスを宣言した変数です。 TableViewControllerクラスで
:DataControllerクラスで
var dc = DataController.sharedInstance
:
static var sharedInstance = DataController()
それをテストしたところ、同じ結果が得られましたが、それでも保存できませんでした。 –
ありがとうございました –
この例では、静的なグローバル変数の使用法の一般的なビューを見ることができます:http://stackoverflow.com/a/43466290/3401842 – kkakkurt