2010-05-17 99 views
1

设置背景颜色的UIView我在视图控制器下面的代码:无法从视图控制器

- (void)viewDidLoad { 
    [super viewDidLoad]; 
ThemeManager *themer = [ThemeManager sharedInstance]; 
UIView *theView = self.view; 
UIColor *forBackground = [themer backgroundColour]; 
[theView setBackgroundColor:forBackground]; 
} 

但是当执行获取到setBackgroundColor线,我得到以下错误:

*** -[NSCFNumber CGColor]: unrecognized selector sent to instance 0x1237c40 
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFNumber CGColor]: unrecognized selector sent to instance 0x1237c40' 

有一些简单的事情我做错了,我该如何设置背景颜色?

我必须继承该视图,并在那里做?我不希望有额外的类,尽管这是更好地分离整个模型/视图/控制器的东西。

更新:由[themer backgroundColour]返回的值是使用colorWithPatternImage:构造的,这可能会有所作为吗?

更新:如果我在使用colorWithRed构建的ThemeManager中使用了一个值:green:blue:alpha:,那它工作正常。有没有办法使用带背景图像的颜色来做到这一点?以下作品OK:

[theView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"myimage.png"]]]; 

更新:这个作品也没关系:

UIColor *forBackground = [UIColor colorWithPatternImage:[UIImage imageNamed:@"myimage.png"]]; 
[theView setBackgroundColor:forBackground]; 

在我原来的例子,将对象从[themer backgroundColor]返回是一个UIColor,所以有什么问题呢?

当我步骤通过与调试器:

UIColor *forBackground = [themer backgroundColour]; 

导致forBackground感型的NSConstantValueExpression *

UIColor *forBackground = [UIColor colorWithPatternImage:[UIImage imageNamed:@"myimage.png"]]; 

导致forBackground感型的UIDeviceRGBColor *

这里是ThemeManager的backgroundColour方法的代码:

- (UIColor *)backgroundColour { 
if (backgroundColour == nil) { 
    backgroundColour = [UIColor colorWithPatternImage:[UIImage imageNamed:@"myimage.png"]]; 
} 

return backgroundColour; 
} 

backgroundColour也是实例变量的名称。

回答

2

的问题是,我没有retainUIColorThemeManager中,所以它在第一个视图上工作,但不在后面的视图中。

ThemeManager新代码:

backgroundColour = [[UIColor colorWithPatternImage:[UIImage imageNamed:@"myimage.png"]] retain]; 
0

看来,这条线返回无效的实例:

UIColor *forBackground = [themer backgroundColour]; 

错误说forBackgroundNSCFNumber类,而不是类UIColor类的预期。请检查backgroundColour方法是否返回正确的类型。

更新:

有你的调试器这种方法检查的backgroundColour价值?

- (UIColor *)backgroundColour { 
    if (backgroundColour == nil) { 
     backgroundColour = [UIColor colorWithPatternImage:[UIImage imageNamed:@"myimage.png"]]; 
    } 
    return backgroundColour; 
} 

我怀疑在创建实例时backgroundColour未设置为空。所以测试失败,并且该方法返回一个随机引用。

+0

调试器与含setBackgroundColor – Curyous 2010-05-17 09:31:44

+0

您打造的图案颜色只有backgroundColour是零线的红色箭头停止。你确定backgroundColour是零吗?如果您在创建颜色的线上放置断点,调试器会停止吗? – 2010-05-17 12:06:13

+0

好点,我已经将颜色生成代码移动到了init方法中,无论如何它是一个更好的解决方案,尽管它不是这个问题的根本原因。 – Curyous 2010-05-18 10:06:14

0

我可能看起来好像他们返回的是一个数字而不是一种颜色,也许他们处理RGB十六进制值?我有时使用类方法+(UIColor *)getCustomColor:(CustomColorType)颜色构建customColor类;我一旦确定了应用程序的颜色主题,就可以使用该功能,但它也使您可以在一个地方更换颜色并随时随地改变颜色。我通常以0xFFFFFFFF值来做这件事,因为这是大多数设计师处理颜色的方式。

这是 “复制 - 浪费” 从我的最后一个项目:

// 
// CustomColor.h 
// FC 
// 
// Created by RickiG on 12/19/09. 
// Copyright 2009 www.rickigregersen.com.. All rights reserved. 
// 

#import <Foundation/Foundation.h> 

typedef enum { 

    CustomColorWhiteText, 
    CustomColorDarkGreyText, 
    CustomColorLightGreyText, 
    CustomColorGreyText, 
    CustomColorLightBlueText, 
    CustomColorDarkWhiteText, 
    CustomColorLightWhiteText, 
    CustomColorLightPurpleText, 
    CustomColorOrange, 
    CustomColorRed, 
    CustomColorSilver, 

} CustomColorType; 

@interface CustomColor : NSObject { 

} 

+ (UIColor*) getCustomColor:(CustomColorType) color; 

@end 

和实现:

// 
// CustomColor.m 
// FC 
// 
// Created by RickiG on 12/19/09. 
// Copyright 2009 www.rickigregersen.com.. All rights reserved. 
// 

#import "CustomColor.h" 


@implementation CustomColor 

+ (UIColor*) getCustomColor:(CustomColorType) color { 

    int value; 

    switch (color) { 

     case CustomColorWhiteText: 
      value = 0xffffff; 
      break;   
     case CustomColorDarkGreyText: 
      value = 0x373737; 
      break; 
     case CustomColorGreyText: 
      value = 0x7a7a7a; 
      break; 
     case CustomColorLightGreyText: 
      value = 0xd3d3d3; 
      break;   
     case CustomColorLightBlueText: 
      value = 0x8ed6ff; 
      break; 
     case CustomColorDarkWhiteText: 
      value = 0x979797; 
      break; 
     case CustomColorLightWhiteText: 
      value = 0xe8e8e8; 
      break; 
     case CustomColorLightPurpleText: 
      value = 0xd17efc; 
      break; 
     case CustomColorOrange: 
      value = 0xfb8720; 
      break; 
     case CustomColorRed: 
      value = 0xeb0008; 
      break; 
     case CustomColorSilver: 
      value = 0xe3e3e3; 
      break; 

     default: 
      value = 0x000000; 
      break; 
    } 

    int r, g, b; 
    b = value & 0x0000FF; 
    g = ((value & 0x00FF00) >> 8); 
    r = ((value & 0xFF0000) >> 16); 

    return [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:1.0f]; 
} 

@end 

这样我可以随时去:

[UIView setBackgroundColor:[CustomColor getCustomColor:CustomColorWhiteText]; 

从任何地方在我的项目中。 我有相同类型的文件来处理通过应用程序重复使用的文本标签,按钮和其他界面元素。

希望它帮助,如果你正在构建应该能够改变它的外观上飞一个UI,即使它是你问的不完全是:)

相关问题