我目前动态地添加一个UIImage我的UIScrollView对象,一切都如预期运行。 我现在需要添加额外的功能(图像的中心UIActivityIndicator当它从互联网加载,和下方的标签),所以我想为什么不创建具有所有这些奠定了一个自定义视图,因为我需要他们是,然后只需将其加载到滚动视图。 我虽然已经遇到了一个问题,当我把它添加到滚动视图,什么也不显示。 以下是我有:
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;
@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.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
我包括我的XIB文件的截图,并在模拟器上运行的程序。 第一个画面显示没有在滚动视图(通过使用我的自定义类所作的尝试),并滚动视图的第二页是通过添加一个UIImageView到的UIScrollView(这工作)作出的尝试。 任何人都可以指出我在做什么错? 难道我不允许加载自定义视图到一个UIScrollView? 谢谢你的帮助!
IB截图- http://i.stack.imgur.com/gz9UL.png
iPhone模拟器没有与CustomView截图图片- http://i.stack.imgur.com/zhswq.png
iPhone模拟器图像的UIImageView的截图- http://i.stack.imgur.com/97vmU.png
看起来好像CustomImageView
是一个子类UIViewController
,不是UIImageView
或UIView
。 您不能添加UIViewController
作为这样的一个子视图。 改变它的子类UIView
,它应该工作。
要加载UIView
从笔尖,你需要声明你IBOutlet
,你会为一个属性UIViewController
。 定义正确的自定义类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
子类需要不同的方法来从笔尖加载,所以需要覆盖上述代替所示的方法和使用的代码行
[[NSBundle mainBundle] loadNibNamed:@"<NIB NAME HERE>" owner:self options:nil];
加载笔尖。
继你有关连接基视点评论,这里就是我的意思是:
创建自定义类和笔尖文件后,打开笔尖,并强调“文件的所有者”在左边,然后在顶部右侧,然后从左侧的图标3,看起来像一个身份证什么的。 在“类”中,添加自定义类的名称。
在你customClass,添加一个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;
@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:@"<NIB NAME HERE>" owner:self options:nil];
[self addSubview:self.baseView];
}
return self;
}
@end
只要你这一切连接在IB应该做工精细,只是记得要加基本视点
As mentioned earlier initWithNibName
belongs to controllers.
As I think, you're going the wrong way.
you have a ViewController and want to add a customView, to load dynamically a image from URLData.
You have 2 Options:
Option 1: Do everything in IB:
In Interfacebuilder edit/(or add a new one) the ViewController's XIB File and add the views that you like. The file's owner is a UIViewController Class/Subclass. Do everything like you did before, except doing it in the view controller. In your App delegate initWithNibName your ViewController like so
self.viewController = [[testViewController alloc] initWithNibName:@"testViewController" bundle:nil];
If you want to, initialize your own view controller, which you like to push and push it in the stack.
What you're doing wrong: You are initializing a customImageView with a Frame, but the nib doesn't get loaded automatically. The Properties are there, but the nib isn't.
If you really want a stable thing choose
Option 2: Do everything programatically (It is easier, more understanding and lightweight):
From your implementation we do the following:
NewsViewController.m
Do like you did before!!!
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;
@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;
}
Then do everything you did before.
That would do a better job