2015-05-15 18 views
0

好的,讓我解釋一下我的設置。我有一個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. 
} 

} 
+0

您將需要實現'prepareForSegue'並可能是放鬆segue – Paulw11

+0

所以基本上1.你想在用戶按下文本字段時轉換到新的視圖。 2。用戶選擇一行並直接返回第一個視圖,並在文本字段中使用所選的行值? – Eendje

+0

@JacobsonTalom - 是的,這是正確的,這正是我要找的! – Nick89

回答

0

做你想做的,你需要實現從委託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在viewDidLoad中宣佈這一觀點CONTROLER從登錄類的委託

login.loginDelegate = self 

不,我們只需要實現,將通過與布爾值的詳細視圖控制器被調用函數:

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

我知道這不是一個非常簡單的過程,使用後它會更容易使用和理解,它總是一個好主意在文檔中閱讀更多here

+0

嗨@IcaroNZ - 謝謝我設法讓segue能夠正常工作,你能告訴我用什麼方法將數據從tableView傳回給textfield。 – Nick89

+0

@ Nick89你需要使用這個協議,我會用一個簡單的例子來編輯帖子,以幫助你 – Icaro

+0

@ Nick89完成讓我知道你是怎麼回事,這不是一個簡單的過程,而是蘋果建議的過程 – Icaro