我已经开始了一个新的 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];
}
但这不起作用。我应该做些什么?