I'm using Xcode 13, tried to taste the function of using pure Objective-c code to set up my HelloWorld App, but run into Black Screen. Any kind thoughts? Bellow is that I've done.
1 - New Project, select App, Interface - Storyboard, Language - Objective-C, Create. 2 - Remove viewController.h, viewController.m, Main storyboard xib.to trash. 3 - Create Cocoa Touch Class - RootViewController, UN-select Also create XIB File.
Then modified files are looks below:
//
// AppDelegate.h
// HelloCode
//
// Created by Herman Ye on 2022/2/26.
//
#import <UIKit/UIKit.h>
#import "RootViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
//self.window.rootViewController = [[RootViewController alloc] init];
self.window.rootViewController = RootViewController.new;
[self.window makeKeyAndVisible];
return YES;
}
#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self renderPage];
}
-(void) renderPage {
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat labelWidth = 90;
CGFloat labelHeight = 20;
CGFloat labelTopView = 150;
CGRect frame = CGRectMake((screen.size.width - labelWidth)/2, labelTopView, labelWidth, labelHeight);
UILabel* label = [[UILabel alloc] initWithFrame:frame];
label.text = @"Hello Code";
label.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label];
}
... ...
PS added log into SceneDelegate
//
// SceneDelegate.m
// HelloWorld
//
// Created by Herman Ye on 2022/2/24.
//
#import "SceneDelegate.h"
@interface SceneDelegate ()
@end
@implementation SceneDelegate
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
NSLog(@"%@", @"SceneDelegate1=============================>willConnectToSession");
}
- (void)sceneDidDisconnect:(UIScene *)scene {
// Called as the scene is being released by the system.
// This occurs shortly after the scene enters the background, or when its session is discarded.
// Release any resources associated with this scene that can be re-created the next time the scene connects.
// The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
NSLog(@"%@", @"SceneDelegate2=============================>sceneDidDisconnect");
}
- (void)sceneDidBecomeActive:(UIScene *)scene {
// Called when the scene has moved from an inactive state to an active state.
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
NSLog(@"%@", @"SceneDelegate3=============================>sceneDidBecomeActive");
}
- (void)sceneWillResignActive:(UIScene *)scene {
// Called when the scene will move from an active state to an inactive state.
// This may occur due to temporary interruptions (ex. an incoming phone call).
NSLog(@"%@", @"SceneDelegate4=============================>sceneWillResignActive");
}
- (void)sceneWillEnterForeground:(UIScene *)scene {
// Called as the scene transitions from the background to the foreground.
// Use this method to undo the changes made on entering the background.
NSLog(@"%@", @"SceneDelegate5=============================>sceneWillEnterForeground");
}
- (void)sceneDidEnterBackground:(UIScene *)scene {
// Called as the scene transitions from the foreground to the background.
// Use this method to save data, release shared resources, and store enough scene-specific state information
// to restore the scene back to its current state.
NSLog(@"%@", @"SceneDelegate6=============================>sceneDidEnterBackground");
}
@end
Run app log: 2022-02-27 10:32:14.811892+0800 HelloCode[29356:4206590] SceneDelegate1=============================>willConnectToSession 2022-02-27 10:32:14.812183+0800 HelloCode[29356:4206590] SceneDelegate5=============================>sceneWillEnterForeground 2022-02-27 10:32:14.814368+0800 HelloCode[29356:4206590] SceneDelegate3=============================>sceneDidBecomeActive