1

任何人都知道如何从另一个视图控制器更改笔尖内的视图?我有一个从 nib 文件中的视图到视图类文件的出口,并且我有@synthesize视图类.m文件。然后我#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];" 

但我无法改变背景。

有没有人尝试过这样的事情?感谢您的任何意见,谢谢!

编辑:已经解决了这个UIColor问题,但这不是问题。

4

3 回答 3

2

Try this code I have made the demo app for you. create file CustomView.h

#import <UIKit/UIKit.h>

@interface CustomView : UIView

@property (nonatomic, strong) IBOutlet UILabel *titleLbl;

@end

CustomView.m. If you are using 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 of CustomView.XIB is CustomView. and connect outlets. Where ever you want to use CustomView take a UIView object in your XIB, and rename UIView class with CustomView. Create an IBOutlet in your .h file and connect it with CustomView object in XIB. Now do this:

self.customViewObj.backgroundColor = [UIColor redColor];
self.customViewObj.titleLbl.text = @"Prateek";

In your case your customview object is not created. if you print your object it will show you nil.

于 2013-06-01T02:02:49.583 回答
0

你用-colorWithRed:green:blue:alpha错了。他们期望一个介于 0 和 1 之间的浮点值。您需要执行以下操作:

infoView.contentView.backgroundColor = [UIColor colorWithRed:50.0/255.0 green:50.0/255.0 blue:50.0/255.0 alpha:1];" 
于 2013-05-31T15:53:29.027 回答
0

如果您尝试更改背景颜色..然后执行以下操作:

infoView.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:1];
于 2013-05-31T17:34:48.227 回答