如何设置UIScrollView的子类,并在Interface Builder中连接它

cgo*_*ain 1 iphone iphone-sdk-3.0 ios4

首先,我在视图控制器中开始使用下面的代码,但出于对我有用的原因,我需要将下面的代码放在一个单独的类中.所以我创建了一个我在下面发布的CustomView类.

此时,我可以在视图控制器中创建此类的实例,创建一个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;
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

Bra*_*ams 5

您可以CustomView非常轻松地在IB中创建实例.只需拖出一个UIScrollView并根据需要定位即可.然后打开Identity Inspector(cmd + 4)并将类更改为CustomView.然后,您可以将其连接到正常的插座.