Programmatically made view with rest of project in main.storyboard - swift

I have a quick question for you guys that I havent found a definitive answer to. I have a app made with every viewcontroller designed in the main.storyboard. Recently I had to change out my table view viewcontroller for receiving data with collectionView in order to implement some more features.
But I have made this view programmatically. And I have made it exactly how I want it.. When I was done I wasnt sure if I could even use it with my main.storyboard made project. Atleast no similar question I have found has given me the exact answer to that.
So I figured I should ask you guys if thats possible and if it is.. if its a complicated process. This is my bachelor's thesis work so I am working against the clock.
If anyone knows, and maybe even have some tips on how I do this, I'd greatly appreciate it!

If your viewController conforms correctly to all the necessary methods; you should be able to create a viewController in your storyboard; hook it up the way you would any other uiObject; and then set the custom class in the attributes inspector to the class of your ViewController.
If the view and not a viewController; you do a similar thing, drag the conforming type to your storyboard and subclass.
If you'd like to see your view render on your storyboard; have the object declare #IBDesignable before the class declaration.
#IBDesignable
class myView: UIView {}

Just present the programittaclly created view controller from code:
let view = YourViewController()
self.present(view, completionHandler: nil)
Hope this helps!

Related

Cannot add Outlet connection from Button to ViewController

I am trying to create an IBOutlet connection/reference from my button to my UIViewController in Xcode, but it is only giving me the "option" to add an Action connection. I am trying to utilize the viewDidLoad and baselineAdjustment to vertically center the buttons label when the text scales to fit the width, but am not having any luck.
Does anyone have a suggestion or know what I'm doing wrong?
EDIT: Adding a few more screenshots for clarity
If I select my main Scene View, the class is of type ViewController. If I select the view that the Button in question is a part of(it is in a ContainerView), it is of class UIViewController and the options do not present a choice of ViewController.
Make sure the right class subclassing the UIViewController, make sure when you are doing it, you choosing the UIViewController and not the View or the safe area.
Let me know if it solved it
I see the you have multiple ViewControllers in storyboard. Ideally, each View controller in the storyboard is supposed to be of only one type of UIViewController implementation and it's also true the other way around. so, If you have say 3 UIViewControllers in Your storyBoard, then you will need to create 3 .swift files which implement UIViewController like so:
abcVC:UIViewController { .....
efgVC:UIViewController { .....
ViewController:UIViewController { ..... //this is the default one you get.
and then set the class of each ViewController in your storyboard to one of each of the above.
if your ViewController which has the button you want to outlet has a class type abcVC, then you can outlet your button only in abcVc's implementation in abcVC.swift.
Hope it makes sense. to see how to set class, refer #Vadim F. 's answer.
and if you happen to upvote this answer, please also consider upvoting #Vadim F. 's answer.
This is how you can crate a new .swift file while subclassing a UIViewController: File -> new -> File -> Cocoa touch class -> #make it subclass of UIViewController and give it a unique name eg: abcVC
Certify that your ViewController in the Storyboard is of the class ViewController (i.e your view controller) and not from UIViewController.

iOS & Swift: ViewController class is defined, but when is it created as an object?

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.

Pull to refresh - Solution found but unable to implement it

I am using the example shown in the Pulltorefresh library. They have implemented it to fit a TableViewController class. But mine is a UIViewController, and i am adding a TableView in it.
I found a SO answer that shows a workaround for this. Can someone tell me how to implement it as described in the SO answer. Here's the link for the answer
That answer is not talking about using the library you are referencing. They are talking about how you would go about creating a UIScrollView with the "pull to refresh" functionality.
It shouldn't be that hard to transition the code to a UIViewController. Just change the code to subclass UIViewController and setup a UITableView property on the view controller (and make sure to set the data source & delegate of the UITableView to the view controller). There might be more to it but these are the basics. If you ask a more specific question about how to transition it to a UIViewController then maybe I (or someone else) can help more.
maybe you could try a different library?
I've used this one in the past, and it worked great!
http://cocoacontrols.com/platforms/ios/controls/egotableviewpullrefresh
Edit:
Here is the exact tutorial I used. He does exactly what you want. Adds the pull to refresh to a UITableView that is contained in a UIViewController!

Separate UITableView Controller/Delegate Reference Quandary

I've been following the guide in the following (StackOverflow question), which has been very helpful, and by completing the steps I've managed to successfully have a UITableView with a separate controller and delegate/datasource.
However, even though everything is working, I'm unable to reference or change anything instance of the UITableview.
For example: let's say the UIViewController for the table is TableViewController and the separate delegate/datasource class is TableDelegate. I have everything hooked up in interface builder as in the previous SO question. In TableDelegate.m, within viewDidLoad, I put the following:
self.navigationItem.leftBarButtonItem = self.editButtonItem;
And nothing changes in the UITableView as it should. This includes pushing UIViewControllers, which also has no effect as if the code wasn't even there, although when I NSLog(), it appears it is being run.
The odd part, however, is that even though that doesn't work, if I set the table's alpha property to 0, it works.
Thanks for any help in advance, let me know if you need any more information to solve the problem.
From your comments, it looks like both TableViewController and TableDelegate are subclasses of UIViewController. If this is the case, make sure both are being added to the navigation stack by pushing them onto a UINavigationController or adding them to a UITabBarController: otherwise, viewDidLoad may not be called.
As an aside, I don't think it's a good idea to have your UITableViewDataSource be a UIViewController subclass when you've already got a UITableViewController to handle that table view.

Moving tableview into existing window in Interface Builder

In Interface Builder, I dragged a UITableViewController object from the library into the project. Then, a tableview popped up. How do I get that tableview inside my already existing window?
Thanks, I'm totally lost!
That tableview is part of the UITableViewController, and is connected to its view property. The second window pops up so you can set that UITableView's properties directly.
If you're just starting out, I'd suggest checking out a few tutorial. The site I started learning from is called http://icodeblog.com/, and I'd also recommend the Beginning iPhone Development book by Jeff LaMarche and Dave Mark. Also, if you have iTunes, the Stanford iTunes class is a terrific way to learn in-depth about iPhone development, and it is free to download from iTunesU.
If you have created a view based application, the thing that you need is to add a uitableview from the library to the view. Be sure that you add uitableview, not its controller. It will be seen under dataviews
You have to take UIViewController first and bind it with view. Then add UITableView in your controller and bind it with IBOutlet object of UITableView.
Then you have to set datasource and delegate of UITableView. That's all. Now you can access all the methods of UITableVIew inside your controller.
If you still find any problem then please let me know. I will definitely help you out to solve this problem.