loadView: functions in UIView iOS - ios

I don't understand the mechanism of loadView: function (this function is in UIView).
I created a project as below:
First, I created a iPhone's window-based project.
Then, I created a UIView subclass
Next, I created a UIViewController subclass, with no xib.
Lastly, in the loadView: function of the class I created in the third step, I designate the UIView object (in the class I created in the second step) as the view variable of the UIViewController object (in the third step).
If I omit the last step, and place the statement NSLog(#"test LoadView"); in the loadView: function, then when the project is run, the statement NSLog(#"test LoadView"); is invoked continuously, result in the run is overflow.
Please explain me! Thank you!

loadView: is only invoked when the view property is nil. Use this when creating views programmatically. default: create a UIView object with no subviews. For ex -
- (void)loadView
{
UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
[view setBackgroundColor:color];
self.view = view;
[view release];
}
By implementing the loadView: method, you hook into the default memory management behavior. If memory is low, a view controller may receive the didReceiveMemoryWarning message. The default implementation checks to see if the view is in use. If its view is not in the view hierarchy and the view controller implements the loadView: method, its view is released. Later when the view is needed, the loadView: method is invoked again to create the view.
Not sure why you want to use loadView: but you can do just as much in viewDidLoad:
Reference -
Why is this iPhone program not calling -loadView?
loadView
Hope this helps.

Related

It's confusing where to put init code

I have a UIView, for which I implemented the init method. This works for me because I always initialize my UIViews with the simple code below.
MYView *view = [[MYView alloc] init];
[self.view addSubview:view];
[view setFrame:CGRectMake(0,0,99,99)];
QUESTION: Can this view be used with storyboards? or do I need to implement other "init" methods? Also, what happens if someone instantiates the view with initWithFrame? Do I need to implement all the "init" methods to handle all these cases?
Storyboard and XIB files will use the initWithCoder: methods.

viewWillAppear not invoked for subview controller

I have an odd case -- a view controller that creates its own view in loadView and which is then added to an existing view.
Here is the code that creates and adds the VC:
self.doneButtonViewController = [[DoneButtonViewController alloc] init];
[self.view addSubview:self.doneButtonViewController.view];
This code is executed in viewDidLoad of the "parent" VC.
The odd thing is that the viewWillAppear method of the added VC is never invoked (nor is viewDidAppear), but the viewWillDisappear method of the added VC is invoked (at the appropriate time), just as one would expect.
Any clue as to why viewWillAppear is not getting invoked?
The application isn't aware of the subview's view controller if you do this, you need to introduce view controller containment to make the root view controller aware. Doing so will handle any events like this.
Because loadView could be called more than once pre iOS 6, I'd advise creating the view controller within init, and then add the subview within loadView. It should be like this:
- (id)init {
...
self.doneButtonViewController = [[DoneButtonViewController alloc] init];
[self addChildViewController:self.doneButtonViewController];
[self.doneButtonViewController didMoveToParentViewController:self];
...
}
- (void)loadView {
...
[self.view addSubview:self.doneButtonViewController.view];
...
}
See "Implementing a Container View Controller" at http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html
As for me, adding child view controller in parent view controller can solve the problem that "viewWillAppear" of the child view controller not get called.

Programatically adding a tableView, viewDidLoad not called

I would like to use my viewDidLoad function in my tableViewController.
How can I make viewDidLoad run in my controller?
tableViewController = [[TableViewController alloc] init];
UITableView *tableView = [[UITableView alloc] init];
tableViewController.view = tableView;
....
From Apple documentation:
This method is called after the view controller has loaded its view hierarchy into memory. This method is called regardless of whether the view hierarchy was loaded from a nib file or created programmatically in the loadView method. You usually override this method to perform additional initialization on views that were loaded from nib files.
So you can try to instantiate it from NIB or overwrite the loadView method. Another step from Apple documentation:
If you cannot define your views in a storyboard or a nib file, override the loadView method to manually instantiate a view hierarchy and assign it to the view property.
viewDidLoad will be called when the view is actually loaded, which will happen after you present your view controller, by, e.g.:
adding it to a navigation controller,
adding it to a tab bar controller,
presenting it modally.
This is the missing bit in your code. If you explain how you would like to present your view controller, I may help further. Also, have a look at this: Presenting View Controllers.
(I assume the fact that you tried to override the view property of your table view controller was just an attempt "to make things work" -- but you do not need to do anything about that, the view controller will be correctly set up with a table view inside of it).
tableViewController = [[TableViewController alloc] init];
tableViewController.tableView // This is your newly generated tableview
viewDidLoad will be called after you assign the tableView to another parentview

iOS uiview and uiviewcontroller

Hy
i have two classes uiviewcontroller and uiview. I have one view controller. Inside i have uiview. Inside uiview i have textfield and when i write a text and click done i need to refresh uiviewcontroller.
I tried with this in uiview class:
-(IBAction)textFieldReturn:(id)sender
{
ViewController *vc = [[ViewController alloc] init];
[vc viewDidLoad];
}
i need refresh the same as you click the button and open viewcontroller.
I am guessing you mean that you want to "refresh" the view, not the view controller. To do that simply call [self setNeedsDisplay] from the view, or [self.view setNeedsDisplay] from the view controller. Also make sure that the textfield is a subview of the uiview. Either do that in the nib file or in code by calling [self addSubview: (textfield here)].
Also, if you want to access the view controller from the view you will need to create an IBOutlet, simply allocating a new ViewController object within the view does not mean that the created view controller controls the view. Hopefully that makes sense. I'd recommend going through some ios starter tutorials as well. Just google that there are a lot.

UIView: adding UIViewController's view as a subview and its removing

I would like to ask what is the correct way to add and remove UIViewController's view as a child view.
So, having UIViewController initialized I can add its view to view hierarchy as follows:
UIViewController *myViewControler = [[UIViewController alloc] init];
[someAnotherView addSubview:myViewController.view];
Question 1: Should I release myViewController.view after addSubview: call?
If I want to remove myViewController's view from view hierarchy I call [myViewController.view removeFromSuperview];
Question 2: How should I release myViewController instance in this case after its view removedFromSuperview?
You do not need to release the view, the owning view controller will do this for you.
I normally put the declaration of myViewController in the header and then release and nil it when I am done with it (either somewhere in the normal flow or in the dealloc of the containing view controller).

Resources