3

In iOS, my views work individually but I can't switch between them.

Now after a lot of google-ing around I've fond that the navigation based app would work great with the stack for views. The problem is all my views are nib/xib-less and custom tailored in the source code. Because of that I need my own UINavigationController and push and pop views by hand/code. Since every tutorial is either nib/xib and IB bound or just a mash of code snippets I need a concrete example how to do it.

A simple example with 2 programmatically created view (can be empty just have to use loadView instead of initializing with a nib/xib) and a working stack (a push and a pop of the views like: load app,create some root view if needed, push one view and from that view push the second one and then pop them) would be awesome, or at least a tutorial in that way with the whole source of the project and not snippets.

Thanks in advance.

EDIT: After some extra thinking, a little more clarification wouldn't be bad. My app will basically consist of 5 or 6 views which will be called form their respective previous view, i.e. a drill-down app.

Thunder Rabbit
  • 5,405
  • 8
  • 44
  • 82
Krunch
  • 85
  • 1
  • 9
  • That i dont understand. I too have several views, i need to switch them programatically. And all tutorials are done with .xib/.nib files. So i have just finished my fifth hour of searching for examples. I have read the docs but that was not enough for me. The last thing that i found and has helped me was this [video](http://www.youtube.com/watch?v=ugrbVirgHnA) on youtube Grrr. – Martin Berger Oct 04 '12 at 10:48

1 Answers1

4

Here's a brief code, only the essential parts:

CodeViewsPushingAppDelegate.m

#import "CodeViewsPushingAppDelegate.h"
#import "ViewNumberOne.h"
@implementation CodeViewsPushingAppDelegate

@synthesize window = _window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:    (NSDictionary *)launchOptions
{
    UINavigationController *navController = [[UINavigationController alloc] init];
    ViewNumberOne *view1 = [[ViewNumberOne alloc] init];
    [navController pushViewController:view1 animated:NO];
    [self.window addSubview:navController.view];
    [self.window makeKeyAndVisible];
    return YES;
}

ViewNumberOne.h

#import <UIKit/UIKit.h>

@interface ViewNumberOne : UIViewController
{
    UIButton *button;
}

- (void)pushAnotherView;

@end

ViewNumberOne.m

#import "ViewNumberOne.h"
#import "ViewNumberTwo.h"
@implementation ViewNumberOne

- (void)loadView
{
    [super loadView];
    button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(110, 190, 100, 20);
    [button setTitle:@"Push Me!" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(pushAnotherView) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

- (void)pushAnotherView;
{
    ViewNumberTwo *view2 = [[ViewNumberTwo alloc] init];
    [self.navigationController pushViewController:view2 animated:YES];
    [view2 release];
}

ViewNumberTwo.h

#import <UIKit/UIKit.h>

@interface ViewNumberTwo : UIViewController
{
    UILabel *label;
}

@end

ViewNumberTwo.m

#import "ViewNumberTwo.h"

@implementation ViewNumberTwo

- (void)loadView
{
    [super loadView];
    label = [[UILabel alloc] init];
    label.text = @"I am a label! This is view #2";
    label.numberOfLines = 2;
    label.textAlignment = UITextAlignmentCenter;
    label.frame = CGRectMake(50, 50, 200, 200); //whatever
    [self.view addSubview:label];
}
Niv
  • 2,294
  • 5
  • 29
  • 41
  • 1
    Working like a charm, hope other people will have use from this too. – Krunch Aug 09 '11 at 19:25
  • 1
    Happy to help :) after all, except for the initing ( you call init and implement loadView instead of initWithXib ) it's exactly the same. I would recommend using xibs though - It's less resources-consuming, faster, and easier to change and localize. – Niv Aug 09 '11 at 19:29
  • 1
    Thanks for the info, but my views will be dynamic so xibs are out of question, anyways, I knew it wasn't hard, I was messing with the Navigation based template but just couldn't make it work, switched to a blank window based one and then something was always missing. Thanks ;) – Krunch Aug 09 '11 at 19:34
  • @Niv How did ViewNumberOne and ViewNumberTwo got `navigationController` property? In method `- (void)pushAnotherView;` there is a line `[self.navigationController pushViewController:view2 animated:YES];`. Did you make a pointer to navigation controller and then passed pointer to your navigation controller during initialization or somehow else? I originally thought that it is a job of delegates to do this job `UINavigationControllerDelegate` but no, this category is used to enable additional control for current loading view. – Martin Berger Oct 04 '12 at 11:11