我的UIButton没有被屏幕吸引



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

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

您的窗口似乎未初始化,并且您的函数返回得太早。 试试这个:

- (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会立即退出函数,并且不会执行其下的任何代码。

最新更新