3

I am making a class for sharing. This class will be used many times in my project. So I have defined many methods there with a parameter that specify refrence UIViewcontroller on which i want to present something like SLComposeViewController. I just want to ask one thing that is it normal to pass a UIVIewcontroller as parameter or it is not recommended to pass UIVIewController as parameter.

// ViewController Class
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func shareFacebook(sender: UIButton) {
let sharePanelObj = SharePanel()
sharePanelObj.facebookShare(self)
    }

    @IBAction func shareTwitter(sender: UIButton) {
    }
}


// Reusable Base Class
class SharePanel: NSObject {

    func facebookShare (refrenceViewController:UIViewController) -> () {
        if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook) {
            let fbShare:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)

            refrenceViewController.presentViewController(fbShare, animated: true, completion: nil)

        } else {
            let alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.Alert)

            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
            refrenceViewController.presentViewController(alert, animated: true, completion: nil)
        }
    }
}
ankit
  • 3,537
  • 1
  • 16
  • 32
  • 2
    Show the code which explains what you're doing and why you're passing it. Is it a controller passing itself so something can be presented from it? – Wain May 06 '16 at 06:50
  • do you want to create base class for application which can be useful for calling same method in different controller. like that? – Jigar Tarsariya May 06 '16 at 06:52

2 Answers2

4

I don't see any issues with sending your view or viewController as a parameter for this use but it might be cleaner to add those methods as extensions to UIViewController and then the you can just act on self instead of the parameter.

One thing to consider might be the memory management if you keep a reference to the view controller. That might screw a bit with the load and unload of the views that are often relied upon to setup the views.

Moriya
  • 7,750
  • 3
  • 35
  • 53
2

1) Create one controller with name like, BaseViewController.

2) Write all common methods which you want reuse in other controller.

3) Then give this base view controller as a parent view controller for that view controller where you want to use their methods. like,

class HomeViewController: BaseViewController
{

}

you can give this base class to all controller where you want to reuse that methods. This way you can achieve this base class concept. Hope this will help to you.

Jigar Tarsariya
  • 3,189
  • 3
  • 14
  • 38
  • 1
    Avoid subclassing as much as you can. This code will become very hard to maintain after it grows and as a result you will deal with chain of subclasses and unused methods. – RealNmae Jun 08 '19 at 11:15