简体   繁体   English

无法在iOS 8中更改UIView背景颜色和UILabel文本颜色

[英]Cannot change UIView background color and UILabel text color in iOS 8

I've looked at many posts and none seem to help with the current issue I'm experiencing. 我看过很多帖子,似乎都无法解决我遇到的当前问题。 I am trying to change the UIView background color and UILabel text color programmatically on a button click. 我试图以编程方式在单击按钮时更改UIView背景颜色和UILabel文本颜色。 They are all connected in the interface builder. 它们都在接口构建器中连接。 What I did was open the ViewController.h file next to the Main.storyboard and clicked and dragged to connect the view (backgroundView) and label (letterR). 我所做的是打开Main.storyboard旁边的ViewController.h文件,然后单击并拖动以连接视图(backgroundView)和标签(letterR)。

If anyone can see what I am doing wrong that would be great thanks! 如果有人看到我在做什么错,那将非常感谢!

ViewController.h ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{

    IBOutlet UIView *backgroundView;
    IBOutlet UILabel *letterR;
}

@end

ViewController.m 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

When monitoring the NSLog output of the label and background this is what I am getting: 当监视标签和背景的NSLog输出时,这就是我得到的:

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

Printing the color of the letter:(null) 印刷字母的颜色:(空)

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

Printing the color of the background:2014-11-26 15:23:13.467 Rave[3962:342332] Background Color: UIDeviceRGBColorSpace 0 1 1 1 打印背景色:2014-11-26 15:23:13.467 Rave [3962:342332]背景色:UIDeviceRGBColorSpace 0 1 1 1

Also the background color is being modified just not updated on screen of the simulator. 同样,背景色正在修改,只是没有在模拟器的屏幕上更新。

There are several things than could be wrong. 有几件事可能是错误的。 You need to check several things, first the connection between storyboard (or Xib) and your viewController are Ok. 您需要检查几件事,首先,情节提要(或Xib)与viewController之间的连接正常。 A good things its make the connection with the background view all by code. 一件好事使所有与后台视图的连接都通过代码实现。 (If you are adding another view inside de view in order to be the background delete it, If you are only a View in story is Ok). (如果要在背景视图中添加另一个视图以作为背景,请删除它,如果您只是故事中的视图就可以)。

Try this in your code: 在您的代码中尝试以下操作:

- (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]);
}
}

You need to see than the UILabel exist.(And the answer is no null. Well, This is a start point, test this a comment for more help. 您需要查看UILabel是否存在。(答案不是空。嗯,这是一个起点,请对此注释进行测试以获取更多帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM