Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何设置UIScrollView的子类,并在Interface Builder中连接它_Iphone_Iphone Sdk 3.0_Ios4 - Fatal编程技术网

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

Iphone 如何设置UIScrollView的子类,并在Interface Builder中连接它,iphone,iphone-sdk-3.0,ios4,Iphone,Iphone Sdk 3.0,Ios4,首先,我从我的视图控制器中的以下代码开始,但出于对我有用的原因,我需要将以下代码放在一个单独的类中。所以我创建了一个CustomView类,我在下面发布了这个类 在这一点上,我是否可以在我的视图控制器中创建这个类的实例,创建一个IBOutlet并将其连接到interface builder中的UIScrollView(或某种视图)并获得相同的行为,我将如何做类似的事情 customView.m #import <UIKit/UIKit.h> #import <QuartzCor

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

在这一点上,我是否可以在我的视图控制器中创建这个类的实例,创建一个IBOutlet并将其连接到interface builder中的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;
#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
#导入
#进口
@界面自定义视图:UIScrollView{
UIScrollView*滚动视图;
UIImageView*图像视图;
}
@属性(非原子,保留)IBMOutlet UIScrollView*scrollView;
@属性(非原子,保留)UIImageView*imageView;
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;
#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
#导入
@实现自定义视图
@综合滚动视图、图像视图;
-(id)init{
if(self=[super init]){
//初始化代码
UIImageView*temp=[[UIImageView alloc]initWithImage:[UIImageName:@“myImage.png”];
self.imageView=temp;
[临时释放];
scrollView.contentSize=CGSizeMake(imageView.frame.size.width,imageView.frame.size.height);
//其他滚动视图属性
scrollView.delegate=self;
[滚动视图添加子视图:图像视图];
}
回归自我;
}
-(无效)解除锁定{
[滚动视图发布];
[图像视图发布];
[super dealoc];
}
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件{
//采取行动
}
@结束

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

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

您真的希望在您的UIScrollView中有一个UIScrollView吗

在init方法中,scrollView实例应为nil,因此访问它将无法按预期工作


我不确定整个构造/层次结构是您想要的…

您真的希望在您的UIScrollView中有一个UIScrollView吗

在init方法中,scrollView实例应为nil,因此访问它将无法按预期工作


我不确定整个构造/层次结构是您想要的…

第二个UIScrollView怎么样了?第二个UIScrollView怎么样了?所以我需要在我的视图控制器中创建这个类的实例,然后在viewDidLoad方法中alloc和init吗?您会像对待任何其他IB出口一样对待它。因此,将ivar添加到控制器中,使用
IBOutlet
修饰符添加@property,@合成它,在
viewDidUnload
dealloc>中释放它,最后连接IB中的插座。然后在控制器中激发
viewDidLoad
方法时,该类的实例将被完全实例化并添加到控制器的视图中。所有这些都是在后台发生的,您不需要自己进行alloc init。我已经完成了这项工作,但它似乎没有将我的视图加载到scroll Views中,所以我需要在视图控制器中创建此类的实例,然后在viewDidLoad方法中进行alloc和init吗?您可以像对待任何其他IB出口一样对待它。因此,将ivar添加到控制器中,使用
IBOutlet
修饰符添加@property,@合成它,在
viewDidUnload
dealloc>中释放它,最后连接IB中的插座。然后在控制器中激发
viewDidLoad
方法时,该类的实例将被完全实例化并添加到控制器的视图中。所有这些都是在后台发生的,不需要您自己进行alloc init。我已经做到了这一点,但它似乎没有将我的视图加载到滚动视图中。老实说,我正在尝试创建一个自定义控件,它使用UIScrollView从左到右滑动,相信我,它工作得很好。唯一的问题是,我想为我的控件创建一个自定义类,以便我能够在interface builder中以非常简单的方式连接它,并在Hiding中执行特定操作。您可能应该从UIScrollView继承,或者从常规UIView继承并包含一个,但不是两者都有。老实说,我正在尝试创建一个自定义控件,它使用UIScrollView从左向右滑动,相信我,它工作得很好。唯一的问题是,我想为我的控件创建一个自定义类,以便我能够在interface builder中以非常简单的方式连接它,并在Hiding中执行特定操作。您可能应该从UIScrollView继承,或者从常规UIView继承并包含一个,但不能同时包含两个。