1

別のビューコントローラーからペン先内のビューを変更する方法を知っている人はいますか? nib-file のビューからビュー クラス ファイルへのアウトレットがあり、ビュー クラス@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

デモアプリを作成したこのコードを試してください。ファイルを作成CustomView.h

#import <UIKit/UIKit.h>

@interface CustomView : UIView

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

@end

CustomView.m. 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;
}

のセットfileOwnerCustomView.XIBCustomView です。コンセントを接続します。使用したい場所でオブジェクトをCustomView取得し、クラスの名前を. ファイルに を作成し、 のオブジェクトに接続します。今これを行います:UIViewXIBUIViewCustomViewIBOutlet.hCustomViewXIB

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

あなたの場合、customviewオブジェクトは作成されません。オブジェクトを印刷すると、表示されますnil

于 2013-06-01T02:02:49.583 に答える
0

の使い方を-colorWithRed:green:blue:alpha間違っています。彼らは 0 から 1 の間の float 値を期待しています。

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 に答える