mirza
mirza

Reputation: 77

how to create a generic popup view controller in swift iOS

how to create a generic popup view controller that can be called by multiple view controllers with different data. I have created a popupviewcontroller class with a label and a button. Label and button will have different text based on call from different viewcontrollers. In short what is proper and feasible way to create a generic pop up view that can be used by multiple viewcontrollers

class CustomPopUpViewController: UIViewController{


@IBOutlet weak var vWCustomSubVw: UIView!
@IBOutlet weak var lblHeadingText: UILabel!
@IBOutlet weak var lblDescription: UILabel!
@IBOutlet weak var btnCustom: UIButton!

var strLblHeadingText = String()  // string for heading label
var strLblDescription = String() // string for description label
var strBtnCustom = String()// string for custom button

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = UIColor.black.withAlphaComponent(0.2)

    self.showAnimate()



    lblHeadingText.text = strLblHeadingText
    lblDescription.text = strLblDescription
    btnCustom .setTitle(strBtnCustom, for: UIControlState.normal)

}

 func showAnimate()
{
    self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
    self.view.alpha = 0.0;
    UIView.animate(withDuration: 0.25, animations: {
        self.view.alpha = 1.0
        self.view.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
    });
}


func removeAnimate()
{
    UIView.animate(withDuration: 0.25, animations: {
        self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
        self.view.alpha = 0.0;
    }, completion:{(finished : Bool)  in
        if (finished)
        {
            self.view.removeFromSuperview()
        }
    });
}


}

and i am calling it from another viewcontroller like this:-

 func btnInfoTapped(){

    let customPopUpVC = UIStoryboard(name: "Course", bundle: nil).instantiateViewController(withIdentifier: "CustomPopUpViewController") as! CustomPopUpViewController
    self.addChildViewController(customPopUpVC)
    customPopUpVC.view.frame = self.view.frame
    self.view.addSubview(customPopUpVC.view)
    customPopUpVC.didMove(toParentViewController: self)



}

So i want to make it generic, say it a global method or something to call same class from different Viewcontrollers

Upvotes: 1

Views: 3911

Answers (2)

huynguyen
huynguyen

Reputation: 7770

I think this pod will help you EzPopup (https://github.com/huynguyencong/EzPopup). You can show your view controller as a pop up, easily:

// init YourViewController
let contentVC = ...

// Init popup view controller with content is your content view controller
let popupVC = PopupViewController(contentController: contentVC, popupWidth: 100, popupHeight: 200)

// show it by call present(_ , animated:) method from a current UIViewController
present(popupVC, animated: true)

Upvotes: 0

Uma_Shanker_Tiwari
Uma_Shanker_Tiwari

Reputation: 463

You can create a common method to present alert controller

import Foundation
import UIKit

class AlertHelper: NSObject {

//Alert with title and dismiss button only
static func showAlertWithTitle(_ conroller: UIViewController, title: String, message: String = "" ,dismissButtonTitle: String, dismissAction:@escaping ()->Void) {

    let validationLinkAlert = UIAlertController(title:title, message:message, preferredStyle: .alert)
    let dismissAction = UIAlertAction(title: dismissButtonTitle, style: .default) { (action) -> Void in
        dismissAction()
    }

    validationLinkAlert.addAction(dismissAction)
    conroller.present(validationLinkAlert, animated: true, completion: nil)
}

//Alert with title with message
static func showALertWithTitleAndMessage(_ controller: UIViewController, title: String, message: String, dismissButtonTitle: String, okButtonTitle: String, dismissAction:@escaping ()-> Void, okAction:@escaping ()-> Void) {

    let validationLinkAlert = UIAlertController(title:title, message:message, preferredStyle: .alert)

    let dismissAction = UIAlertAction(title: dismissButtonTitle, style: UIAlertActionStyle.default) { (action) in
        dismissAction()
    }

    let okAction = UIAlertAction(title: okButtonTitle, style: UIAlertActionStyle.default) { (action) in
        okAction()
    }

    validationLinkAlert.addAction(dismissAction)
    validationLinkAlert.addAction(okAction)

    controller.present(validationLinkAlert, animated: true, completion: nil)

}
}

Call this function from your Viewcontroller

AlertHelper.showAlertWithTitle(self, title: message, dismissButtonTitle: "OK") { () -> Void in

    }

Upvotes: 1

Related Questions