Cannot add Outlet connection from Button to ViewController - swift

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.

Related

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.

iOS5 Second View Controller

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

Creating a base class for customizing the view and let other (UIViews) inherit from that class

I read this post but I can't get it working: Change Background Color...
I like to create a base UIView class for all my view customization like background pattern, color of the buttons, color of the tab bar etc.
This is what I've tried:
Created a new Single View Application
Customized the view in UIViewController method -viewDidLoad
Created a new subclass of that UIViewController
In IB I created a new UIViewController object
In IB (Identity Insp.) I set the UIViewController class to the subclass I created earlier
Is this not the proper way to do this?
I was thinking that the new created UIViewController class will inherit all
customizations from my first UIViewController class since I subclassed that one.
Second question is how can I use a base class for certain objects instead of a whole view?
For example how do I let all UIButtons in my App inherit all the behavior I have defined in just one single class?
Hope you can help.
Happy Easter for all.
I believe you want to customize your view, but exactly how and when is not clear to me.. so I will just give the common method which will give you the general idea of how to do it..
First create a subclass of UIView say BaseView with properties to be customized, now in the viewDidLoad methods of your viewController create an object of BaseView and customize this object as you like.. once this is done just set the self.view property of your viewController to this newly created BaseView object and then release the BaseView object..
hoping this helps you somewhat...
EDIT:
based on the comments ..
First create a BaseViewController class which subclasses UIViewController... in the viewDidLoad of this class customize your view using self.view
Second now the viewControllers which you want to use in your tabController should subclass this BaseViewController... just don't forget the [super viewDidLoad]; is called in the viewDidLoad of this viewcontroller.
this is the best I have to offer.. :) let me know if it works for you.

Add views to "more" tab item

Is it possible to change appearence of "more" item in TabBarController? For example can I add custom views and change layout of tableView containing "more" controllers list?
Just find it was already discussed in
Customizing the More menu on a Tab bar
https://discussions.apple.com/thread/2399024
So we can get instance of this "More" ViewController using:
UIViewController *moreViewController = tabBarController.moreNavigationController.topViewController;
In fact it is of undocumented class UIMoreListController declared as:
#interface UIMoreListController : UIViewController <UITableViewDelegate, UITableViewDataSource>
moreViewController.view property contain UITableView and we can use it, for example add tableHeaderView:
UITableView *moreTableView = (UITableView*)moreViewController.view;
moreTableView.tableHeaderView = myOwnCustomView;
But I am not sure is this code "applesafe"? Because it use private class although not explicitly.
And what if I want to add a view that shouldn't scroll with table? Any ideas?
yes, this is all possible, if you could give us an example of what you want, maybe with a screenshot or something, then we can help you with some code examples
You can do what you want to do...
by using UITableViewController as a moreview tabbaritem.
and can create a moreview some what similar to the standard moreView controller.

iPhone Advanced table view cells example

I have been going through and re-creating Apple's "Advanced Table View Cells" example to better understand everything. I have done everything, but somehow missed one detail and I can't figure it out.
If you launch their example (http://developer.apple.com/iphone/library/samplecode/AdvancedTableViewCells/Introduction/Intro.html) and open up the RootViewController.xib file, they have a "tableView" outlet on the inspector that is linked to File's Owner. For whatever reason, I can't get that to show up on my version.
My code is almost verbatim of Apple's, so what am I missing? Did I not declare that somewhere or doesn't that get taken from the RootViewController.m file?
Did you set File's Owner to the class of the object you are trying to link to?
To check if you did so, open your xib file, and see if the File's Owner type is the class you want to link to.
If it doesn't and reads something else like NSObject, open the inspector, go to the Identity tab (Cmd+4), click File's Owner and in the Class Identity section, there's a dropdown menu with the list of classes in your project. Select your UITableViewController subclass and then try to make the link.
Their RootViewController subclasses UITableViewController. It is this that generates the tableView outlet.
So after being bugged by this problem for a while... I figured out a workaround. Please note that all of my code was exactly the same as Apple's and yet IB would never show a tableView outlet (as if it wasn't subclassing UITableView properly in my UINavigation Controller).
Essentially all I did was change the subclass from a UITableViewController to a UIViewController and then called the delegate and datasource protocols. After doing that and creating my own tableView outlet, I was finally able to hook up the tableView and get it working as it should.
#interface RootViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
used to be
#interface RootViewController : UITableViewController {