【问题标题】:When user presses in a textField, segue to a tableviewController and pass back the row content to the textField, in Swift当用户在文本字段中按下时,转到 tableviewController 并将行内容传回文本字段,在 Swift
【发布时间】: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


【解决方案1】:

要做你想做的事,你需要从委托 UITextFieldDelegate 实现函数 textFieldDidBeginEditing 并在那里调用 prepareForSegue 表单

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 in viewDidLoad 将此视图控制器声明为登录类的委托

login.loginDelegate = self

不,我们只需要使用布尔值实现细节视图控制器调用的函数:

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

我知道这不是一个非常简单的过程,使用几次后它会更容易使用和理解,阅读更多文档here

总是一个好主意

【讨论】:

  • 嗨@IcaroNZ - 谢谢我已经设法让segue现在工作,你能告诉我我用什么方法将数据从tableView传回文本字段。
  • @Nick89 您需要为此使用协议,我将通过一个简单的示例编辑帖子以帮助您
  • @Nick89 done 让我知道你的进展,这不是一个简单的过程,而是苹果建议的过程
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-24
  • 1970-01-01
  • 2017-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多