簡體   English   中英

無法更改UIView背景顏色

[英]Cannot change UIView background colour

我有一個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)

丹,謝謝您的時間。

您的堆棧跟蹤顯示如下:

2 CoreFoundation 0x0000000101f9665d-[NSObject(NSObject)didNotRecognizeSelector:] + 205 3 CoreFoundation 0x0000000101ef6d8d ___forwarding_ + 973 4 CoreFoundation 0x0000000101ef6938 _CF_forwarding_prep_0 + 120 5 UIKit 0x0000000100_466_Hash(Color)

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

檢查並確保popupView實際上是有效的UIView對象。

好的,我嘗試了@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]];

這成功了。

甚至可以將圖像設置為背景,如下所示:

  1. 對於顏色
    popupView.backgroundColor = [UIColor colorWithRed:0 green:255 blue:0 alpha:1.0];

  2. 要將圖像設置為背景:
    popupView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"ImageName.png"]];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM