当用户按在Textfield中时,请转到tableViewController,然后以Swift将行内容传递回Textf



好吧,让我解释一下我的设置。我有一个Textfield,一个Uibutton和一个装有3个问题的阵列。这个想法是用户必须回答每个问题,下一个按钮将将它们传递给数组中的下一个问题。

我想从textfield到tableViewController,然后在该行中选择一个项目,然后将其返回到TextField,因此用户可以按下Next按钮并继续下一个问题。<<<<<<<<<<<<<<<<<<<</p>

我尝试了许多不同的方式,没有运气(不一定有任何错误,只是行不通) - 下面是我的代码,那是我的主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.
}
}

要执行您想要的事情,您需要从代表Uitextfielddelegate实现功能textfielddidbegineding,并在此处致电Preparforsegue表单

extension YourClass: UITextFieldDelegate
{
    func textFieldDidBeginEditing(textField: UITextField)
    {
        performSegueWithIdentifier("yourSegue", sender: self)
    }    
}

现在您需要一个代表才能将表视图中的值选择返回到调用的视图中。

当您说"它不起作用"时,我不确定您在哪个部分中遇到问题,无论如何,当用户单击文本字段

中时,这应该解决视图过渡的问题

要将详细信息视图传递到主视图,您需要实现协议。在详细的视图中声明协议,我的示例是在详细信息视图中发生的登录检查,并将结果传递回:

protocol LoginDelegate
{
    func loginResult(result: Bool)
}

将可选的代表声明为同一控制中的全局:

var loginDelegate:LoginDelegate? = nil

现在测试该委托是否在解开它之前是否存在,并且是否确实调用协议中的函数声明:

    if let loginDel = self.loginDelegate{
        loginDel.loginResult(userResult)
    }

这将有助于我们将价值传递给主视图。返回到主视图,您需要声明主视图实现了我们创建的新委托方法:

扩展MainViewController:LogIndelegate {}ANS在ViewDidload中将此视图控制器声明为登录类的委托

login.loginDelegate = self

不,我们只需要实现带有布尔值的详细信息控制器将调用的功能:

extension LoginViewController: LoginDelegate
{
    func loginResult(result: Bool)
    {
       //Save the result value to be used by the MainViewController
    }
}

我知道这不是一个非常简单的过程,两次都在使用后更容易使用和理解,它总是一个好主意,在此处的文档中阅读更多

最新更新