4

Xcode 4.5 と iOS 6.0 を使用しています。xcode からデフォルトのシングル ビュー テンプレートを選択しました。アプリケーション ウィンドウにラベルを追加したいだけですが、それができません。私を修正してください。

- (BOOL)application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController"        bundle:nil];
self.window.rootViewController = self.viewController;
label =[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 20)];
label.text = @"Test";
[label setBackgroundColor:[UIColor whiteColor]];
[self.window addSubview:label];
[self.window bringSubviewToFront:label];
[self.window makeKeyAndVisible];
return YES;
}

PS - ViewController ビューの上、つまりウィンドウ上にラベルを配置したいので、ウィンドウによって表示されるビューが変更されても、ラベルは常にそこに表示されます。ここにラベルだけを表示したくありません。

私は答えを得た

[self.window.rootViewController.view addSubview:label];

ポインタを与えてくれてありがとう。

4

2 に答える 2

4

を削除するだけRootviewControllerです。

- (BOOL)application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.

label =[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 20)];
label.text = @"Test";
[label setBackgroundColor:[UIColor whiteColor]];
[self.window addSubview:label];
[self.window bringSubviewToFront:label];
[self.window makeKeyAndVisible];
return YES;
}

ここにラベルのみを表示したくない場合は、以下のように使用します。

self.viewController = [[ViewController alloc] initWithNibName:@"ViewController"        bundle:nil];
self.window.rootViewController = self.viewController;


[self.Viewcontroller.view addSubview:label];
于 2012-11-22T10:45:54.103 に答える
3

self.window の代わりに self.window.rootViewController.view にラベルを追加します

UILabel *label =[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 20)];
label.text = @"Test"; [label setBackgroundColor:[UIColor whiteColor]];
[self.window.rootViewController.view addSubview:label];
于 2012-11-22T10:44:05.970 に答える