UIView没有出现在UIScrollView中

我目前正在动态地向我的UIScrollView对象添加一个UIImage,一切都按预期工作。 我现在需要添加额外的function(当它从互联网加载时,UIActivityIndi​​cator位于图像的中心,下面是一个标签)所以我想为什么不创建一个自定义视图,其中包含所有这些,因为我需要它们是,然后只需将其加载到scrollView中。 我遇到了一个问题,当我将它添加到scrollView时,没有任何显示。 这是我有的:

NewsViewController.m:

imageScrollView.contentSize = CGSizeMake(320*numberOfPages, 303); pageControl.numberOfPages = numberOfPages; dispatch_queue_t imageDownload = dispatch_queue_create("imageDownload", NULL); __block NSData *temp; CustomImageView *customImageView = [[CustomImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 303)]; [imageScrollView addSubview:customImageView]; [[customImageView activityIndicator] startAnimating]; dispatch_async(imageDownload, ^{ temp = [[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://sofzh.miximages.com/iphone/farm.jpg"]]retain]; dispatch_async(dispatch_get_main_queue(), ^{ customImageView.imageView.image = [[UIImage alloc] initWithData:temp]; [[customImageView activityIndicator] stopAnimating]; [customImageView setNeedsDisplay]; customImageView.caption.text = @"HAHAHAHAHHAHA"; [imageScrollView setNeedsDisplay]; [temp release]; }); }); dispatch_release(imageDownload); 

CustomImageView.h:

 #import  @interface CustomImageView : UIView { IBOutlet UIImageView *imageView; IBOutlet UILabel *caption; IBOutlet UIActivityIndicatorView *activityIndicator; } @property (nonatomic, retain) IBOutlet UIImageView *imageView; @property (nonatomic, retain) IBOutlet UILabel *caption; @property (nonatomic, retain) IBOutlet UIActivityIndicatorView *activityIndicator; @end 

CustomImageView.m

 #import "CustomImageView.h" @interface CustomImageView () @end @implementation CustomImageView @synthesize caption, imageView, activityIndicator; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // eg self.myOutlet = nil; } - (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } @end 

我包括我的XIB文件的屏幕截图,以及在模拟器上运行的程序。 第一张图片在scrollView(通过使用我的自定义类进行的尝试)中没有显示任何内容,滚动视图的第二页是通过向UIScrollView(工作)添加UIImageView进行的尝试。 谁能指出我做错了什么? 我不允许将自定义视图加载到UIScrollView中吗? 谢谢你的帮助!

IB截图 – http://sofzh.miximages.com/iphone/gz9UL.png

iOS模拟器无图像与CustomView截图 – http://sofzh.miximages.com/iphone/zhswq.png

iOS模拟器图像与UIImageView截图 – http://sofzh.miximages.com/iphone/97vmU.png

看起来好像CustomImageViewUIViewController上的子类,而不是UIImageViewUIView 。 你不能像这样添加UIViewController作为子视图。 将它更改为子类UIView ,它应该工作。

要从.nib加载UIView ,您需要像对UIViewController一样声明您的IBOutlet属性。 在IB中定义正确的自定义类并连接所有内容,包括基本视图。 然后在自定义视图类中覆盖以下方法。

 - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code. // [[NSBundle mainBundle] loadNibNamed:@"" owner:self options:nil]; [self addSubview:self.view]; } return self; } 

您似乎已将UIViewController子类中的方法剪切并粘贴到UIView子类中。 首先删除@synthesize@end之间粘贴的所有方法。 UIView子类需要从.nib加载不同的方法,因此您需要覆盖上面显示的方法并使用代码行

[[NSBundle mainBundle] loadNibNamed:@"" owner:self options:nil];

加载笔尖。

关于连接基本视图的评论,这就是我的意思:

创建自定义类和nib文件后,打开笔尖并突出显示左侧的“文件所有者”,然后在右上角,从左侧选择第3个图标,看起来像ID卡或其他内容。 在“类”框中,添加自定义类的名称。

在customClass中,添加一个名为baseView的IBOutlet UIView属性,并在根级别添加一个覆盖IB视图的UIView,将这两个连接起来。 然后在其上添加其他所有内容

您的代码现在看起来像这样:

CustomImageView.h

 #import  @interface CustomImageView : UIView { IBOutlet UIView *baseView IBOutlet UIImageView *imageView; IBOutlet UILabel *caption; IBOutlet UIActivityIndicatorView *activityIndicator; } @property (nonatomic, retain) IBOutlet UIView *baseView; @property (nonatomic, retain) IBOutlet UIImageView *imageView; @property (nonatomic, retain) IBOutlet UILabel *caption; @property (nonatomic, retain) IBOutlet UIActivityIndicatorView *activityIndicator; @end 

CustomImageView.m

 #import "CustomImageView.h" @interface CustomImageView () @end @implementation CustomImageView @synthesize baseView, caption, imageView, activityIndicator; - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code. // [[NSBundle mainBundle] loadNibNamed:@"" owner:self options:nil]; [self addSubview:self.baseView]; } return self; } @end 

如果你在IB中连接它,它应该工作正常,只需记住添加baseView

如前所述, initWithNibName属于控制器。
我认为,你走错了路。

你有一个ViewController,并希望添加一个customView,从URLData动态加载图像。

你有2个选项:
选项1:在IB中做一切:
在Interfacebuilder中编辑/(或添加一个新的)ViewController的XIB文件并添加您喜欢的视图。 该文件的所有者是UIViewController类/子类。 做你以前做的所有事情,除了在视图控制器中做。 在你的App委托initWithNibName你的ViewController就像这样

  self.viewController = [[testViewController alloc] initWithNibName:@"testViewController" bundle:nil]; 

如果您愿意,请初始化您自己的视图控制器,您希望将其推入并将其推入堆栈中。

您做错了什么:您正在使用Frame初始化customImageView,但是nib不会自动加载。 属性在那里,但笔尖不是。

如果你真的想要一个稳定的东西选择
选项2:以编程方式执行所有操作 (更简单,更理解,更轻松):
从您的实施中我们执行以下操作:

NewsViewController.m

像你之前做的那样!

CustomImageView.h:

 #import  @interface CustomImageView : UIView { UIImageView *imageView; UILabel *caption; UIActivityIndicatorView *activityIndicator; } @property (nonatomic, retain) UIImageView *imageView; @property (nonatomic, retain) UILabel *caption; @property (nonatomic, retain) UIActivityIndicatorView *activityIndicator; @end 

CustomImageView.m:

 - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code caption = [[UILabel alloc] initWithFrame:CGRectMake(0, 250, 320, 50)]; [caption setBackgroundColor:[UIColor greenColor]]; imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 250)]; [imageView setBackgroundColor:[UIColor blueColor]]; activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; self.activityIndicator.frame = CGRectMake(320/2-25, 460/2-25, 50, 50); [self.activityIndicator startAnimating]; [self addSubview:self.caption]; [self addSubview:self.imageView]; [self addSubview:self.activityIndicator]; } return self; } 

然后做你以前做过的一切。 那样做会更好