简体   繁体   中英

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

尝试这个:

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

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.

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM