Joakim Tennfors
Joakim Tennfors

Reputation: 3

xCode - Changing views between 2 existing xib-files

I'm very new to xCode and objective-C so I wanted to make a simple textRPG.

I'm currently making a character creation process consisting of 4 xib-files. The only way I got switching views to work was to look at the utility-template. Problem is, now I have the first screen being the delegate for the second screen, being the delegate for the third screen etc. So by the end of the character creation process I can't dismiss the views because that just "steps back" through the views.

When I've searched around for a solution I've found a addSubview-method but it seems like that makes a new view, like, empty to arrange programmatically.

All I need is a simple way to switch from one loaded xib to another xib. Have I misunderstood addSubview or do I need something completely different?

(If it helps: I've worked with VB for several years, in case you notice that I missed some kind of concept concerning views and such) Thanks in advance! :)

Upvotes: 0

Views: 14890

Answers (4)

ADM
ADM

Reputation: 41

Use this code. It is really simple and works well.

  View *view = [[View alloc] initWithNibName:@"xibNameGoesHere" bundle:nil];
view.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:view animated:YES completion:nil];

This will switch to another xib file and the two views won't own one another. I am using it in my own game right now.

Upvotes: 4

Marko Hlebar
Marko Hlebar

Reputation: 1973

@Joakim Ok, this is the way I do it. I have a class called RootViewController : UIViewContoller, whose view I add directly to the window. Then I make a subclass of UIView i.e. MyView. All of my views then subclass MyView. I also make an enum for all my views. In RootViewController you should have a switchView:(int)view method that looks something like this:

    -(void) switchView:(myView) view
    {
        [_currentView removeFromSuperview];

        switch(view)
        {
            case titleView:
            _currentView = [[TitleView alloc] initWithRoot:self];
            break;
            case homeView:
            _currentView = [[HomeView alloc] initWithRoot:self];
            break;
            default: break;
        }
        [self.view addSubview:_currentView];
        [_currentView release];
    }

in @interface RootViewContoller define MyView *_currentView; TitleView and HomeView both subclass MyView and have a common method -(id)initWithRoot:(RootViewController*) rvc.

To switch between views use [_rvc switchView:homeView]; Hope this helps :)

Upvotes: 2

Marko Hlebar
Marko Hlebar

Reputation: 1973

another idea is not to use interface builder at all. i have been working with iphone apps for two years now and found that interface builder really prolongs the time to actually make something. make your own root controller and think about the logic you need to navigate through the views.

Upvotes: 0

hoha
hoha

Reputation: 4428

It is called UINavigationController. Idea is

1) You push corresponding 'next' controller into navigation controller each time user submits current screen. Bonus - you'll get 'back' button for free on each step of character creation.

2) After character is created you pop all character creation controllers from stack.

Please read View Controller Programming Guide for iOS before trying to 'switch views' and such. You'll save tons of time and nerves.

Upvotes: 0

Related Questions