应用程序启动时加载视图

发布于 2024-12-18 18:20:28 字数 938 浏览 2 评论 0原文

我想创建一个只有一个视图的应用程序(TestViewController.h TestViewController.m)。 (没有标签栏,没有导航栏)不知道为什么在我启动应用程序后,屏幕完全黑了。看来应用程序没有成功加载视图?因为如果加载视图,屏幕应该是白色的。我说得对还是不对?

这是 AppDelegate.h

#import <UIKit/UIKit.h>
@class TestViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    UIWindow *window;
    TestViewController *testrViewController;   
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) TestViewController *testViewController;

@end

这是 AppDelegate.m

#import "AppDelegate.h"
#import "TestViewController.h"

@implementation AppDelegate

@synthesize window = window;
@synthesize testViewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self.window addSubview:testViewController.view];
    [self.window makeKeyAndVisible];
    return YES;
}

I want to create a app with only one view(TestViewController.h TestViewController.m). (no Tabbar, no Navigation Bar) Don't know why after i launch the app, the screen is totally black. It seems that the app did not load the view successfully? Since if the view is loaded, the screen should be white. Am I right or not?

Here's AppDelegate.h

#import <UIKit/UIKit.h>
@class TestViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    UIWindow *window;
    TestViewController *testrViewController;   
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) TestViewController *testViewController;

@end

Here's AppDelegate.m

#import "AppDelegate.h"
#import "TestViewController.h"

@implementation AppDelegate

@synthesize window = window;
@synthesize testViewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self.window addSubview:testViewController.view];
    [self.window makeKeyAndVisible];
    return YES;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

勿忘初心 2024-12-25 18:20:28

在我看来,你并没有开设课程

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 // add this line
 testViewController = [[TestViewController alloc] init];
 [.....]
 }

希望它有帮助

It seems to me that you're not instatiating the class

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 // add this line
 testViewController = [[TestViewController alloc] init];
 [.....]
 }

Hope it helps

指尖微凉心微凉 2024-12-25 18:20:28

如果您以编程方式创建它,那么您还应该实例化 window

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

然后实例化 ViewController

testViewController = [[TestViewController alloc] init];

,然后使其可见

[self.window addSubview:testViewController.view];
    [self.window makeKeyAndVisible];

if you are creating it programmatically, then you should also instantiate window

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

then your ViewController

testViewController = [[TestViewController alloc] init];

and then make it visible

[self.window addSubview:testViewController.view];
    [self.window makeKeyAndVisible];
心在旅行 2024-12-25 18:20:28

您是否为 testViewController 创建了 .xib?如果没有,那么您必须在 testViewController 上添加一个子视图。然后尝试。我希望它会起作用。

UIView *testView=[[UIView alloc]initwithFrame:CGRectMake(0,0,320,480)];

[testViewController addsubview:testView];
[self.window addSubview:testViewController.view];
[self.window makeKeyAndVisible];

did you create .xib for testViewController. If not then you have to add a subview over your testViewController. and then try. I hope it will work.

UIView *testView=[[UIView alloc]initwithFrame:CGRectMake(0,0,320,480)];

[testViewController addsubview:testView];
[self.window addSubview:testViewController.view];
[self.window makeKeyAndVisible];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文