为什么我看到黑屏?我没有分配什么东西吗?
在 xcode 中,这是我创建的视图之一:
如果我在模拟器中运行该程序,我可以很好地看到这个视图。但是,如果我创建一个 UIViewController 类并将其与此视图挂钩(我确认此视图是 UIViewController),这就是我在模拟器中得到的内容:
为什么我会看到这个空白屏幕?如何获得与第一张图像类似的屏幕?
编辑:代码
#import "EnterLevelViewController.h"
@interface EnterLevelViewController ()
@end
@implementation EnterLevelViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)loadView
{
// Implement loadView to create a view hierarchy programmatically, without using a nib.
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
In xcode, this is one of the views I have created:
If I run the program in the simulator, I can see this view fine. However, if I create a UIViewController class and hook it up with this view (I confirmed this view is a UIViewController), this is what I get in the simulator:
Why do I get this blank screen? How can I get the screen that looks like the first image?
Edit: code
#import "EnterLevelViewController.h"
@interface EnterLevelViewController ()
@end
@implementation EnterLevelViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)loadView
{
// Implement loadView to create a view hierarchy programmatically, without using a nib.
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从您的代码看来,您已取消注释视图控制器中的
-(void)loadView
方法,该方法在模板中提供。当您执行此操作时,控制器将尝试以编程方式构造视图,而不是使用笔尖(故事板)。完全删除空方法,看看是否有帮助。It appears from your code that you have uncommented the
-(void)loadView
method from the view controller, which is provided in the template. When you do this, the controller will try to construct the view programmatically, instead of using the nib (storyboard). Remove the empty method completely and see if that helps.只需从 tabBarController 拖动到情节提要中的新 ViewController,当您放开时,按下关系 ViewController 的选择。
以下是如何使用三个选项卡进行设置的快速示例,其中一个选项卡包含您的视图:
https://github.com /HubertK/TabBar_Example
just drag from the tabBarController to the new ViewController in the storyboard and when you let go, press the selection for relationship ViewController.
Here is a quick example of how to set it up with three tabs, one with your view:
https://github.com/HubertK/TabBar_Example
一开始作为项目一部分的故事板已经包含视图控制器及其关联的视图。您可以在您发布的故事板图像中看到它 - 它是底部浅灰色背景的图标。该视图控制器被设置为根视图控制器,并且作为项目模板一部分的代码加载该视图控制器及其视图。这就是您在模拟器中看到的。当您添加另一个视图控制器时,您必须将其配置为根控制器。由于它没有连接到它的视图(或者如果有,则该视图为空),因此您将不再看到任何内容。
The storyboard that was part of your project at the outset already included a view controller and its associated view. You can see it in the storyboard image that you posted -- it's the icon at the bottom with the light gray background. That view controller was set up as the root view controller, and the code that came as part of the project template loaded that view controller and its view. That's what you were seeing in the simulator. When you added another view controller, you must have configured it as the root controller instead. Since it doesn't have a view connected to it (or if it does, the view is empty), you no longer see anything.