I would like to have my Swift Project load it's main Storyboard and View Controller from a Framework rather than the project itself.
Is this possible?
All help welcome.
I'm sure you have long since resolved your issue, but posting the answer here in case it's useful to someone else coming across it from Google.
You can specify another storyboard as the main storyboard by selecting your project from the navigator, selecting the app target from the left panel and going into the 'Info' tab. There is a setting there for "Main storyboard base file name."
For your specific issue, that's probably not what you want though. Instead, open your app's Main.storyboard, highlight the view controller, open the Identity Inspector, and set the Class to the one from your framework. To get this to work for my project, I actually had to delete the existing Main.storyboard, create a new one, drop a View Controller on it, and set "Is Initial View Controller" from the Attributes Inspector first.
Updated answer :
Let's assume you have a framework called myLogin with a storyboard with id "frameworkStoryboard" and a view controller with id "login".
You would
import myLogin
to load your framework and then put in your code
let storyBoard = NSStoryboard(name: "frameworkStoryboard", bundle: nil) as NSStoryboard
let loginViewController = storyBoard.instantiateControllerWithIdentifier("login") as! NSViewController
view.window?.contentViewController = loginViewController
to load your storyboard and corresponding view.
Basically you could use variables for both names (storyboard and controller) instead of just writing it down.
The example is for osx, the only difference for iOS would be using UIViewController instead of NSViewController, same goes for the storyboard, also you would not present the controller the same way on iOS (this example will switch the current windows content view instead of presenting it.
Related
Learning Swift as my first new language in many years, I've come across something I'm curious about using Xcode and creating a new iOS project using the single view template.
In the default ViewController.swift file, UIkit is imported, and then the class ViewController is defined, inheriting from UIViewController. But I can't seem to find out where or how this class is ever created or initialized as an object.
Default ViewController.swift example
If I define my own class, or even want to create another view controller, I must first initialize it as an object somewhere in order to use it. So where is this default ViewController getting made?
Thanks for any help you can offer for me to try to conceptualize this!
Open your "Main.storyboard" file. Make sure View Controller is selected on the left sidebar:
Then on the right sidebar you can see that your ViewController class is in the class field:
So, when your app is loaded, the default storyboard is loaded, and that storyboard is responsible for creating an instance of the ViewController class and set it up for you. To test this out, you could create a new class, say ViewController2 and make it inherit from UIViewController. ViewController2 would then be available in the right sidebar:
And then your ViewController2 code will be used instead of ViewController.
You have changed the location of the ViewControllerQuestion.app file
I believe that the UIApplicationDelegate takes care of the creation of the initial view controller. You still can manually create view controllers as you mentioned and segue to them.
Normally you can create a view controller in a storyboard and use the Interface Builder to set up a segue to that view controller.
I hope this answers your question.
I am developing a swift application, which uses a Walkthrough screen as initial view controller when the app is launched for its first time.
I have a skip button there, which should dismiss it when the user clicks on it. The Problem occurs with the action of the button, where I have a code and inside the code I have to assign a name for the first TabBarViewController (the main screen in the app). Please look at the picture:
Would you please help me how to give a Class Name to this initial TabBar Controller,I successfully created a Storyboard ID in my case(InitialViewController), but I can't give a class name, look at the picture:
I have never done this before, I tried to create a new CocoaTouch file and assign a name TabBar Controller, but when I go to the Main StoryBoard and try to type the name inside, it is not recognised. Any ideas how to assign a class name to it ?
It looks like your TabBarController is a regular UITabBarController
In that case your code should look like this:
let nextView = self.storyboard?.instantiateViewController(withIdentifier: "InitialViewController") as! UITabBarController
// force unwrapping for brevity
You must have selected it to be a subclass of UIViewController, however, right now you want it to be a Sub-class of UITabViewController.
I'm currently developing a framework in Swift that includes a storyboard that I designed myself. What I need to do is that the user that uses my framework can create a segue to my storyboard. Then I'd like to be able to recreate a segue to the user storyboard that that I "leave" my storyboard.
How could I do so ?
Thx !
Your question is vague and I am not entirely sure what you are asking. However, I can't comment yet so I will try to answer with what little I know. It seems like you are asking how to transition storyboards. The simplest way is using a UIButton in your storyboard. Open up storyboard and go to the navigation controller. Then, add a button to the Nav Controller. Right click (or control click, depending on your settings), and drag the line to the main storyboard. Lastly, click on the show option. When a user clicks on the button, it will immediately switch to the main view controller. Don't forget to add the button in the view controller. Hopefully, this is what you meant and helps. If you wish switch view controllers in code, this is a duplicate and you should go here: How to switch view controllers in swift?. If this doesn't answer your question, comment and I will try and help. For future reference, please provide more information: snippets of code, pictures, anything that better demonstrates your problem.
I fixed it by getting the instance of a new view controller added on the user storyboard with the view controller identifier like so :
ProcessOut.backViewController = storyboard?.instantiateViewControllerWithIdentifier("back")
And then I present it like I would present a normal viewController from my storyboard.
I want to use generally the old .xib files in my iPhone application. But when it comes to tableViewController storyboard is a lot more convenient in order to make custom cells etc. Is it possible to make a .xib based application and in the middle of it, to use a storyboard for a UITableViewController and its DetailedViewController only?
You can use a storyboard for any part of a program. Storyboards are not an all or nothing concept. You can have just one view controller in a storyboard, or a small network that just represents a subsection of your app.
To use just one view controller from a storyboard:
Add a new storyboard to your existing project.
Add a single view controller to the storyboard.
Assign an identifier to the view controller in the inspector.
Use the UIStoryboard class to load the storyboard resource
Use -[UIStoryboard instantiateViewControllerWithIdentifier:] to create a new instance of that view controller.
Install the view controller some place in your app by doing something like pushing it on to a navigation controller, or run it modally.
Both can work fine together (Storyboards and Nib files). In the TVC that is part of your storyboard, just instantiate the destination VC in code and use the usual initWithNibName method to load the nib file.
You can add a storyboard to any project, but the point of storyboards is to centralize your XIB files into one location rather than having 10 XIB files you can have 1 .storyboard file that contains 10 scenes representing your views. This shows your connections to other scenes, and you can manage all the seques and transitions of each scene. So is it possible, yes you could add a storyboard to your project, but I would recommend you design you entire application in a storyboard if you want to use them.
I have an application, say 'MyApp', which by default loads the view controller 'MyAppViewController' whenever the application launches. Later, I added a new view controller 'NewViewControler' to the project.
I now want the 'NewViewController' to be my default view controller which loads when the app launches.
Please let me know what changes I need to make in my project to achieve this.
Its easy, just:
Open your Storyboard
Click on the View Controller corresponding to the view that you want to be the initial view
Open the Attributes Inspector
Select the "Is Initial View Controller" check box in the View Controller section
Open MainWindow.xib and replace MyAppViewController with NewViewController.
In your app delegate class, replace the property for MyAppViewController with one for NewViewController. Connect NewViewController to its new outlet in Interface Builder.
In application:didFinishLaunchingWithOptions: add NewViewController's view to the window instead of MyAppViewController's view.
Most likely your main NIB file is still set to "MainWindow", check your *-Info.plist file.
If that's the case you can open the MainWindow.xib in Interface Builder. You should see a View Controller item. Bring up the inspector window and change the Class Identity to point to your new class. That should take care of instantiating your class.
As this feels like a "newbie" question (please pardon me if I'm mistaken) I would also highly recommend the following article:
iPhone Programming Fundamentals: Understanding View Controllers
Helped me understand the whole ViewController thing and the IB interaction..
As for me with xcode 4.3.3, all I had to do was simply replace all references of 'MyAppViewController' with 'NewViewController' in the AppDelegate h and m files.
Perhaps all the other steps have been taken out in the newer versions of xcode.
Hope this helps.