Connecting the view controller to the code in swift - ios

When I create two view controllers in the main story board, how do I connect the second one to code?
An example would be:
I put a label in the first view controller called myLabel
I put another label in the second view controller called myLabelTwo
Now, the first view controller can connect to the code easily, but the second view controller 's myLabelTwo cannot be connected.

Create new UIViewController class, ViewController2 for example
In your storyboard, select your second view controller and on the right side menu click on "Show the identity inspector"
Change the Class content to "ViewController2" or whatever else.
You can now connect "myLabelTwo" to your "viewController2"
Please check this link to understand storyboard and view controllers.

Related

I can't connect the IBAction of UIButton into the ViewController class

When I try to link the UIButton object to the ViewController class in order the create an IBAction, Xcode doesn't show me the window for creating it.
I have to say that It only happens with the second ViewController of the NavigationController, because with the first one it works.
I believe it should be a Xcode bug...
The situation is the one below:
In order for CTRL-drag to work, the ViewController must be told which ViewController class to use.
Once that is done, you can use split view and Control-drag from the button to the code. Xcode will not let you Control-drag to the wrong file.
You cannot control-drag from a button on View Controller 1 to the class file for View Controller 2.
I'm not sure if I see it all in the right way, but you have code for one view controller, which is called ViewController. In this view controller you have an action method for the button via avanti and an action method for the button via indietro. However, the buttons are not in the same scene of your story board. You have the first scene with the button via avanti linked with the ViewController.swift file (Controller initial scene), but the second scene (Controller finale scene) is not linked to your the ViewController.swift file, which it shouldn't. You have to create a new view controller file for every scene in your storyboard and put the action methods in the view controller files that are linked with the appropriate scene. It is also preferable not to call your view controller swift files ViewController.swift, but give them a meaningful name.
Hope this helps.
Kind regards,
MacUserT
the thing you are trying to add is the self.view which is already added in UIViewController Class. try adding another control like button or another UIView

Swift3, How to use same UITableView in different viewController

In my project, I have table including many cell prototypes, outlets and constraints already in xViewController class.
I would like to know how if I want to use this table again in yViewController class.
Is it possible or I have to create the new one?
You can use ContainerView for the same. You can take a separate UITableViewController or UIViewController as per your requirement.Let called this class CommonTableViewController.
In your XViewController , remove tableview and move all code to CommonTableViewController. Drag and drop container view from the object library on XViewController. You can delete pre- defined child of your ContainerView. Now add embed segue between XViewController and CommonTableViewController.
Drag and drop container view on YViewController as well and add segue as mentioned above.
Do let me know if you have any further queries.
Refactor your table view controller to its own dedicated storyboard (In Interface Builder: Select the table view controller, and choose on the Menu bar: Editor > Refactor to Storyboard...).
Make sure your table view controller is the "Initial View Controller" (has an arrow pointint to it from the left, on the storyboard).
Now, on the main view of every view controller on which you wish to display your table view, insert a Container View and replace the target of the Embed segue with a Storyboard Reference to the Table View stotyboard you just created.

(Xcode 7.3.1) Not able to connect subview controllers to view controller.swift

I am trying to make a card game with multiple view controllers. I have created my application in a single view application, and have been trying to use multiple view controllers. When attempting to drag the images/buttons into the view controller code, it will not accept it unless it comes from the main view controller. I have posted screenshots that show my problem
You have to make separate class for for every view controller and you have to assign that class to respective view controller from identity inspector from utilities.
Check the screenshot,
So here setting view controller is class , subclass of viewcontroller. So, create subclass of viewcontroller, then select respective viewcontroller from interface builder and assign that class to that view controller and then open that class in assistant editor and you will be able to connect outlets or ibactions.

I have two view controllers on the story board, I can connect one VC to code in one swift file, but can't connect the second.

So, as my title describes, I can connect one view controller in my story board to the first view controller.swift file but unable to connect the second view controller in story board to my second view controller.swift file. Any reason for this? Am I missing something?
Thanks for any help,
Jon
You can add a ViewController-class to your ViewController in your Storyboard by following the steps below:
But you have to make sure that you have created your class correctly.
To do that do the following:
File->New->File->iOS-Source->Cocoa Touch
At Subclass of you have to choose: UIViewController
Add a name, click Next and Create
Now you can add your created controller like mentioned above.

Unable to make outlets

I'm not able to create an outlet on the view controller.
I started out with the single view application template, and added another view controller to my storyboard. Now, when I try to create an outlet from the second view controller to the file, Xcode doesn't allow me to make it.
What could be the problem?
When you add a View Controller on a story board you have to change it's class to the View Controller you want it to be controlled by, you can do it by opening the Identity Inspector tab in the Utilities panel (option+command+3) and type or find the class name of the View Controller you want, after doing this, you can play with it's outlets, check out the screenshot:
EDIT: Forgot to mention, you have to have selected the View Controller you want to change the class for in the left list.

Resources