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

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.

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.

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

UIView or UIViewController for custom View

What is the best way to create a custom UIView that I can consume in Interface Builder?
Create a custom UIView in Interface Builder and inherit from UIView in a code file, then somehow use it in another UIView ala like a control. (How do I do this?)
Create a custom UIView in Interface Builder and have a custom UIViewController wire it up. In my main ViewController, place the new view.
Basically, I am trying to create a reusable display view and would like a quick way to change it across all my instances with minimal effort. I already have laid out my XIB in Interface Builder.
The best is the 1st way. And don't forget to place IBOutlet keyword before class member, that you want to see in Interface Builder.
#interface MyViewController : UIViewController {
IBOutlet UILabel *m_MyLabel;
}
....
You'll want to do (1). Presumably you've got an existing IB file in which you'd like to place the custom UIView subclass? In that case, go to that file, drag out a UIView, and then in the "Application Identity" tab of the inspector (4th tab) set the Class to your custom class (as defined in code).

MVC: Where to load views?

I have a relatively simple app. I have a fullscreen UIView subclass (musicGridView) that the user interacts with, and a UIScrollView subclass (scrollView) that loads this class in, as well as a ViewController and mainWindow.xib.
Right now, I have a viewController instance loaded in the nib, which is of type myViewController. The nib also has instance of myUIView, which the myViewController is pointed to.
In AppDelegate, I do this:
[window addSubview:scrollView];
[scrollView addSubview: viewController.view];
scrollView.musicGridView = (MusicGridView*) viewController.view;
which I think is wrong. Ideally, the appDelegate doesn't have an instance of scrollView.
In scrollView's AwakeFromNib class, I do the initialization on the scrollView.
The way I think it should go, is load the [window addSubview:viewController.view] in appDelegate, and then point the viewController to an instance of scrollView instead of musicGridView. Then, in scrollView awakeFromNib add a subview of musicGridView.
Eventually, I want to create another view owned by scrollView that is actually a side TabBar (which isn't possible with the given API's) that the user can scroll to the left to reach.
So I guess amongst other MVC stuff, the question is, should the viewController point to the scrollView, which contains all other content UIView subclasses, including musicGridView?
It sounds like you are not using Interface Builder to design your UI. Since this is a new project, I would suggest doing that. You should not have to write any code like this at all. You will however need to describe your outlets and actions.
The two most important things you need to know when dealing with IB are the IBOutlet and IBAction keywords.
Sample class header:
#interface MyClass : NSObject {
IBOutlet UIScrollView* myScrollView;
}
- (IBAction) doWork;
#end
Now you can wire these two things up using Interface Builder by dragging sources to destinations with your mouse. YouTube has lots of tutorial videos on IB if you need a better description of how to do this.
I'm someone who doesn't use Interface Builder for UI design; in my experience, you need to go either all-IB, or all-programmatic. To solve your particular problem, I think you need your musicGridView to be an instance or extension of UIScrollView.
This can be done in your custom viewController's loadView method - simply initialize it to a UIScrollView (and add things to it), instead of a plain old UIView.
However, as described above, this approach isn't compatible with an IB-centric approach, as is confirmed in the UIViewController Class Reference
All subclasses of UIViewController
should implement the loadView method
to set the view property unless you
load your view from a nib file.

How can I use an UIScrollView in an Multiview-Application?

I have:
- an AppDelegate class
- two viewController classes
- three nib files (MainWindow.xib, firstView.xib, secondView.xib)
I want:
The secondView.xib should have an UIScrollView inside, so that I can add a lot of stuff to the view and it can be scrolled down.
Where would I have to conform to the UIScrollViewDelegate protocol now? I feel the AppDelegate class is the wrong place, because its far away from that secondView and not all my views want to use a UIScrollView.
since UIScrollView is located in the secondView.xib I assume its functionality is limited to that view. If you have an instance of UIViewController that serves as the controller for the secondView.xib then the best place to implement UIScrollViewDelegate would be on that UIViewController directly. You are correct that implementing it on the AppDelegate doesn't make sense if you have a more specific derived controller where it should belong. Hope this helps!