【问题标题】:Get value from Modal View in Swift iOS从 Swift iOS 中的模态视图中获取价值
【发布时间】:2015-06-10 07:49:06
【问题描述】:

我正在尝试开始编写 Swift,我正在尝试从模态视图控制器中获取一个值,但没有成功。

我有两个控制器,ViewController 和 modalViewController。

在 ViewController 中,我有一个 UITableView,只需按一下按钮,我就可以打开 modalViewController。

然后从 UITextField 我传递值。 我已经实现了一个带有委托和函数的协议,但是在某个地方我遗漏了一些东西或出错了。

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.
}

}

我的代码没有错误,委托不工作,它总是为零。

谢谢。

【问题讨论】:

    标签: ios swift delegates protocols


    【解决方案1】:

    您必须在呈现之前设置您的modalViewControllerdelegate 属性。如果你正在使用 segues,你可以在prepareForSegue(_:) 中执行此操作。

    此外,类名应以大写字母开头(modalViewController 应为 ModalViewController)。只有实例应该以小写字母开头。

    【讨论】:

      【解决方案2】:

      您必须在您的第一个视图控制器中分配委托。 另外,你必须将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 来设置委托。

      【讨论】:

      • 要访问模态视图,当我按下 ViewController 的按钮时,我正在使用 segue。
      • 这个按钮,这只是你的 ViewController 中的另一个按钮,你将 ctrl 拖到了 ModalViewController 中吗?从您最初的帖子中,听起来您想单击一个单元格并打开 ModalViewController。
      • 是的,它是一个我 ctrl 拖动到另一个视图并将 segue 设置为模态的按钮。我刚刚找到了一个很好的教程,涵盖了我的问题,所以我会从那里开始。 raywenderlich.com/81880/storyboards-tutorial-swift-part-2
      【解决方案3】:

      另一个选项,而不是使用委托,是使用展开转场。这是一个教程: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 中的所有内容链接起来,如教程中所示。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-03-02
        • 1970-01-01
        • 2016-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多