How did iOS set UIViewController's view frame before shown? - ios

I am not familiar with iOS UIViewController's detail implement. I have the following code to create a new UIViewController and show it, but the frame I set during initWithFrame method does not worked, the controller's view always is fullscreen(320*480).
UIViewController *viewController = [[UIViewController alloc] init];
// view
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 140, 130)];
viewController.view = view;
[view release];
AppController *app = (AppController*)[[UIApplication sharedApplication] delegate];
UINavigationController *nav = [app navController];
[nav pushViewController:viewController animated:YES];
[viewController release];
I search releative thoughts in apple developer documents, but I found nothing useful for this. How did UIViewController deal with its view frame property before show it? Where can I found useful documentation.
Thank you.
Update: In fact, the code is from cocos2d-iphone DirectorTest: https://github.com/cocos2d/cocos2d-iphone/blob/release-2.0-rc1/tests/DirectorTest.m#L143

You've got this all wrong - you really need to read (and understand) Apple's UIViewController docs:
View Controller Catalog
View Controller Programming Guide
View Controller Reference
If you're creating a view in code for a view controller, you should do it in the view controller's loadView method.
Directly from Apple's documentation:
Creating a View Programmatically
If you prefer to create views programmatically ...
you do so by overriding your view controller’s loadView
method. Your implementation of this method should do the following:
Create a root view object. The root view contains all other views
associated with your view controller. You typically define the frame
for this view to match the size of the app window, which itself should
fill the screen. However, the frame is adjusted based on how your view
controller is displayed. See “View Controller View Resizing.”
You can use a generic UIView object, a custom view you define, or any
other view that can scale to fill the screen.
Create additional subviews and add them to the root view. For each
view, you should do the following:
Create and initialize the view. For system views, you typically use
the initWithFrame: method to specify the initial size and position of
the view. Add the view to a parent view using the addSubview: method.
Assign the root view to the view property of your view controller.

Related

rootViewController and addSubview in Objective-C?

Im starting to learn OC.
The first question is about _window.rootviewcontroller and [_window addSubview:...]
Both of the two ways can set view for UIWindow (actually, UIWindow is inherited from UIView).
So what I want to know is :
Is setting the rootviewcontroller for window just using the addSubview method to implement , or it's something else?
more exactly:
is
_window.rootviewcontroller = viewController;
meaning
[_window addSubview: viewController.view];
or
_window = viewController.view; //UIWindow : UIView
or something else?
Thanks a lot.
Is there anyone who can tell me some details about UIWindow and the rootViewController property?
If you use addSubview: to have to pass a UIView instance but when you call rootviewcontroller you passing UIViewController instance to the UIWindow.
You can use addSubview but you have to associate UIView superview (Which needs to be UIViewController) to the UIWindow, to make it behave the same,
something like that (old way to to that:
[window addSubview:myViewController.view];
[window makeKeyAndVisible];
By using rootviewcontroller it will do it for you.
This is taken from Apple:
The root view controller provides the content view of the window.
Assigning a view controller to this property (either programmatically
or using Interface Builder) installs the view controller’s view as the
content view of the window. If the window has an existing view
hierarchy, the old views are removed before the new ones are
installed.
Obviously not. The root view controller is generally assigned to window in appdelegate class.
Also, root view controller is always associated with UINavigationController. So that any root view controller of a UINavigationController will be a its content view controller.
Where as, add subview is just a method of UIView class. Which helps to add any subview to the respective view.

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.

Display UISplitviewController In a PopOverViewController

I was recently displaying a list of items to a user via a tableview in a popover when a button was tapped. The table was displayed sections and headers.
I'm experimenting now with different ways to display the information and I want to try display the data in a UISplitViewController. The left view is a tableviewcontroller with headings. The right view is also a tableviewcontroller with sub headings.
I still want the data to be provided in a popover manner but when I've tried this the popover appears with nothing in it. I've put breakpoints in the viewDidLoad methods of my left and right view for the SplitView. The breaks are being hit but nothing is displaying in my popover.
How can I display a UISplitView IN a PopoverController.
My Root View
- (IBAction)showPopover:(id)sender {
self.SVC = [[SplitViewContainerViewController alloc] init];
self.pickerPopover = [[[UIPopoverController alloc] initWithContentViewController:SVC] autorelease];
CGRect frame= CGRectMake(0,0, 0, 0);
[self.pickerPopover presentPopoverFromRect:frame inView:self.view permittedArrowDirections:0 animated:NO];
LoadView Of SplitViewContainerViewController
- (void)loadView
{
UIView *view=[[[UIView alloc]initWithFrame:CGRectMake(0, 0, 750, 880)]retain];
self.view=view;
self.splitViewController = [[UISplitViewController alloc] init];
self.leftViewController=[[[LeftViewController alloc] initWithStyle:UITableViewStylePlain] autorelease];
self.rightViewController=[[[RightViewController alloc] initWithStyle:UITableViewStylePlain] autorelease];
self.splitViewController.viewControllers= [NSArray arrayWithObjects:self.leftViewController, self.rightViewController, nil];
[self.view addSubview:self.splitViewController.view];
self.contentSizeForViewInPopover = CGSizeMake(750,880);
[super loadView];
}
UISplitViewControllers do not work unless they are the root view controller of the window. In order to do something like this you are going to need to create a custom split view controller and use that instead of UISplitViewController.
From Apple's documentation:
A split view controller must always be the root of any interface you create. In other words, you must always install the view from a UISplitViewController object as the root view of your application's window. The panes of your split-view interface may then contain navigation controllers, tab bar controllers, or any other type of view controller you need to implement your interface.
You may want to start by looking at MGSplitViewController, however, be aware that there are some limitations in iOS 4 and below including the inability to correctly forward rotation and view appearance and disappearance events to the child view controllers.

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).

How can I replace a UIViewController with a UISplitView Controller?

Mine is a normal viewcontroller application. When I click a button on this mainView, it adds a 2nd view(a different xib) as a subview to the mainView. I want this 2nd view to be replaced by a splitview. So it means I should replace the 2nd view controller with a UISplitViewController, isnt it?
Can I do that? Is it possible to add a splitviewcontroller's view like v add a viewcontroller's view?
You should be aware that, currently, the only official usage of UISplitViewController is as a root view controller of an application; Apple does not intend for it to be a child view controller. Apparently, this is due to bugs with handling rotation of the iPad, and may get fixed at a later date. So you should avoid adding a UISplitViewController's view as a subview to anything.
You can subclass UIViewController and then in the init:
UIViewController *left = ...;
UIViewController *right = ...;
UISplitViewController *splitVC = ...;
splitVC.viewControllers = [NSArray arrayWithObjects:left,right,nil];
self.view = splitVC.view;
return self;
Then just use this as a normal UIViewController.

Resources