如何设置UIScrollView的子类,并在Interface Builder中将其连接起来
首先,我从下面的代码开始,在我的视图控制器中,但由于我工作的原因,我需要将下面的代码放在单独的类中。所以我创建了一个我在下面发布的CustomView类。如何设置UIScrollView的子类,并在Interface Builder中将其连接起来
在这一点上,我有可能在我的视图控制器中创建这个类的一个实例,创建一个IBOutlet并将它连接到界面生成器中的UIScrollView(或某种视图),并获得相同类型的行为,我将如何做这样的事情?
customView.m
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface CustomView : UIScrollView <UIScrollViewDelegate> {
UIScrollView *scrollView;
UIImageView *imageView;
}
@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
@property (nonatomic, retain) UIImageView *imageView;
customView.m
#import <UIKit/UIKit.h>
@implementation CustomView
@synthesize scrollView, imageView;
- (id)init {
if (self = [super init]) {
// Initialization code
UIImageView *temp = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myImage.png"]];
self.imageView = temp;
[temp release];
scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
//Other Scroll View Properties
scrollView.delegate = self;
[scrollView addSubview:imageView];
}
return self;
}
- (void)dealloc {
[scrollView release];
[imageView release];
[super dealloc];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//Perform an action
}
@end
您可以在IB创建CustomView
一个实例很容易。只需拖出UIScrollView
然后将其定位即可。然后打开Identity Inspector(cmd + 4)并将类更改为CustomView
。然后,您可以像正常一样将其钩到插座上。
所以我需要在我的视图控制器中创建这个类的实例,然后在alloc和init中声明viewDidLoad方法? – cgossain 2010-11-17 16:34:57
你会像任何其他IB店一样对待它。因此,将伊娃添加到您的控制器中,使用'IBOutlet'修饰符添加@property,@synthesize,在'viewDidUnload'和'dealloc'中释放它,最后连接IB中的插座。然后等到viewDidLoad方法在你的控制器中触发时,这个类的一个实例将被完全实例化并添加到控制器的视图中。所有这些都发生在后台,不需要你自己alloc-init。 – 2010-11-17 16:48:26
我已经完成了,但它似乎不是加载我的视图滚动视图 – cgossain 2010-11-17 17:01:02
你真的想在你的UIScrollView中使用UIScrollView吗?
您的scrollView实例应该在您的init方法中为零,因此访问它不会按预期工作。
我不确定整个结构/层次结构是你想要的...
第二个UIScrollView怎么了? – Brandon 2010-11-17 16:23:37