8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

XIBやストーリーボードを使わずにEmpty Projectに画面を2つ追加し、お互いに画面遷移させるには

Last updated at Posted at 2013-06-07

スクリーンショット(2013-06-07 11.37.36).png

スクリーンショット(2013-06-07 11.37.48).png

以上のような2つの画面をお互いに遷移するようなアプリケーションを作る。

  1. アプリケーション本体を用意する
    [New Project]->
    [Empty Project]

    プロジェクトがつくられ、AppDelegate.hAppDelegate.mができる。

  2. 1枚目の画面を用意する
    [New File]->
    [UIViewCotroller subclass]->
    [class名]ViewController1
    [with XIB]チェックを外す

    プロジェクト内にViewController1.hViewController1.mができる。

  3. 2枚目の画面を用意する
    [New File]->
    [UIViewCotroller subclass]->
    [class名]ViewController2
    [with XIB]チェックを外す

    プロジェクト内にViewController2.hViewController2.mができる。

  4. ViewController1.mのコードを書く

ViewController1.m

〜略〜

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // ラベルを追加
    UILabel* label = [[[UILabel alloc] initWithFrame:self.view.bounds] autorelease];
    label.text = @"Hello, world!";
    label.textAlignment = UITextAlignmentCenter;
    label.backgroundColor = [UIColor blackColor];
    label.textColor = [UIColor blackColor];
    label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:label];
    
    // ボタンを追加
    UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setTitle:@"画面遷移" forState:UIControlStateNormal];
    [button sizeToFit];
    CGPoint newPoint = self.view.center;
    newPoint.y += 50;
    button.center = newPoint;
    button.autoresizingMask =
    UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
    [button addTarget:self action:@selector(buttonDidPush) 
        forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

〜略〜

- (void)buttonDidPush
{
    // 自分自身を背面へ移動
    [self.view.window sendSubviewToBack:self.view];
}

  1. ViewController2.mのコードを書く
ViewController1.m

〜略〜

- (void)viewDidLoad
{
    [super viewDidLoad];
    
     // ラベルを追加
    UILabel* label = [[[UILabel alloc] initWithFrame:self.view.bounds] autorelease];
    label.text = @"こんばんは、世界!";
    label.textAlignment = UITextAlignmentCenter;
    label.backgroundColor = [UIColor blackColor];
    label.textColor = [UIColor blackColor];
    label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:label];
    
    // ボタンを追加
    UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setTitle:@"画面繊維" forState:UIControlStateNormal];
    [button sizeToFit];
    CGPoint newPoint = self.view.center;
    newPoint.y += 50;
    button.center = newPoint;
    button.autoresizingMask =
    UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
    [button addTarget:self action:@selector(buttonDidPush) 
     forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

〜略〜

- (void)buttonDidPush
{
    // 自分自身を背面へ移動
    [self.view.window sendSubviewToBack:self.view];
}
  1. AppDelegate.hのコードを書く
AppDelegate.h

〜略〜

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    UIViewController* _viewController1;
    UIViewController* _viewController2;
}

〜略〜

  1. AppDelegate.mのコードを書く
AppDelegate.m

〜略〜

#import "AppDelegate.h"
#import "ViewController1.h"
#import "ViewController2.h"

@implementation AppDelegate

@synthesize window = _window;

- (void)dealloc
{
    [_viewController1 release];
    [_viewController2 release];
    
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
//    self.window.backgroundColor = [UIColor whiteColor];
    
    _viewController1 = [[ViewController1 alloc] init];
    _viewController2 = [[ViewController2 alloc] init];
    [self.window addSubview:viewController1_.view];
    [self.window addSubview:viewController2_.view];
    [self.window bringSubviewToFront:_viewController1.view];
    
    [self.window makeKeyAndVisible];
    return YES;
}

〜略〜

これで実行すれば2つのビューが交互に表示されるはずである。


ブログやってます:PAPA-tronix !

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?