-1

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

Herman
  • 1
  • 1
  • The code in your didFinishLaunchingWithOptions needs to be ripped out and moved into the scene delegate. See https://stackoverflow.com/questions/59560742/ios13-app-black-screen-self-window-not-working – matt Feb 27 '22 at 02:45
  • Thanks Matt. I've moved the code from AppDelegate to SceneDelegate , but still ran into the blackscreen. Not sure anything else I should make change? – Herman Feb 27 '22 at 07:54
  • `self.window = [[UIWindow alloc] initWithFrame:` is wrong. Read my example code more carefully. See https://stackoverflow.com/a/58405507/341994 (sorry it's not Objective-C) – matt Feb 27 '22 at 11:05
  • Thanks for the help here, really appreciate that. – Herman Feb 27 '22 at 15:22

1 Answers1

-1

The Black Screen disappeared - issue solved by making the change like below. But I do NOT quite understand the difference/reason, any kind explanation is highly appreciated for new bird like me :-)

Original code(black screen displayed) in SceneDelegate.m

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

    RootViewController *rootViewController  = RootViewController.new;
    rootViewController.view.backgroundColor = UIColor.greenColor;
    self.window.rootViewController = rootViewController;
    [self.window makeKeyAndVisible];

Current code(page is properly rendered) in SceneDelegate.m

    UIWindowScene *windowScene = (UIWindowScene *)scene;
    self.window = [[UIWindow alloc] initWithWindowScene:windowScene];
    self.window.frame = windowScene.coordinateSpace.bounds;

    RootViewController *rootViewController  = RootViewController.new;
    rootViewController.view.backgroundColor = UIColor.greenColor;
    self.window.rootViewController = rootViewController;
    [self.window makeKeyAndVisible];
Herman
  • 1
  • 1
  • All you're doing here is what everyone knows you should do (my answer and many others tell you), so there is no need for this answer. And you don't get to ask another question as part of your answer. Your best move would be just to delete your question and move on. – matt Feb 27 '22 at 13:52