When using storyboards with Swift, how do I also include code? - ios

I often look up questions about Swift regarding formatting and Views, and answers I find will include lines of code. If I am using storyboards, where should I look to include code?
Thanks in advance.

You include code to it's corresponding class.
let's say you're using a UIViewController in storyboard:
make a class of type ViewController,
and put the class name for it
now both the class and the storyboard viewcontroller work together. and you can just add code in that class...

Adding onto slimboy's answer, if you are beginner, you might forget to connect the IBOutlets/IBActions. Here is how.
You switch to double pane view with the storyboard on the left and the view controller on the right.
Then ctrl click from the view element to the top of the view controller
Name you view whatever (myTitleLabel).
Now you can use code to refer to the myTitleLabel. IBAction works the same, but make sure to change the option to Action (default is outlet in Swift).

Related

IBAction issue in Xcode StoryBoard

I'm a newbie in coding and I'm learning Swift to develop my first application.
I'm following this YT tutorial trying to integrate a SideMenu :
https://www.youtube.com/watch?v=iq-tWW45Vhk
The fact is that after I added the #IBAction func into my code like this:
I do not find the received action I create in the code like in the video at 4:47
I found out that this appears only in my first ViewController (the blue screen) of my StoryBoard but I want the function to appear on the top right view controller
Can someone help me?
Thx
Julien
For all viewControllers in story board you have to create separate swift class files, and create it's IBAction outlet in it's class file.
add viewController class here :
after this you will be able to create IBOutlet and IBActions in that class.
I found out that this appears only in my first ViewController (the blue screen) of my StoryBoard but I want the function to appear on the top right view controller Storyboard
You need to put the action in the view controller class where you want it to appear. If it's showing up somewhere else, it's in the wrong class.

Adding Multiple View Controllers to ViewController.swift From Storyboard UI

I have created several view controllers that I wish to now add tap actions to in my Main.storyboard, but they are not connected to the ViewController.swift. I am trying to connect the ViewControllers via the control-click-drag-to-swift-file method, but the object does not appear in the swift file. Is there a way to simply import your entire storyboard with all the ViewControllers into the swift file so you can start coding?
Thank you.
If I understand correctly, you have multiple view controllers all with the same class "ViewController" that you want to duplicate and edit all the same way? You have to label each class differently or else the system crashes its like having twins and naming them all the same name and getting confused why they dont know which one you are talking to.

How can I link an outlet from a view controller to an other?

I am working on an app that uses parse so I used the "starter project" as a base and worked from there.
The issue I am facing is that the ViewController is controlling the login screen a well as others such as the tableView and mapView witch I added later.
As this is the case if it would be possible I would link the map outlet by simply dragging from the code to the map but obviously this is not possible, How could I solve this problem (I understand I may be looking at the problem the wrong way but any help would be appreciated)
here is the code with the map outlet
here is what the layout looks like
The MVC model, Model-View-Controller model, isn't intended to have an action in one view touch the controller of another view. In InterfaceBuilder, you should only ever be able to attach actions to the controller for that specific view.
In general, if you set the file owner to ViewController, then you can only link IBoutlets to that view controller not make to another one.
your map is available in your MapViewController not ViewController, so you need to give the reference/IBoutlet of map need to assign the MapViewController, if you want to implement in ViewController, you need to create new one map
No you have to create different file for each controller.
you cant add outlet of all in one controller

iOS Development: Is it necessary to change the class of the View Controller in the storyboard?

I am currently learning iOS development with Objective C. I am facing a couple of issues and I was wondering whether someone could give me some clarifications about xCode. I was reading a book on iOS and I was following it's instructions.
It was saying to create a header and an implementation file with the name test.h and test.m respectively. The test class will have UIViewController as superclass. This is all very fine...
Then, from the instructions, it says to drag out a View Controller from the object library onto the storyboard. Then, the book says:
"Now that the view controllers is in place, it’s time to set it up with the correct controller class. Select the view controller and bring up the Identity Inspector. In the Custom Class section of the inspector, change the class to test, and press Return to set it."
My question is: Is it really important to change the class of the view controller to test? Why can't I let the class remain as UIViewController? If I let the class remain as UIViewController, will the outlets and actions created on the view controller in the storyboard not be able to 'communicate' between the View Controller in the storyboard and the test.h and test.m files?
Thanks for reading
Kindest Regards
Yes, it is imperative that you set the class. Fortunately Xcode will not allow you to add outlets in the storyboard if the class set does not define them. If you don't set the correct class then the wrong class will be instantiated at runtime and your custom code will not run.
Keep in mind that the storyboard contains an archive of your design which can be unpacked and used at runtime. It doesn't work automatically and it needs to be configured properly. If, for example, you were to add outlets and actions pointing to an instance of UIViewController instead of test (whose name should start with a capital letter) then you would get an exception at runtime when the archive is unpacked and it is found that the outlets can't be connected, because UIViewController doesn't define them.
YES, it is mandatory. In addition to other answers posted here, I would like to tell you to think over this thing:
Suppose you have more than one Class & ViewController in your demo
project, how do you expect XCode to assign proper Classes to specific
ViewControllers?

IOS Why does a view have 2 Custom Classes?

Probably quite a simple question but I have not seen it addressed so far (I am a ios newbie)
So I start Xcode and create a simple (leave it Blank) view controller in Storyboard.
Click at the bottom so that it highlights in blue allows me to assign that view to a Controller that I am creating.
Thats found under the Custom Class field in xcode
Now if I click anywhere inside that blank view I see that Custom Class reference has changed.
Its now reffering to something else and not my Custom Controller that it was just pointing to.
What is this custom Class a reference to ?
What do I need to use if for ?
Does my view have 2 custom classes associated to it ??
Anyway as you can see I am confused by this.
Thanks !
When you click inside the controller, you're clicking on the controller's view, not the controller itself, so you're seeing its class (which should be UIView if you haven't changed it).

Resources