好的,讓我解釋一下我的設置。我有一個textField,一個UIButton和一個包含3個問題的數組。這個想法是用戶必須回答每個問題,並且下一個按鈕會將它們傳遞給數組中的下一個問題。當用戶在textField中按下時,繼續執行tableviewController並將行內容傳回給textField,在Swift中
我想從textField繼承到tableviewController,然後選擇行中的一個項目,並將其返回到textField,以便用戶可以按Next按鈕繼續下一個問題。
我已經嘗試了許多不同的方式,並沒有運氣(不一定有任何錯誤,它只是不起作用) - 下面是我的代碼多數民衆贊成在我的主要風險投資。我已經在main.storyboard中添加了一個tableViewController。
任何幫助非常感謝!由於
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet var questionLabel: UILabel!
@IBOutlet var buttonLabel: UIButton!
@IBOutlet var myBackgroundView: UIImageView!
@IBOutlet var questionTextField: UITextField!
let questions = ["Where are you going?", "Which city?", "When do you go?"]
var currentQuestionIndex = 0
let placeholder = ["Country", "City", "Date"]
var currentPlaceholderIndex = 0
@IBAction func nextButton(sender: AnyObject) {
// Initial setup on button press
questionTextField.hidden = false
barImage.hidden = false
// Reset text field to have no text
questionTextField.text = ""
// Displays the questions in array and displays the placeholder text in the textfield
if currentQuestionIndex <= questions.count && currentPlaceholderIndex <= placeholder.count {
questionLabel.text = questions[currentQuestionIndex]
questionTextField.placeholder = placeholder[currentPlaceholderIndex]
currentQuestionIndex++
currentPlaceholderIndex++
buttonLabel.setTitle("Next", forState: UIControlState.Normal)
// Animate text for questionLabel
UIView.animateWithDuration(1.0, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.5, options: nil, animations: {
self.questionLabel.center = CGPoint(x: -110 , y: 305 + 20)
}, completion: nil)
} else {
performSegueWithIdentifier("countdownSegue", sender: self)
//Add some logic here to run whenever the user has answered all the questions.
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Hides the text field
questionTextField.hidden = true
questionTextField.delegate = self
// Sets the button text
buttonLabel.setTitle("Get started", forState: UIControlState.Normal)
// Sets the question text to be blank
questionLabel.text = ""
// Sets placeholder text in the text field
questionTextField.placeholder = ""
}
// resigns the keyboard when user presses the return/next key on keyboard
func textFieldShouldReturn(textField: UITextField) -> Bool {
questionTextField.resignFirstResponder()
return true
}
// Resigns the keyboard if the user presses anywhere on the screen
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
self.view.endEditing(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
您將需要實現'prepareForSegue'並可能是放鬆segue – Paulw11
所以基本上1.你想在用戶按下文本字段時轉換到新的視圖。 2。用戶選擇一行並直接返回第一個視圖,並在文本字段中使用所選的行值? – Eendje
@JacobsonTalom - 是的,這是正確的,這正是我要找的! – Nick89