How to set up a subclass of UIScrollView, and connect it in Interface Builder
First of all I began with the code below in my view controller but for reasons that work for me I needed the below code to be in a separate class. So I created a CustomView class which I've posted below.
At this point is it possible for me to create an instance of this class in my view controller, create an IBOutlet and connect it to a UIScrollView (or some kind of view) in interface builder and get the same kind of behavior, and how would I do something like that?
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)touc开发者_运维百科hesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//Perform an action
}
@end
You can create an instance of CustomView
in IB quite easily. Just drag out a UIScrollView
and position it however you want. Then open the Identity Inspector (cmd+4) and change the class to CustomView
. You can then hook this to an outlet like normal.
Do you really want a UIScrollView inside your UIScrollView?
Your scrollView instance should be nil in your init method, so accessing it won't work as expected.
I am not sure the whole construct / hierarchy is what you want...
精彩评论