[英]Change UIView (inside nib) background from other viewcontroller
Anyone know how to alter a view inside a nib from another viewcontroller? 有谁知道如何通过另一个ViewController更改笔尖内部的视图? I have a outlet from the view in nib-file to the view class-file and I have @synthesize
the view-class .m
file. 我从nib文件中的视图到视图类文件都有一个出口,并且我有@synthesize
视图类.m
文件。 And then I #import "InfoView.h"
(which is the view class) the view to the viewcontroller and at last I: 然后,我将#import "InfoView.h"
(这是视图类)导入视图到视图控制器,最后我:
InfoView *infoView;
if (infoView == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"infoView" owner:self options:nil];
infoView = [nib objectAtIndex:0];
}
infoView.contentView.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:1];"
But I can't get the background to change. 但是我无法改变背景。
Has anyone tried to do something like this before? 有人曾尝试做过这样的事情吗? I appreciate any input thank you! 感谢您的输入,谢谢!
EDIT: Have addressed the UIColor
issue but that was not the problem. 编辑:解决了UIColor
问题,但这不是问题。
Try this code I have made the demo app for you. 试试这个代码,我已经为您制作了演示应用程序。 create file CustomView.h
创建文件CustomView.h
#import <UIKit/UIKit.h>
@interface CustomView : UIView
@property (nonatomic, strong) IBOutlet UILabel *titleLbl;
@end
CustomView.m. CustomView.m。 If you are using XIB 如果您使用的是XIB
#import "CustomView.h"
@implementation CustomView
@synthesize titleLbl = _titleLbl;
- (id)initWithCoder:(NSCoder *)aDecoder
{
if(self = [super initWithCoder:aDecoder])
{
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:self options:nil];
UIView *theEditView = [nibObjects objectAtIndex:0];
theEditView.frame = self.bounds;
[self addSubview: theEditView];
theEditView = nil;
}
return self;
}
Set
fileOwner
ofCustomView.XIB
is CustomView. 设置fileOwner
的CustomView.XIB
是CustomView。 and connect outlets. 并连接插座。 Where ever you want to useCustomView
take aUIView
object in yourXIB
, and renameUIView
class withCustomView
. 您要使用CustomView
任何地方,请在XIB
获取一个UIView
对象,然后使用CustomView
重命名UIView
类。 Create anIBOutlet
in your.h
file and connect it withCustomView
object inXIB
. 在您的.h
文件中创建IBOutlet
,并将其与XIB
CustomView
对象连接。 Now do this: 现在执行以下操作:
self.customViewObj.backgroundColor = [UIColor redColor];
self.customViewObj.titleLbl.text = @"Prateek";
In your case your customview
object is not created. 在您的情况下,不会创建customview
对象。 if you print your object it will show you nil
. 如果您打印对象,它将显示nil
。
You're using the -colorWithRed:green:blue:alpha
wrongly. 您错误地使用了-colorWithRed:green:blue:alpha
。 They expect a float value between 0 and 1. You need to do: 他们期望浮点值介于0到1之间。您需要执行以下操作:
infoView.contentView.backgroundColor = [UIColor colorWithRed:50.0/255.0 green:50.0/255.0 blue:50.0/255.0 alpha:1];"
如果您要更改背景色,请执行以下操作:
infoView.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:1];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.