2015-04-05 45 views
1

我試圖開始編寫Swift,我試圖從沒有運氣的模式視圖控制器中獲取值。在Swift iOS中從Modal View獲取價值

我有兩個控制器,ViewController和modalViewController。

在ViewController中我有一個UITableView,並按下按鈕我打開了modalViewController。

然後從UITextField我傳遞的價值。 我已經實現了一個委託和func的協議,但在某個地方我失去了一些東西或錯了。

ViewController.swift

import UIKit 

class ViewController: UIViewController,UITableViewDelegate,modalViewControllerDelegate{ 

@IBOutlet var table: UITableView! 
var tableData = ["First Row","Second Row","Third Row"] 
override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

} 

override func viewDidAppear(animated: Bool) { 

    table.reloadData() 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

func tableView(table:UITableView?,numberOfRowsInSection section: Int) -> Int 
{ 
    return tableData.count 
} 

func tableView(table:UITableView?,cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! 
{ 
    let cell:UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Default,reuseIdentifier:"cell") 
    cell.textLabel?.text = tableData[indexPath.row] 

    return cell 
} 

func sendText(text: NSString) { 
    tableData.append(text) 
} } 

modalViewController.swift

import UIKit 

protocol modalViewControllerDelegate { 
func sendText(var text: NSString) 
} 

class modalViewController: UIViewController{ 

let delegate: modalViewControllerDelegate? 

@IBOutlet var textField: UITextField? 
@IBAction func cancelButton(sender: AnyObject) { 
    dismissViewControllerAnimated(true, completion: nil) 
} 

@IBAction func saveButton(sender: AnyObject) {   
    delegate?.sendText(self.textField!.text) 
    dismissViewControllerAnimated(true, completion: nil) 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do view setup here. 
} 

}

我在代碼中沒有錯誤,委託不工作,它總是零。

謝謝。

回答

0

您必須在呈現之前設置您的modalViewControllerdelegate屬性。如果您正在使用segues,則可以在prepareForSegue(_:)中執行此操作。

此外,類名應以大寫字母開頭(modalViewController應爲ModalViewController)。只有實例應該以小寫字母開頭。

1

您必須在第一個視圖控制器中分配委託。 此外,您必須將let delegate: modalViewControllerDelegate?更改爲var,否則您無法更改它。

現在你的委託是空的。

目前還不清楚你如何訪問ModalViewController。如果您使用塞格斯:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == "modalViewControllerSegue" { 
     var destination = segue.destinationViewController as CategoryViewController 
     destination.delegate = self 
    } 
} 

或者,如果你在編程做:

var modalViewController = ModalViewController(parameters) 
modalViewController.delegate = self 
presentViewController(modalViewController, animated: true, completion: nil) 

故事板標識符:

let destination = UIStoryboard.mainStoryboard().instantiateViewControllerWithIdentifier("ModalViewController") as ModalViewController 
delegate = self 
showViewController(destination, sender: nil) 

編輯:

如果你想通過選擇一個單元來訪問ModalViewController,您需要使用tableView: didSelectRowAtIndexPath方法:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    performSegueWithIdentifier("modalViewControllerSegue", sender: self) 
} 

使用這個,你需要方法prepareForSegue來設置委託。

+0

要訪問模態視圖,我使用的是Segue,當我按下ViewController中的按鈕時。 – 2015-04-05 21:56:32

+0

這個按鈕,這是你的ViewController中的另一個'ctrl'拖動到你的ModalViewController嗎?從你最初的文章來看,它聽起來像你想單擊一個單元格並打開ModalViewController。 – 2015-04-05 22:10:09

+1

是的,這是一個按鈕,我ctrl拖動到另一個視圖,並設置segue模態。我剛剛找到了一個涵蓋我的問題的好教程,所以我將從那裏開始。 http://www.raywenderlich.com/81880/storyboards-tutorial-swift-part-2 – 2015-04-05 22:18:20

0

另一種選擇,而不是使用委託,是使用unwind segue。這裏有一個教程:http://www.cocoanetics.com/2014/04/unwinding/

在你的情況,你提出的視圖控制器,你可以有方法:

func returnFromModalView(segue: UIStoryboardSegue) { 
    // This is called when returning from the modal view controller. 

    if let modalVC = segue.sourceViewController as? ModalViewController 
     where segue.identifier == "mySegueID" { 

      let text = modalVC.textField.text 
      // Now do stuff with the text. 
     } 
    } 

然後就是連接起來,一切都在Interface Builder中所示的教程。