Ryan Hampton
Ryan Hampton

Reputation: 319

Swift: How to pass information to a pop up view?

I am trying to pass information to my custom PopUpViewController using the github extension (https://github.com/Orderella/PopupDialog). The Popup uses a viewcontroller I've named PopUpViewController(with a xib file), and the view controller that the PopUp is initiated from is called MainViewController.

The information passed to the PopUpViewController will be an array(named: popUpArray) of type String and used for displaying the contained information within a table (named: tableView).

MainViewController code:

func showCustomDialog(_ sender: UIButton) {

    // Create a custom view controller
    let PopUpVC = PopUpViewController(nibName: "PopUpViewController", bundle: nil)

    // Create the dialog
    let popup = PopupDialog(viewController: PopUpVC, buttonAlignment: .horizontal, transitionStyle: .bounceDown, gestureDismissal: true)


    // Create second button
    let buttonOne = DefaultButton(title: "Ok", dismissOnTap: true) {

    }

    // Add buttons to dialog
    popup.addButton(buttonOne)

    // Present dialog
    present(popup, animated: true, completion: nil)
}

PopUpViewController Code:

 import UIKit

class PopUpViewController: UIViewController {

@IBOutlet weak var imageView: UIImageView!

@IBOutlet weak var titleLabel: UILabel!

@IBOutlet weak var tableView: UITableView!

Upvotes: 0

Views: 2681

Answers (2)

Reginaldo Costa
Reginaldo Costa

Reputation: 879

Create a convenience init in PopUpViewController like following

convenience init(nibName: String, arrayOfString: [String] ){
  self.data = arrayOfString
  self.init(nibName: nibName, bundle:nil)
}

Then on MainViewController call the convenience init you just created like this something like this

// Create a custom view controller let PopUpVC = PopUpViewController("PopUpViewController", arrayOfString: ["String1", "String2"])

Upvotes: 0

dirtydanee
dirtydanee

Reputation: 6151

Just declare a new variable on PopUpViewController called data with type Array<String>.

After this, when you are creating your viewController, you can just pass it to the controller. After that it is just a simple tableView implementation in PopUpViewController to display the data.

Extend PopUpViewController with data parameter.

import UIKit

class PopUpViewController: UIViewController {

 @IBOutlet weak var imageView: UIImageView!
 @IBOutlet weak var titleLabel: UILabel!
 @IBOutlet weak var tableView: UITableView!
 // Data variable
 public var data: [String] = []

}

Add the data upon calling showCustomDialog() function

// Create a custom view controller
    let PopUpVC = PopUpViewController(nibName: "PopUpViewController", bundle: nil)
 // Assign the data
    PopUpVC.data = popUpArray

Upvotes: 3

Related Questions