Get value from Modal View in Swift iOS

痴心易碎 提交于 2019-12-11 09:53:28

问题


i'm trying to start writing Swift and i'm trying to get a value from a modal view controller with no luck.

I have two controllers, the ViewController and modalViewController.

In ViewController i have a UITableView and with a press of a button i open the modalViewController.

Then from a UITextField i pass the value. I have implement a protocol with delegate and func but somewhere i'm missing something or had it wrong.

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

}

I have no errors in the code, the delegate is not working, it's always nil.

Thanks.


回答1:


You have to assign the delegate in your first view controller. Also, you have to change let delegate: modalViewControllerDelegate? to a var, or else you can't change it.

Right now your delegate is empty.

It's unclear how you're accessing ModalViewController. If you're using segues:

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

Or if you're doing it programmatically:

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

Storyboard identifier:

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

EDIT:

If you want to access ModalViewController by selecting a cell you need the tableView: didSelectRowAtIndexPath method:

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

Using this, you'll need the method prepareForSegue to set the delegate.




回答2:


You have to set your modalViewController's delegate property before presenting it. If you're using segues, you can do this in prepareForSegue(_:).

Also, class names should begin with uppercase letters (modalViewController should be ModalViewController). Only instances should begin with lowercase letters.




回答3:


Another option, instead of using delegation, is to use an unwind segue. Here's a tutorial: http://www.cocoanetics.com/2014/04/unwinding/

In your case, in your presenting view controller you could have the method:

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

And then just link up everything in the Interface Builder as shown in the tutorial.



来源:https://stackoverflow.com/questions/29462380/get-value-from-modal-view-in-swift-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!