I have One storyboard with different/multiple UIViews and I want a ViewController for each UIView. Swift - ios

I have one storyboard with different views, I do not want all the codes in ViewController.swift I would rather have a Swift for each UIView.
Is it possible? and how do I do?

Use UIContainerView instead of just UIView. When you drag a UIContainerView into your storyboard it'll automatically create a View. Just make a new Cocoa Touch Class file in your project. Make sure the file is put under UIViewController
Then click on the new View that was automatically generated, go to Properties, then select your new Swift file. This connects the View to the file.

Related

Cannot control-drag in xcode between cocoa touch uiview class and subview in storyboard

I'm new to xcode and iOS.
I'm following apple tutorial, but I can't figure out a thing.
In the tutorial, when there is the need of a connection between the storyboard and the ViewController.swift, it's done a connection using the control-drag from the storyboard to the viewController.swift, and it works like a charm.
If I add a subview to storyboard, apple does the connection and the declaration by code in a custom cocoa touch class that implements UiView Protocol, not using the control-drag.
Now, given the fact that is clear to me the code writted by apple, I'm just wondering why the control-drag doesn't work.
I've tried to do the same thing, adding a button via ui and then tried to control-drag to cocoa touch class, and I cannot do this.
But if I do the same thing, but trying to connect it to the ViewController.swift it works perfectly.
Now, I'm just wondering why this happens.
Can't I use control-drag with a custom UiView?
Thanks for answering.
You can only control-drag from outlet to view on xib, after creating outlet in code you will see a dot at the left side, you can drag from there to your view and outlet will work
In the storyboard, you have to set the class name to the view controller.
If you want to create a custom view that will have subview of it's own, you can do so using a Xib file (pretty much like a storyboard, but for only one view instead of for a whole app workflow) and then you'll be able to make all the connection you wish. Storyboard is not meant to design your customView. Connection are mainly used to provide a easy/cost-effective reach from the control grasp to it's main view's subviews (IBOutlets) or to '0 line' a target action pattern. One exception is with the custom tableView / collectionView cell though..
For connectioning between storyboard & class
you must in the
storyboard -> customClass -> chose your class

how to work with multiple viewcontroll and use the viewcontroll.h and m file

I am new with xcode.
I used one viewcontroller and coded it in the viewcontriller.h and .m files.
My problem is when I add more viewcontrollers, I can't create more action from the new viewcontrollers to the basic files (viewcontroller.h and .m).
For example to right click and drag swipe recognizer from new viewcontrollers to the viewcontroller files (h&m files) in interface builder.
As my comment seems to be the answer, I'll add it as an answer:
I suspect you do want to set the class of the newly added view controllers in interface builder to the class of the view controller you want it to be.
Then, you can drag-connect like with the first one, xcode did set up for you initially.
Also, do remember to use different view controllers for different purposes (you might have to create another subclass of UIViewController).

Loading a custom view in a storyboard from a separated nib file

I've customized an UIView in its own .xib file in order to being able to reuse it in several scenes of my storyboard. It is only a view with no view controller behind. I'm trying to load it in the storyboard and to see it in Interface Builder in the scenes I want to place it, with no success. I've been looking for a way to do that and I didn't found a post with an answer that worked for me.
I'm using Xcode 6.3.2 and my app is targeting iOS 7 and above. Could somebody help me?
Thanks in advance
What I do is make a subclass of UIView with its xib (CustomView.h, CustomView.m and CustomView.xib). And add a UIView to your controller on storyboard. Then assign its class to your custom view class.
This works without loading custom xib via code. But you cannot see the subviews of your custom view on storyboard.

IOS Custom UIView doesn't show

I'm sorry but i'm still a bit dumb in UIViews and creating a custom one.
I have a custom view and a xib file that are connected through the XIB IB.
I want to add this view to the storyboard.
What you see in this picture is an empty view which i've connected to the custom view class
When launching the app - i see the exact same view without my custom view being loaded.. what am i missing ?
It is not enough to just link your xib view to the storyboard through the view class name. You should copy the view from your xib (open your xib, select the root view and copy) to your storyboard view controller (select the parent view and paste). You can even open (double tap the xib file in the navigator) your xib in a new window and do a drag drop to the story board. Note that all the property wirings and constrains that you had defined in the xib will be retained when you copy paste your view from xib file.
The answer is actually pretty simple.
I had a recursive init process - meaning that although i did over initWithCoder method, in the xib file, i've set the view class to be my class.
What i should have done is to make my xib file's owner to be my class instead.
And then all the loadFromNibName worked perfectly in my initWithCoder!

Adding Outlets to View Controller via Table View Controller

I'm attempting to create an application for iOS, using Xamarin and storyboards. Within this, there shall be a button located in a TableViewController which will navigate the user.
To do this, I've used a Container, embedded to a TableViewController. However, my concern is I wish to add an Outlet from the button within the TableViewController to the ViewController header class - Yet it only allows me to try and add one to the 'UITableViewController.h' file.
What is causing this and how am I able to add the Outlet to the 'ViewController.h' file? (Extremely sorry if this makes little sense)
You should not use both the UITableViewController and the UIViewController. UITableViewController is a subclass of UITableViewController so all you need the UITableViewController and you should drag the outlet to it.

Resources