@blog.justoneplanet.info

日々勉強

UINavigationControllerを使う

新しい事をやりすぎて古いことを忘れそうなのでメモしておく。

■実装

早速実装する。

AppDelegate.h

#import <UIKit/UIKit.h>
#import "MainViewController.h"

@class MainViewController;

@interface SampleNavigationAppDelegate : NSObject <UIApplicationDelegate> {
@private
    UINavigationController *nav;
    MainViewController *mainViewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) UINavigationController *nav;
@property (nonatomic, retain) MainViewController *mainViewController;
@end

AppDelegate.m

#import "SampleNavigationAppDelegate.h"

@implementation SampleNavigationAppDelegate
@synthesize nav;
@synthesize mainViewController;

@synthesize window=_window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    mainViewController = [[MainViewController alloc] init];
    nav = [[UINavigationController alloc] initWithRootViewController:mainViewController];
    nav.view.frame = [UIScreen mainScreen].applicationFrame;
    [self.window addSubview:nav.view];
    [self.window makeKeyAndVisible];
    return YES;
}

MainViewController.h

#import <UIKit/UIKit.h>
#import "SecondViewController.h"

@class SecondViewController;

@interface MainViewController : UIViewController {
    SecondViewController *secondViewController;
}
@property (nonatomic, retain) SecondViewController *secondViewController;
@end

MainViewController.m

@implementation MainViewController
@synthesize secondViewController;

-(void)onclick:(UIButton*)sender{
    secondViewController = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:secondViewController animated:YES];
    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setFrame:CGRectMake(0, 0, 320, 480)];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    [label setText:@"first view"];
    [self.view addSubview:label];
    [label release];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn setFrame:CGRectMake(250, 0, 50, 30)];
    [btn addTarget:self action:@selector(onclick:) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:btn];
}

画面は凄い適当だけど・・・

コメントはまだありません»

No comments yet.

RSS feed for comments on this post.TrackBack URL

Leave a comment