我正在尝试开始编写 Swift,我正在尝试从模态视图 Controller 中获取一个值,但不幸的是。
我有两个 Controller ,ViewController 和 modalViewController。
在 ViewController 中,我有一个 UITableView,按下一个按钮,我打开了 modalViewController。
然后我从 UITextField 传递值。 我已经使用 delegate 和 func 实现了一个协议(protocol),但在某处我遗漏了一些东西或者有错误。
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.
}
我的代码没有错误,代理不工作,它总是 nil。
谢谢。
最佳答案
您必须在第一个 View Controller 中分配委托(delegate)。
此外,您必须将 let delegate: modalViewControllerDelegate?
更改为 var,否则您无法更改它。
现在您的委托(delegate)是空的。
不清楚您是如何访问 ModalViewController 的。如果您正在使用 segues:
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)
Storyboard标识符:
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
来设置委托(delegate)。
关于ios - 从 Swift iOS 中的模态视图中获取值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29462380/