はじめに
今回は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 のコードを書いていきました。
アプリを実行してみると完成図のようになると思います。
ここまで見ていただきありがとうございます、皆様の学びの助けになれば幸いです。