Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.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
Objective c Obj-C,iOS,如何编写/使用包含ToucheSBegind和touchesMoved等的标准类?_Objective C - Fatal编程技术网

Objective c Obj-C,iOS,如何编写/使用包含ToucheSBegind和touchesMoved等的标准类?

Objective c Obj-C,iOS,如何编写/使用包含ToucheSBegind和touchesMoved等的标准类?,objective-c,Objective C,我将在几个视图中使用与视图控制器中相同的代码,我不想将代码粘贴到每个视图中 有人能给我看几行代码,告诉我怎么做吗 我猜我必须在接口中声明类的一个实例,并为我将要进行子分类的每个触摸功能放置一个hand-back方法?创建一个适当的超类,该超类将需要进行子分类,以便执行您需要它的特定任务。这是OOP如此棒的众多原因之一:P 例如,在objective-c中创建的每个对象都是NSObject的子类,NSObject包含标准方法,如release retain和init,它们最初创建对象 如果你想编写

我将在几个视图中使用与视图控制器中相同的代码,我不想将代码粘贴到每个视图中

有人能给我看几行代码,告诉我怎么做吗


我猜我必须在接口中声明类的一个实例,并为我将要进行子分类的每个触摸功能放置一个hand-back方法?

创建一个适当的超类,该超类将需要进行子分类,以便执行您需要它的特定任务。这是OOP如此棒的众多原因之一:P

例如,在objective-c中创建的每个对象都是NSObject的子类,NSObject包含标准方法,如release retain和init,它们最初创建对象


如果你想编写一个游戏,你可以创建一个名为object的超类,它将被子类化,用于指定某些类型的对象,如道具或碎布玩偶。

创建一个适当的超类,它将需要被子类化,以便执行你需要它的特定任务。这是OOP如此棒的众多原因之一:P

例如,在objective-c中创建的每个对象都是NSObject的子类,NSObject包含标准方法,如release retain和init,它们最初创建对象


如果你想编写一个游戏,你可以创建一个名为object的超类,它将被子类化,用于指定某些类型的对象,如道具或碎布玩偶。

如果我正确理解了这个问题,你希望为ToucheSBegind、touchesMoved执行相同的代码位。。。从你的每一个观点

我这样做的方式是使用委托模式

@protocol ViewTouchDelegate
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
@end

@interface ViewA : UIView {
    id <ViewTouchDelegate> touchDelegate;
}

@implementation ViewA
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self->touchDelegate touchesBegan:touches withEvent:event];
}
@end // End View A

@interface ViewB : UIView {
    id <ViewTouchDelegate> touchDelegate;
}

@implementation ViewB
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self->touchDelegate touchesBegan:touches withEvent:event];
}
@end // End View B
@protocol ViewTouchDelegate
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件
@结束
@界面视图A:UIView{
id代表;
}
@实现视图a
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件{
[自我->触摸代理触摸开始:触摸事件:事件];
}
@结束//结束视图A
@界面视图B:UIView{
id代表;
}
@实现视图b
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件{
[自我->触摸代理触摸开始:触摸事件:事件];
}
@结束//结束视图B
这将允许您将所有触摸处理代码放在一个位置(在符合ViewTouchDelegate协议的类中),并将该委托给每个视图

在我的示例中,我已使ToucheSpender的签名与UIResponder的ToucheSpender的签名相同,但您可以根据自己的需要进行定制

编辑:示例ViewTouchDelegate

@interface MyViewTouchDelegate : NSObject <ViewTouchDelegate>
@end

@implementation MyViewTouchDelegate
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    //put all your common touch handling code in here
}
@end
@接口MyViewTouchDelegate:NSObject
@结束
@实现MyViewTouchDelegate
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件{
//将所有常用触摸处理代码放在这里
}
@结束

定义ViewTouchDelegate协议中为您的目的所需的任何其他方法,例如touchesMoved、ToucheSEnd、yourOwnCustomEvent。

如果我正确理解了这个问题,您希望为ToucheSStart、touchesMoved执行相同的代码位。。。从你的每一个观点

我这样做的方式是使用委托模式

@protocol ViewTouchDelegate
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
@end

@interface ViewA : UIView {
    id <ViewTouchDelegate> touchDelegate;
}

@implementation ViewA
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self->touchDelegate touchesBegan:touches withEvent:event];
}
@end // End View A

@interface ViewB : UIView {
    id <ViewTouchDelegate> touchDelegate;
}

@implementation ViewB
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self->touchDelegate touchesBegan:touches withEvent:event];
}
@end // End View B
@protocol ViewTouchDelegate
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件
@结束
@界面视图A:UIView{
id代表;
}
@实现视图a
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件{
[自我->触摸代理触摸开始:触摸事件:事件];
}
@结束//结束视图A
@界面视图B:UIView{
id代表;
}
@实现视图b
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件{
[自我->触摸代理触摸开始:触摸事件:事件];
}
@结束//结束视图B
这将允许您将所有触摸处理代码放在一个位置(在符合ViewTouchDelegate协议的类中),并将该委托给每个视图

在我的示例中,我已使ToucheSpender的签名与UIResponder的ToucheSpender的签名相同,但您可以根据自己的需要进行定制

编辑:示例ViewTouchDelegate

@interface MyViewTouchDelegate : NSObject <ViewTouchDelegate>
@end

@implementation MyViewTouchDelegate
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    //put all your common touch handling code in here
}
@end
@接口MyViewTouchDelegate:NSObject
@结束
@实现MyViewTouchDelegate
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件{
//将所有常用触摸处理代码放在这里
}
@结束
定义ViewTouchDelegate协议中为您的目的所需的任何其他方法,例如touchesMoved、touchesEnded、yourOwnCustomEvent。

正如Antwan和jjwchoy所提到的,有几种方法可以执行类似的操作。在大多数OOP语言中,子类化是首选的方式。子类继承其超类的所有处理以及您的自定义行为。要做到这一点,您需要以类似于下面所示的方式实现一个类:

JULView.h(新类的名称)中,可能会有这样的代码

#import <Foundation/Foundation.h>
//... #import for all of the other classes/libraries you need

@interface JULView : UIView
{
  // your iVars go here
}

// Your Custom Properties and Methods go here
- (void)customMethod

@end
要使用这个新的子类,只需导入头文件

#import "JULView.h"
进入您正在使用的类,并像实例化UIView一样实例化JULView

// instead of
UIView *aView = [[UIView alloc] init];

// you would use
JULView *aView = [[JULView alloc] init];
除了子类化之外,Objective-C还提供了一些实现这一点的方法:委托模式和类别。jjwchoy在描述实现委托使用的通用方法方面做得非常出色,因此不需要重复。相反,让我们看一下类别

类别使用其他方法或现有方法的版本扩展现有类。例如,假设您想添加一个方法,将字符串的第一个字母返回给NSString。为此,您将创建一个类别,如下所示:

接口-JULString.h

#import NSString

@interface NSString (JULString)

-(NSString *) firstLetter;

@end
实现—典型的约定是,类别的文件名是要扩展的类的名称,后跟“+”和类的名称