【发布时间】:2015-05-15 23:54:52
【问题描述】:
好的,让我解释一下我的设置。我有一个 textField、一个 UIButton 和一个包含 3 个问题的数组。这个想法是用户必须回答每个问题,然后下一个按钮会将它们传递给数组中的下一个问题。
我想从 textField 切换到 tableviewController,然后在行中选择一个项目,然后将其返回到 textField,因此用户可以按下一步按钮并继续下一个问题。
我尝试了许多不同的方法,但都没有运气(不一定有任何错误,它只是不起作用) - 下面是我在主 VC 上的代码。我还在我的 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 -
所以基本上 1. 当用户按下文本字段时,您想转换到新视图。 2. 用户选择一行并直接返回到第一个视图,在文本字段中选择行值?
-
@JacobsonTalom - 是的,没错,这正是我要找的!
-
这是
UIPickerView的工作 -
@AlexLittlejohn - 嗯,谢谢你的建议 - 你能展示一些我如何让它与 PickerView 一起工作的代码吗?
标签: ios uitableview swift uitextfield segue