Open new view from actual view

Discussion in 'iOS Programming' started by Matteo Gobbi, Aug 23, 2008.

  1. macrumors newbie

    Aug 23, 2008
    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.
  2. thread starter macrumors newbie

    Aug 23, 2008
  3. macrumors 65816

    Jun 29, 2007
    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];
  4. macrumors member


    Sep 9, 2007
    Right here.
    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.



Share This Page