[objective-c]不能改变 UIView 背景颜色

发布时间: 2016/10/2 11:27:49
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有 iOS 应用程序,其中包含 UIView 和子在我的 ViewController UIView。(在创建 Storyboard )。

我想要改变在 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)

感谢您的时间,丹。

解决方法 1:

好吧好吧我试过 @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]];

这是成功的关键。

官方微信
官方QQ群
31647020