开发者

Switching custom views programmatically (pushing and popping)

开发者 https://www.devze.com 2023-03-26 21:02 出处:网络
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 probl

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/开发者_如何学Cxib 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.


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];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消