Should I not override -[UIViewConvtroller view]? - objective-c

I'm told not to override -(UIView *)view ever. Why not? I want to use a custom UIView subclass for my custom UIViewController. What's a better way to do so?

You're not supposed to override -view. To accomplish the behavior you want, you can set up your view controller to load from a nib, or you can override the -loadView method. In the latter method you create whatever view you want and assign it to self.view.

If you're using a nib or storyboard, simply set the root view's class to your new, custom UIView subclass. If you're not using a nib or storyboard, create your custom subclass in the -loadView method and set the view controller's view property to it. So, for example, if you had a custom UIView subclass named MyView, and you're creating it in code (not in a nib or storyboard), you'd do something like:
// This code sample assumes compiling with ARC
- (void)loadView
{
// You should adjust the initial frame to be whatever's appropriate for this
// view controller
MyView* view = [[MyView alloc] initWithFrame:CGRectMake(0, 0, 360, 480)];
[self setView:view];
}

Related

How to add UIView as a subview on UIViewController in UIStoryboard

Is there any way that I can add uiview as a subview over a view controller using UIStoryboard, using xib's we can do that, but i'm unable do that using storyboard.
My storyboard is not holding any of the uiview as a subview when I drag and drop on it, it was placing under the view controller.
Is there any way that I can add it programmatically on my view controller using storyboard.? I'm stuck please help me out
Am I right, you want to hold UIView in storyboards without view controller or superview ?
You can't do that. You should use XIBs to hold custom views.
It doesn't matter you add it programmatically or via drag and drop, in storyboards you can't hold "isolated" views, every view must have a superview and therefore a UIViewController.
Check apple's guide, make sure you understand UIViewController,UIView,UIStoryboard classes and relations between them. Also this.
Hope it helped.
Yes, you can override UIViewController's loadView method to do it as i have written code below.
Because loadView is the method which is called first of all other viewController's loading methods. So you can set it here.
Hope this will work for you as I have tested it on my code.
- (void)loadView {
self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, [UIScreen mainScreen].bounds.size.height)];
self.view.backgroundColor = [UIColor blackColor];
// enter your customization code here
}
write this line in function
-(void) ViewDidLoad:
[self.view addSubView:...];

In storyboard, how to assign a view controller's subview a different delegate/controller m file?

In my storyboard, I have a view (a subclass of UIScrollView called AQGridView) which is nested inside a view controller scene. I want to assign the AQGridView a controller delegate and datasource which is defined in a separate .m and .h file than the parent view controller. How do I do this? Storyboard only lets me drag connectors to AQGridView's parent view controller.
EDIT:
I've tried doing something in the parent view controller like this (where myGrid is an IBOutlet pointing to the AQGridView and myGridController is a property of the parent view controller):
- (void)awakeFromNib
{
// note: kzMyGridController is a subclass of AQGridViewController
myGridController = [[kzMyGridController alloc] init];
myGrid.delegate = myGridController;
myGrid.dataSource = myGridController;
}
But it doesn't appear to be working because none of its delegate methods are being called. What am I doing wrong?
If you are not allowed on the StoryBoard, just do it on the code. It makes more sense, because it actually forces you to have an object (the delegate) and set it when you need it.
It should be:
- (void)awakeFromNib
{
// note: kzMyGridController is a subclass of AQGridViewController
myGridController = [[kzMyGridController alloc] init];
myGrid.delegate = myGridController;
myGrid.dataSource = myGridController;
}
The answer was that I had to call reloadData on the AQGridView. The view is created before its delegate, so its initial data getting methods fire without a receiver.

Difference between viewDidLoad and loadView?

Two objective-c methods, -(void) viewDidLoad and -(void)loadView are methods called upon execution of a program but whats the different between them?
Do you mean viewDidLoad and loadView? viewDidLoad is a method called when your view has been fully loaded. That means all your IBOutlets are connected and you can make changes to labels, text fields, etc.
loadView is a method called if you're (typically) not loading from a nib. You can use this method to set up your view controller's view completely in code and avoid interface builder altogether.
You'll typically want to avoid loadView and stick to viewDidLoad.
Use -(void)loadView when you create the view. Typically usage is:
-(void)loadView {
UIView *justCreatedView = <Create view>;
self.view = justCreatedView;
}
Use -(void)viewDidLoad when you customize the appearance of view. Exapmle:
-(void)viewDidLoad {
self.view.backgroundColor = [UIColor blackColor];
...
}
i think you are talking about loadView and viewDidLoad.
loadView is a method that you not using a nib file - you use it to programmatically 'write' your interface
viewDidLoad fires automatically when the view is fully loaded. you can then start interacting with it.
more to read read in the discussion here iPhone SDK: what is the difference between loadView and viewDidLoad?

Customizing Custom UIView class

I have a custom UIView class that creates a view I can use in my main view controller that allows for better dragging. The problem, is that right now, the object I'm dragging is just a black box. I would like to be able to put my own custom image into that box. How would I do this? The custom UIView only has a .h and a .m and I don't know which method to put the code in.
Here's the code Im using right now
UIImageView *player = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 37, 37)];
player.image = [UIImage imageNamed:#"ghost.png"];
[self addSubview: player];
You could
Add your custom view in your view controllers main xib file and add the image view to it in the interface builder view
In your view controllers viewDidLoad method you could add the UIImageView to the custom view using your current code
Override the drawRect method of your custom view and draw the image there see here (however note the answer which says you should not load the image in the drawRect method, load the image on construction of your view in the initWithFrame or the awakeFromNib method depending on how you load it)

Creating views programmatically

I set views programmatically. Here is how I do that. Let's say I have SettingsViewController.m
In this file I have two methods
-(void)loadView
{
UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
self.view = view;
[view release];
}
-(void)didViewLoad
{
// In that method I create some buttons labels etc
}
Is my approach correct ? To create the view in loadView method and buttons, labels etc in viewDidLoad method
To be honest it doesn't really matter if you put the code for creating views in viewDidLoad or loadView. viewDidLoad is called after the view is loaded, so will even be called if you are instantiating from a XIB. So that's a good place to add extra views if you're using a XIB. If you're programatically creating the view like you are in loadView then you can put the creation of your buttons, labels, etc in loadView or viewDidLoad and it won't really make a difference - viewDidLoad is pretty much called straight after loadView runs anyway.
Personally if I'm creating a view programatically using loadView then I will put all of the view creation code in there rather than in viewDidLoad.