Android teem
Android teem

Reputation: 830

iOS how to start a viewcontroller from framework

I have created my private framework successfully. It has no error. I have created a project now I want a simple thing but I do not have clue. Following is what I want

I want to start the Framework view controller. You can think it is the whole app. I want to start its first view controller. and then the app flow is as per business logic. I want to exit app when user exit the Framework main view controller. Actually it was the complete app but I decided to make it framework for different clients because only small modifications are needed for different clients but I do not know how it should be done.

here is how I am trying to start first view controller in my Client project but it does not recognize that controller.

 @IBAction func onClickBtnStartOver(_ sender: Any)
 {
     let storyBoard : UIStoryboard = UIStoryboard(name: "Storyboard", bundle:nil)            
     let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("idFrameworkVC") as FwViewController
     self.presentViewController(nextViewController, animated:true, completion:nil)
 }

It gives me error like "Use of undeclared type 'FwViewController'"

So what should I do to start the first viewcontroller of framework.

Note: I have viewcontrollers in one Storyboard namely Storyboard inside Framework

Upvotes: 2

Views: 4187

Answers (5)

bhushan deshmukh
bhushan deshmukh

Reputation: 75

In case if you don't want to make your view controller public/open , and still want to open it from client app ,here is the code.

Inside your private framework ,publicly extend the view controller and write function

public extension UIViewController {
func presentMyController() {
        let frameworkBundle = Bundle(identifier: "your framework bundle identifier")
        let storyBoard : UIStoryboard = UIStoryboard(name: "Storyboard", bundle: frameworkBundle)
        let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("idMyController") as MyController
        self.presentViewController(nextViewController, animated:true, completion:nil)
}}

And in your client app

func onButtonClick(){
  self.presentMyController()
 }

Upvotes: 0

Masoud Roosta
Masoud Roosta

Reputation: 475

use your framework bundle identifier

 let frameworkBundle = Bundle(identifier: "your framework bundle identifier")
 let storyboard = UIStoryboard(name: "your framework storyboard name", bundle: frameworkBundle)
 let mas = storyboard.instantiateInitialViewController() as! UIViewController
 self.present(mas, animated: true, completion: nil)

Upvotes: 2

A.s.ALI
A.s.ALI

Reputation: 2082

If everything is setup nicely then you may have not imported your framework in it. This is very important part. Make double check if You have declared Public your framework VC. Then After it you need to start It in you method.

I will suggest to see this selected answer. And this is the case with you

PS: Do not forget to go through into comments on the selected answer as there is discussion about crash. I doubted that you face them, But if you did encounter any crash then fix the issue using that comment in answer

Upvotes: 0

Sagar D
Sagar D

Reputation: 2618

Declare an open func in your framework for opening first viewController :

open func presentFirstViewControllerOn(_ viewController:UIViewController) {
    let storyBoard : UIStoryboard = UIStoryboard(name: "Storyboard", bundle:nil)
    let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("idFrameworkVC") as FwViewController
    viewController.presentViewController(nextViewController, animated:true, completion:nil)
}

and call this method from client app's VC. Deciding which is the first VC (if needed) should be the responsibility of the framework, not your client app.

Upvotes: 2

A. Amini
A. Amini

Reputation: 561

Try this:

          UIApplication.shared.keyWindow?.rootViewController =  UIStoryboard(name: "yy", bundle: nil).instantiateInitialViewController()

Upvotes: 1

Related Questions