ios - 从 Swift iOS 中的模态视图中获取值(value)

标签 ios swift delegates protocols

我正在尝试开始编写 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/

相关文章:

ios - Tableview 搜索栏未正确返回 detailTextLabel(Xcode - Swift)

ios - swift 未转义的控制字符

android - 如何在一个 Firebase 项目中使用两种不同的语言?

swift - 在 Swift 中将 GPS 元数据写入 EXIF

c# - 如何取消 C# 3.5 中的异步委托(delegate)?

ios - Apple App Store 提交 - 模拟账户

objective-c - WatchKit 中 UIApplication.sharedApplication().delegate 的等价物是什么?

swift - 在 Swift 中将文本或数据 append 到文本文件

C# - 重写对 Action<> 的委托(delegate)调用

c# - 使用带参数的委托(delegate)