我正在尝试实际测试如何使用Xcode现在提供的单一视图模板来硬编码接口。
我给了AppDelegate.h一个ivar和UILabel * titleLabel的属性;我的AppDelegate.m代码在 - (void)中声明完成启动:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Set the view controller as the window's root view controller and display.
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
button.titleLabel.font = [UIFont systemFontOfSize: 12];
button.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
button.titleLabel.shadowOffset = CGSizeMake (1.0, 0.0);
[self.window addSubview: button];
}
我获得了成功的编译,但屏幕上没有绘制按钮 - 我只是在模拟器中运行标准空白模板。我如何得到它?
答案 0 :(得分:1)
您的窗口似乎未初始化,并且您的功能过早返回。试试这个:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
// Set the view controller as the window's root view controller and display.
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
button.titleLabel.font = [UIFont systemFontOfSize: 12];
button.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
button.titleLabel.shadowOffset = CGSizeMake (1.0, 0.0);
[self.window addSubview: button];
return YES;
}
请记住return
会立即退出某个函数,并且不会执行其下面的任何代码。