无法更改UIView背景颜色

时间:2014-08-02 17:29:48

标签: ios objective-c uiview uicolor

我有一个iOS应用程序,在我的ViewController中包含一个UIView和一个子UIView。 (在故事板中创建)。

我想在IBAction中更改子视图的背景颜色,但应用程序崩溃了。这是我的代码:

-(IBAction)open_popup {
   [popupView setBackgroundColor:[UIColor colorWithRed:0 green:255 blue:0 alpha:1.0]];
}

更新 - 这就是我宣布弹出式视图的方式

IBOutlet UIView *popupView; // in my header file

更新 - 这是我遇到的错误

2014-08-02 18:20:39.009 ZenApp[1373:60b] 4 
2014-08-02 18:20:39.011 ZenApp[1373:60b] -[NSCFType CGColor]: unrecognized selector sent to instance 0x10c896a60 
2014-08-02 18:20:39.015 ZenApp[1373:60b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType CGColor]: unrecognized selector sent to instance 0x10c896a60' * 

First throw call stack: ( 
0 CoreFoundation 0x0000000101f05495 __exceptionPreprocess + 165 
1 libobjc.A.dylib 0x0000000101c6499e objc_exception_throw + 43 
2 CoreFoundation 0x0000000101f9665d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 
3 CoreFoundation 0x0000000101ef6d8d ___forwarding_ + 973 
4 CoreFoundation 0x0000000101ef6938 _CF_forwarding_prep_0 + 120 
5 UIKit 0x000000010086466c -[UIView(Hierarchy) _setBackgroundColor:] + 37 
6 ZenApp 0x00000001000036f8 -[ViewController change_colour_stage_4] + 168 
7 ZenApp 0x0000000100002e5d -[ViewController change_colours] + 205 
8 ZenApp 0x00000001000030db -[ViewController change_colour_stage_1] + 587 
9 ZenApp 0x0000000100002d17 -[ViewController viewDidLoad] + 3079 
10 UIKit 0x000000010090759e -[UIViewController loadViewIfRequired] + 562 
11 UIKit 0x0000000100907777 -[UIViewController view] + 29 
12 UIKit 0x00000001008502d2 -[UIWindow handleStatusBarChangeFromHeight:toHeight:] + 530 
13 UIKit 0x0000000100851ca6 +[UIWindow _noteStatusBarHeightChanged:oldHeight:forAutolayoutRootViewsOnly:] + 230 
14 UIKit 0x000000010086a92f +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 460 
15 UIKit 0x000000010086abc5 +[UIView(UIViewAnimationWithBlocks) animateWithDuration:animations:completion:] + 59 
16 UIKit 0x0000000100813bf5 -[UIApplication _setStatusBarHidden:animationParameters:changeApplicationFlag:] + 478 
17 UIKit 0x0000000100847c33 -[UIWindow _setHidden:forced:] + 221 
18 UIKit 0x0000000100850ffa -[UIWindow makeKeyAndVisible] + 51 
19 UIKit 0x000000010080cc98 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1788 
20 UIKit 0x0000000100810a0c -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 660 
21 UIKit 0x0000000100821d4c -[UIApplication handleEvent:withNewEvent:] + 3189 
22 UIKit 0x0000000100822216 -[UIApplication sendEvent:] + 79 
23 UIKit 0x0000000100812086 _UIApplicationHandleEvent + 578 
24 GraphicsServices 0x000000010386671a _PurpleEventCallback + 762 
25 GraphicsServices 0x00000001038661e1 PurpleEventCallback + 35 
26 CoreFoundation 0x0000000101e87679 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION + 41 
27 CoreFoundation 0x0000000101e8744e __CFRunLoopDoSource1 + 478 
28 CoreFoundation 0x0000000101eb0903 __CFRunLoopRun + 1939 
29 CoreFoundation 0x0000000101eafd83 CFRunLoopRunSpecific + 467 
30 UIKit 0x00000001008102e1 -[UIApplication _run] + 609 
31 UIKit 0x0000000100811e33 UIApplicationMain + 1010 
32 ZenApp 0x0000000100004413 main + 115 
33 libdyld.dylib 0x00000001034e65fd start + 1 
34 ??? 0x0000000000000001 0x0 + 1 ) 
libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

谢谢你的时间,Dan。

3 个答案:

答案 0 :(得分:0)

您的堆栈跟踪显示:

  

2 CoreFoundation 0x0000000101f9665d - [NSObject(NSObject)   doesNotRecognizeSelector:] + 205 3 CoreFoundation 0x0000000101ef6d8d   ___forwarding_ + 973 4 CoreFoundation 0x0000000101ef6938 _CF_forwarding_prep_0 + 120 5 UIKit 0x000000010086466c - [UIView(Hierarchy)_setBackgroundColor:] + 37

这意味着popupView不知道setBackgroundColor是什么。 (doesNotRecognizeSelector)

检查并确保popupView实际上是一个有效的UIView对象。

答案 1 :(得分:0)

好吧我试过@Tander解决方案但是Xcode建议我像这样访问UIView:" - >"然后它起作用了......我不确定为什么。如果有人知道,如果你能解释为什么它会这样运作,我将不胜感激。

旧代码:

[popupView setBackgroundColor:[UIColor colorWithRed:0.0f/255.0f green:255/255 blue:0/255 alpha:1.0]];

新代码:

[self->popupView setBackgroundColor:[UIColor colorWithRed:0.0f/255.0f green:255/255 blue:0/255 alpha:1.0]];

这就是诀窍。

答案 2 :(得分:0)

即使您可以将图像设置为背景,如下所示:

  1. 对于颜色
    popupView.backgroundColor = [UIColor colorWithRed:0 green:255 blue:0 alpha:1.0];

  2. 用于将图像设置为背景:
    popupView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"ImageName.png"]];