新しいiOS" Empty Application
"-templateプロジェクトを開始しました。このコードをapplication:didFinishLaunchingWithOptions:
メソッド内に配置すると、正常に機能します。
CGRect frame = CGRectMake(10, 10, 300, 25);
UILabel *helloLabel = [UILabel new];
[helloLabel setFrame:frame];
helloLabel.text = @"Hello iPhone!";
[self.window addSubview:helloLabel];
しかし、私が本当にやりたいのは、別のクラスで「addHello」クラスメソッドを作成することです。そのため、表示されるのapplication:didFinishLaunchingWithOptions:
は次のとおりです。
[MyOtherClass addHello];
これは私が他のクラスに入れてみたものです:
+ (void) addHello {
CGRect frame = CGRectMake(10, 10, 300, 25);
UILabel *helloLabel = [UILabel new];
[helloLabel setFrame:frame];
helloLabel.text = @"Hello iPhone!";
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
[window addSubview:helloLabel];
}
しかし、それはうまくいきません。私はどうしたらいいですか?