Reputation: 2998
Okay, let me explain my set up. I have one textField, a UIButton and an array filled with 3 questions. The idea is the user must answer each question, and the next button, will pass them to the next question in the array.
I would like to segue from a textField to a tableviewController, and then select an item in the row, and it have it returned back to the textField, so the user can then press Next button and continue to the next question.
I have tried many different ways and had no luck (not necessarily had any errors, it just doesn't work) - Below is my code thats on my main VC. I have also added a tableViewController in my main.storyboard already.
Any Help is very much appreciated! Thanks
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.
}
}
Upvotes: 0
Views: 3964
Reputation: 14845
To do what you want you would need to implement the function textFieldDidBeginEditing from the delegate UITextFieldDelegate and call prepareForSegue form there
extension YourClass: UITextFieldDelegate
{
func textFieldDidBeginEditing(textField: UITextField)
{
performSegueWithIdentifier("yourSegue", sender: self)
}
}
Now you need a delegate to return the value select in the table view to the view that call it.
When you say "It is just not working" I am not sure in what part you are have a problem, anyway this should solve the problem of the view transition when the user click in the text field
To pass values from the detail view to the master view you will need to implement protocols. In detail view declare a protocol, my example is a login checking that happened in the detail view and the result is passed back:
protocol LoginDelegate
{
func loginResult(result: Bool)
}
Declare a optional delegate as a global in the same control:
var loginDelegate:LoginDelegate? = nil
Now test if this delegate exist before unwrap it and if it does call the function declare in the protocol:
if let loginDel = self.loginDelegate{
loginDel.loginResult(userResult)
}
this will help us to pass the value to the main view. Back to the main view you will need to declare that the main view implement the new delegate method we create using:
extension MainViewController: LoginDelegate{ } ans in viewDidLoad declare this view controler as a delegate from the login class
login.loginDelegate = self
No we just need to implement the function that will get called by the detail view controller with the boolean value:
extension LoginViewController: LoginDelegate
{
func loginResult(result: Bool)
{
//Save the result value to be used by the MainViewController
}
}
I know it is not a very simple process both after use it few times it will be easier to use and understand, it always a good idea read more in the documentation here
Upvotes: 1