gpt4 book ai didi

iphone - 从其他 View Controller 更改 UIView(在 Nib 内)背景

转载 作者:行者123 更新时间:2023-11-29 10:59:06 25 4
gpt4 key购买 nike

有人知道如何从另一个 View Controller 更改 Nib 内的 View 吗?我有一个从 nib 文件中的 View 到 View 类文件的导出,并且我有 @synthesize View 类 .m 文件。然后我 #import "InfoView.h" (这是 View 类) View 到 View Controller ,最后我:

    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 问题,但这不是问题所在。

最佳答案

试试这段代码,我已经为您制作了演示应用程序。创建文件 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";

在您的情况下,您的 customview 对象未创建。如果你打印你的对象,它会显示你 nil

关于iphone - 从其他 View Controller 更改 UIView(在 Nib 内)背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16861206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com