View Full Version : Open new view from actual view

Matteo Gobbi
Aug 23, 2008, 12:05 PM
i'm italian and excuse me if i do error.

I have 3 view: MAINVIEW, VIEW1, VIEW2.
A MAINVIEW is the principal view, and when applications is loades I connect a VIEW1 to it with [mainview addSubView:view1].

now i want that when I click one my button and a method start, it open a new view: VIEW2.

My view are designed by InterfaceBuilder (.xib file).

How must i do to initializate and open this VIEW2?


please you write a easy english thanks.

Matteo Gobbi
Aug 25, 2008, 07:09 AM

Aug 25, 2008, 07:13 AM
You need to make a connection to that view in Xcode.

IBOutlet UIView *view2;


Then link that view to the actually view in Interface Builder. You can then add it just like you added view1.

[view1 removeFromSubview];
[mainView addSubView:view2];

Aug 25, 2008, 09:49 AM
Alternatively, if you have View1 and View2 in different .xib files, you'll need to either:
1. Load the views using the [NSBundle mainBundle] object or
2. Creating a subclass of UIViewController to manage that particular .xib, then instantiate the UIViewController using initWithNibNamed:bundle: The class of the "File's Owner" in that .xib would be the same as your subclass of UIViewController. To get the view to load as a subview, you'd hook the view to the File's Owner (your UIViewController), and then (in code) do:
[window addSubview:[myView1Controller view]];

Personally, I prefer the second method as I understand what it's doing a bit better.