我目前正在向我的UIScrollView对象动态添加一个UIImage,一切都在按预期进行。我现在需要添加额外的功能(从互联网加载时在图像中心的UIActivityIndicator,以及下面的标签),所以我想为什么不创建一个自定义视图,所有这些都按照我需要的方式进行布局,然后直接加载到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://www.wlfarm.org/wp-content/uploads/2012/05/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 <UIKit/UIKit.h>
@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;
@endCustomImageView.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.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end我包含了我的XIB文件的屏幕截图,以及在模拟器上运行的程序。第一张图片没有显示scrollView中的任何内容(通过使用我的自定义类进行的尝试),滚动视图的第二个页面是通过向UIScrollView添加UIImageView进行的尝试(成功了)。谁能指出我做错了什么?是否不允许我将自定义视图加载到UIScrollView中?谢谢你的帮忙!
IB屏幕截图- http://i.stack.imgur.com/gz9UL.png
带有CustomView屏幕截图的iOS模拟器无图像- http://i.stack.imgur.com/zhswq.png
带有UIImageView屏幕截图的iOS模拟器图像- http://i.stack.imgur.com/97vmU.png
发布于 2012-07-24 17:42:15
看起来好像CustomImageView是UIViewController上的一个子类,而不是UIImageView或UIView。你不能像那样添加一个UIViewController作为一个子视图。将其更改为UIView子类,它应该可以工作。
要从.nib加载UIView,需要像声明UIViewController一样声明IBOutlet属性。在IB中定义正确的自定义类,并将所有内容连接起来,包括基本视图。然后在您的自定义视图类中覆盖以下方法。
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code.
//
[[NSBundle mainBundle] loadNibNamed:@"<NIB NAME HERE>" owner:self options:nil];
[self addSubview:self.view];
}
return self;
}您似乎已经将方法从UIViewController子类剪切并粘贴到您的UIView子类中。首先删除在@synthesize和@end之间粘贴的所有方法。UIView子类需要不同的方法才能从.nib加载,因此您需要覆盖上面显示的方法,并使用代码行
[[NSBundle mainBundle] loadNibNamed:@"<NIB NAME HERE>" owner:self options:nil];
加载笔尖。
对于你关于连接基础视图的评论,下面是我的意思:
创建自定义类和nib文件后,打开nib并突出显示左侧的"Files Owner“,然后在右上角,选择左侧第3个图标,看起来像身份证或其他东西。在" class“框中,添加自定义类的名称。
在您的customClass中,添加一个名为baseView的IBOutlet UIView属性,并在根级别添加一个覆盖IB中视图的UIView,将这两者连接起来。然后在此基础上添加其他所有内容
您的代码现在将如下所示:
CustomImageView.h
#import <UIKit/UIKit.h>
@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;
@endCustomImageView.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:@"<NIB NAME HERE>" owner:self options:nil];
[self addSubview:self.baseView];
}
return self;
}
@end如果您在IB中将其全部连接起来,则应该可以正常工作,只需记住添加baseView
发布于 2012-07-24 18:57:13
如前所述,initWithNibName属于控制器。
在我看来,你走错路了。
您有一个ViewController,并且想要添加一个customView,以便从URLData动态加载图像。
您有两个选项:
选项1:在IB中执行所有操作:
在Interfacebuilder中,编辑/(或添加一个新的)ViewController的XIB文件,并添加您喜欢的视图。文件的所有者是一个UIViewController类/子类。像以前一样做所有的事情,除了在视图控制器中做。在你的应用委派中,initWithNibName你的ViewController如下
self.viewController = [[testViewController alloc] initWithNibName:@"testViewController" bundle:nil];如果你愿意,初始化你自己的视图控制器,你喜欢把它推入堆栈。
错误之处:您正在使用框架初始化customImageView,但nib不会自动加载。属性在那里,但笔尖不在那里。
如果你真的想要一个稳定的东西,选择
选项2:通过编程完成所有工作(更简单、更容易理解、更轻量级):
在您的实现中,我们执行以下操作:
NewsViewController.m
就像你之前做的那样!
CustomImageView.h:
#import <UIKit/UIKit.h>
@interface CustomImageView : UIView
{
UIImageView *imageView;
UILabel *caption;
UIActivityIndicatorView *activityIndicator;
}
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) UILabel *caption;
@property (nonatomic, retain) UIActivityIndicatorView *activityIndicator;
@endCustomImageView.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;
}然后做你之前做过的所有事情。那样会做得更好
https://stackoverflow.com/questions/11627152
复制相似问题