LoginSignup
0
0

More than 1 year has passed since last update.

【Objective-C 入門】コードだけで初期画面を生成

Last updated at Posted at 2021-11-18

はじめに

今回はStoryboardを使わず、コードのみで初期画面まで作成してみます。

環境

  • macOS 12.0.1
  • Xcode 13.1
  • Objective-C

完成図

AppDelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow * window;

@end

AppDelegate.m

#import "ViewController.h" インポートし忘れに注意!!

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

    self.window = UIWindow.new;
    [self.window makeKeyAndVisible];

    ViewController *vc = ViewController.new;
    self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController: vc];

    return YES;
}

SceneDelegate.h

@interface SceneDelegate : UIResponder <UIWindowSceneDelegate>

@property (strong, nonatomic) UIWindow * window;

@end

SceneDelegate.m

#import "ViewController.h" インポートし忘れに注意!!

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {

    self.window = [[UIWindow alloc] initWithWindowScene:(UIWindowScene *)scene];
    [self.window makeKeyAndVisible];

    ViewController *vc = ViewController.new;
    self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController: vc];
}

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end

ViewController.m

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = UIColor.whiteColor;

    [self setupNavigationItems];
}

- (void)setupNavigationItems {
    self.navigationItem.title = @"Objective-C";
    self.navigationController.navigationBar.prefersLargeTitles = YES;
}

@end

さいごに

AppDelegate.swift と SceneDelegate.swift のコードを書いていきました。
アプリを実行してみると完成図のようになると思います。

ここまで見ていただきありがとうございます、皆様の学びの助けになれば幸いです。

0
0
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
0
0