1

我正在尝试实际测试如何仅使用 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];

}

我编译成功,但屏幕上没有绘制按钮——我只是让标准空白模板在模拟器中运行。我如何让它绘制?

4

1 回答 1

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立即退出一个函数,并且它下面的任何代码都不会被执行。

于 2012-08-13T04:37:51.433 回答