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.
Related
Background
I am developing an application which contains a top nav bar with an icon that allows to go back to the main ViewController.
As there as several ViewControllers, I don't want to duplicate the segue on each ViewController. I would prefer to create a custom class for the top nav bar and be able to programmatically present the main ViewController. I found that I could maybe call present(viewControllerToPresent:animated:completion)
Questions
Is this the right way to do it?
How do I access the controller I need to pass to this function?
Is there a method which allows to get the controller from its title?
Also, as the main ViewController has already been instantiated when the application started, do I need to instantiate a new one or can I get a reference to the existing one?
So if I get this correct you click mainVC->firstVC->secondVC->nthVC and then you'd want to go directly back to mainVC ? View controllers are stacked so what I'd do is just dismiss all those view controllers that are above mainVC.
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 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.
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'm trying to create a second view controller and link it to a new set of .m & .h files then alter the text of a label on that view controller under the viewDidLoad but am having some issues.
Here's what I am doing so far.
I'm starting with a blank new single page, from the MainStoryboard, I add a new VC and add buttons so I can navigate between the pages. This works fine.
Next I create a new Objective-C Class, "SecondViewController" and choose subclass UIViewController.
Now back on the Storyboard I select the other ViewController and try to pick the new SecondViewController as a Custom Class but it doesn't show up in the list of available classes.
Now, if in step 2 I choose the UIView subclass I can assign it to the ViewController in step 3 but the viewDidLoad options are not available so I am not sure how to program changes to the VC.
What am I missing here?
I am able to use the UIView and use the following to make the change I am asking about. I am not sure if this is the correct way to do this.
- (void)drawRect:(CGRect)rect
{
myLabel.text = #"change2";
}
EDIT ----
Okay, I've figured it out. I was clicking on the VC's View (background) in the Storyboard and not the actual ViewController! Now it works the way I thought it should. I suppose if I had looked at the View Controller Scene I would have noticed what I was doing incorrectly.
Now, if in step 2 I choose the UIView subclass I can assign it to the
ViewController in step 3 but the viewDidLoad options are not available
so I am not sure how to program changes to the VC.
The class of your ViewController (or similar one) from interface builder and another class in your code must extend the same class