UIViewControllerによる2画面遷移

UIViewController

Description

The UIViewController class provides the fundamental view-management model for all iOS apps. You rarely instantiate UIViewController objects directly. Instead, you instantiate subclasses of the UIViewController class based on the specific task each subclass performs. A view controller manages a set of views that make up a portion of your app’s user interface. As part of the controller layer of your app, a view controller coordinates its efforts with model objects and other controller objects—including other view controllers—so your app presents a single coherent user interface.

UIViewControllerクラスは全てのiOSアプリの基礎的なview-managementモデルを提供する。UIViewControllerオブジェクトのインスタンスを直接生成することは少ない。代わりに、それぞれの役割を持つUIViewControllerクラスのサブクラスからインスタンスを生成する。viewコントローラーは、アプリのユーザーインターフェースの一部を担うviewの集まりを管理する。アプリのコントローラーレイヤーの一部として、viewコントローラーは、モデルオブジェクトや他のコントローラーのオブジェクト(他のviewコントローラーのオブジェクトも含む)との調整を行う。そのため、アプリは一貫したユザーインターフェースを持つことができる。

Availability

iOS (2.0 and later)

Declared

UIViewController.h

Reference

UIViewController Class Reference

Guides

View Controller Catalog for iOS, View Controller Programming Guide for iOS

Samples

AdvancedURLConnections, NavBar, Tabster, UICatalog, iAdSuite

UIViewControllerの役割

画面の構成要素をひとまとめにして切り替えることが可能

簡単な2画面遷移の例

  • UIViewControllerクラスのViewを2つ用意し、AppDelegate.mであらかじめ2つとも追加しておく
  • [self.window bringSubviewToFront:viewController1.view]で最初にviewController1を全面に表示
  • UIViewControllerクラスのViewはviewプロパティで参照できる
  • ボタンをタッチした時に呼ばれる[self.view.window sendSubviewToBack:self.view]で自身のViewを背面に隠すことで、もう一つのViewを画面に表示

#import 

@interface StudyAppDelegate : UIResponder 
{
    //UIViewControllerクラスのViewを用意
    UIViewController* viewController1;
    UIViewController* viewController2;
}
@property (strong, nonatomic) UIWindow *window;
@end
#import "StudyAppDelegate.h"
#import "ViewController1.h"
#import "ViewController2.h"

@implementation StudyAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    viewController1 = [[ViewController1 alloc] init];
    viewController2 = [[ViewController2 alloc] init];
    
    [self.window addSubview:viewController1.view];
    [self.window addSubview:viewController2.view];
    
    //最初にviewController1を前面に表示
    [self.window bringSubviewToFront:viewController1.view];
    
    [self.window makeKeyAndVisible];
    return YES;
}
#import 

@interface ViewController1 : UIViewController

@end
#import "ViewController1.h"

@interface ViewController1 ()

@end

@implementation ViewController1

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //ラベルの作成
    UILabel* label = [[UILabel alloc] initWithFrame:self.view.bounds];
    label.text = @"Hello, World";
    label.textAlignment = UITextAlignmentCenter;
    label.backgroundColor = [UIColor whiteColor];
    label.textColor = [UIColor blackColor];
    label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:label];
    
    //ボタンの作成
    UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setTitle:@"画面遷移1" 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]; //自身のViewを背面に移動
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
#import 

@interface ViewController2 : UIViewController

@end
#import "ViewController2.h"

@interface ViewController2 ()

@end

@implementation ViewController2

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UILabel* label = [[UILabel alloc] initWithFrame:self.view.bounds];
    label.text = @"こんばんわ、世界";
    label.textAlignment = UITextAlignmentCenter;
    label.backgroundColor = [UIColor blackColor];
    label.textColor = [UIColor whiteColor];
    label.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:label];
    
    UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setTitle:@"画面遷移2" 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];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

コメント