英文:
iOS Simulator showing Blank White Screen When Trying to Create Frame
问题
以下是您提供的文本的中文翻译:
在《Big Nerd Ranch: iOS Programming 4th Edition》教材的第4章中,我们应该在AppDelegate.m文件中创建一个红色矩形,让它出现在视图的中央,然后在模拟器中显示出来。
我已经逐字复制了代码,但当我运行模拟器时,我得到一个空白屏幕,如下所示:
我不确定为什么会这样。我已经在AppDelegate.h文件中设置了window
属性,所以我知道这不是问题所在。
这是AppDelegate.m文件中的代码:
//
// AppDelegate.m
// Hypnosister
//
//
//
#import "AppDelegate.h"
#import "BNRHypnosisView.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]];
CGRect firstFrame = CGRectMake(160, 240, 100, 150);
BNRHypnosisView *firstView = [[BNRHypnosisView alloc]initWithFrame:firstFrame];
firstView.backgroundColor = [UIColor redColor];
[self.window addSubview:firstView];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
#pragma mark - UISceneSession lifecycle
- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];
}
- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
@end
请注意,我只提供了代码的翻译部分,没有其他内容。
英文:
In Chapter 4 of the Big Nerd Ranch: iOS Programming 4th Edition textbook where we're supposed to create a red rectangle appear in the middle of the view in the AppDelegate.m file and have it appear in the simulator.
I've copied the code word for word and when I run the simulator I get a blank screen like this:
I'm not sure why this is happening. I've already put the property for window
in the AppDelegate.h file so I know that's not why.
This is the code in the AppDelegate.m file:
//
// AppDelegate.m
// Hypnosister
//
//
//
#import "AppDelegate.h"
#import "BNRHypnosisView.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]];
CGRect firstFrame = CGRectMake(160, 240, 100, 150);
BNRHypnosisView *firstView = [[BNRHypnosisView alloc]initWithFrame:firstFrame];
firstView.backgroundColor = [UIColor redColor];
[self.window addSubview:firstView];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
#pragma mark - UISceneSession lifecycle
- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];
}
- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
@end
答案1
得分: 1
如果您使用应用程序场景,您在应用程序委托中手动创建的窗口将被忽略(确切地说,它在场景使自己的窗口成为关键窗口之前的非常短的时间内保持关键窗口状态)。因此,您应该将您在场景委托的-[UISceneDelegate scene:willConnectToSession:options:]
方法中创建的窗口附加到那里:
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
UIWindow *window = [[UIWindow alloc] initWithWindowScene:(UIWindowScene *)scene];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(160, 240, 100, 150)];
view.backgroundColor = UIColor.redColor;
[window addSubview:view];
window.backgroundColor = UIColor.whiteColor;
[window makeKeyAndVisible];
self.window = window;
}
..或者在您的应用程序中摆脱场景委托(您可以参考此问题下的讨论以获取更多详细信息),并且让它继续使用老好的应用程序委托的窗口。请注意,在这种情况下,如果没有指定rootViewController
,您将无法使用窗口,所以您至少需要设置一个虚拟控制器:
_window.rootViewController = [UIViewController new];
英文:
If you use an application scene, the window you create manually in the application delegate is ignored (to be precise it remains the key window for very short period of time, until the scene makes its own window the key one). So you either should attach the window you created in the scene delegate's -[UISceneDelegate scene:willConnectToSession:options:]
method:
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
UIWindow *window = [[UIWindow alloc] initWithWindowScene:(UIWindowScene *)scene];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(160, 240, 100, 150)];
view.backgroundColor = UIColor.redColor;
[window addSubview:view];
window.backgroundColor = UIColor.whiteColor;
[window makeKeyAndVisible];
self.window = window;
}
..OR get rid of the scene delegate in your application (you may refer to the discussion under this question for more details on how to do it) and make it work with the old good application delegate's window. Be advised that in this case you won't be able to use the window without having the rootViewController
specified, so you at least need to set a dummy controller:
_window.rootViewController = [UIViewController new];
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论