LoginSignup
24
26

More than 5 years have passed since last update.

StoryBoardを使わない方法

Last updated at Posted at 2015-09-08

手順

  1. Info.plist内のMain storyboard file base nameの値を空白にする
  2. General - Deployment Info 内のMain Interfaceの値を空白にする
  3. AppDelegate.mに以下のコードを記述する
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // UIWindowの生成
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];

    // 最初に表示されるViewControllerを生成
    FirstViewController *fvc = [[FirstViewController alloc] init];
    self.window.rootViewController = fvc;
    [self.window makeKeyAndVisible];

    return YES;

}

NavigationControllerを使う場合

上記と同様に、AppDelegate.mに以下のコードを記述します。

AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];

    FirstViewController *fvc = [[UIViewController alloc] init];

    // 最初に表示したいViewControllerを指定してインスタンス生成する
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:fvc];
    self.window.rootViewController = nc;
    [self.window makeKeyAndVisible];

    return YES;
}

画面遷移する際には以下のようにします。
FirstViewController内に配置したボタンを押した際に画面遷移することを想定しています。
selfはFirstViewControllerです。

FirstViewController.m
- (IBAction)pushButtonPressed:(UIButton *)sender {

    SecondViewController *svc = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:svc animated:YES];

}
24
26
3

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
24
26