【问题标题】:Cannot change UIView background color and UILabel text color in iOS 8无法在 iOS 8 中更改 UIView 背景颜色和 UILabel 文本颜色
【发布时间】:2014-11-30 18:33:58
【问题描述】:

我查看了很多帖子,但似乎都没有帮助解决我当前遇到的问题。我正在尝试通过单击按钮以编程方式更改 UIView 背景颜色和 UILabel 文本颜色。它们都在界面构建器中连接。我所做的是打开 Main.storyboard 旁边的 ViewController.h 文件,然后单击并拖动以连接视图(backgroundView)和标签(letterR)。

如果有人能看出我做错了什么,那就太感谢了!

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{

    IBOutlet UIView *backgroundView;
    IBOutlet UILabel *letterR;
}

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController

static int flag = 0;

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)ButtonClick:(id)sender {

    if(flag == 0){

        flag = 1;
        letterR.textColor = [UIColor blackColor];
        backgroundView.backgroundColor = [UIColor whiteColor];
    }

    else if(flag == 1){

        flag = 0;
        letterR.textColor = [UIColor whiteColor];
        backgroundView.backgroundColor = [UIColor colorWithRed:0 green:1 blue:1 alpha:1];
    }
}

@end

当监控标签和背景的 NSLog 输出时,这是我得到的:

字母 R: 2014-11-26 15:18:18.358 狂欢[3923:340203] 字母 R: >

打印字母的颜色:(null)

背景: 2014-11-26 15:18:18.358 狂欢[3923:340203] 背景:>

打印背景颜色:2014-11-26 15:23:13.467 Rave[3962:342332] 背景颜色:UIDeviceRGBColorSpace 0 1 1 1

背景颜色也正在修改,只是没有在模拟器屏幕上更新。

【问题讨论】:

    标签: ios objective-c iphone uiview uilabel


    【解决方案1】:

    有几件事可能是错误的。您需要检查几件事,首先故事板(或 Xib)和您的 viewController 之间的连接正常。 一件好事是通过代码与背景视图建立联系。 (如果您在 de 视图中添加另一个视图以作为背景,请删除它,如果您只是故事中的视图,则可以)。

    在你的代码中试试这个:

    - (IBAction)ButtonClick:(id)sender {
    
    if(flag == 0){
    
        flag = 1;
        letterR.textColor = [UIColor blackColor];
        self.view.backgroundColor = [UIColor whiteColor];
        NSLog(@"With flag=0, this is firing, The UILable is:%@",[letteR description]);
     }
    
    else if(flag == 1){
    
        flag = 0;
        letterR.textColor = [UIColor whiteColor];
        self.view.backgroundColor = [UIColor colorWithRed:0 green:1 blue:1 alpha:1];
        NSLog(@"With flag=1, this is firing, The UILable is:%@",[letteR description]);
    }
    }
    

    您需要查看 UILabel 是否存在。(答案是否为空。 好吧,这是一个起点,测试一下这个评论以获得更多帮助。

    【讨论】:

      猜你喜欢
      • 2014-09-25
      • 2018-12-25
      • 1970-01-01
      • 2017-05-30
      • 1970-01-01
      • 2013-11-23
      • 1970-01-01
      • 2013-02-22
      • 1970-01-01
      相关资源
      最近更新 更多