我試圖開始編寫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.
}
}
我在代碼中沒有錯誤,委託不工作,它總是零。
謝謝。
要訪問模態視圖,我使用的是Segue,當我按下ViewController中的按鈕時。 – 2015-04-05 21:56:32
這個按鈕,這是你的ViewController中的另一個'ctrl'拖動到你的ModalViewController嗎?從你最初的文章來看,它聽起來像你想單擊一個單元格並打開ModalViewController。 – 2015-04-05 22:10:09
是的,這是一個按鈕,我ctrl拖動到另一個視圖,並設置segue模態。我剛剛找到了一個涵蓋我的問題的好教程,所以我將從那裏開始。 http://www.raywenderlich.com/81880/storyboards-tutorial-swift-part-2 – 2015-04-05 22:18:20