Objective c 以编程方式创建UIButton-won';你不需要采取行动吗?

Objective c 以编程方式创建UIButton-won';你不需要采取行动吗?,objective-c,uibutton,selector,target-action,Objective C,Uibutton,Selector,Target Action,我有一个自定义类,该类有一个UIButton实例变量。我在类指定的初始值设定项中添加了以下代码: theFishDeathView = [UIButton buttonWithType:UIButtonTypeCustom]; [theFishDeathView setFrame:CGRectMake(15, 15, 50, 50)]; [theFishDeathView setImage:[UIImage imageNamed:@"Small fish - death.png"] forSta

我有一个自定义类,该类有一个
UIButton
实例变量。我在类指定的初始值设定项中添加了以下代码:

theFishDeathView = [UIButton buttonWithType:UIButtonTypeCustom];
[theFishDeathView setFrame:CGRectMake(15, 15, 50, 50)];
[theFishDeathView setImage:[UIImage imageNamed:@"Small fish - death.png"] forState:UIControlStateNormal];
因此,这应该正确地分配/初始化按钮。确实,当调用它时,按钮get会显示在屏幕上(当然也会添加为子视图)

现在,我在我的对象上调用此方法:

[theFishDeathView addTarget:self action:@selector(sellFish) forControlEvents:UIControlEventTouchDown];
下面是
sellFish
方法:

-(void) sellFish {
    thePlayer.dollars += worthInDollars * 3;
    [theFishDeathView removeFromSuperview];
}
但当我尝试按下按钮时,它不会调用该方法。我是不是遗漏了什么

为了完整起见,这里是Fish.h文件。很明显,
fishdeath视图
是Fish对象的实例成员

#import <Foundation/Foundation.h>

@interface Fish : NSObject
{
    float cookingTime;
    float weight;
    int worthInDollars;
    NSString *name;
    NSArray *animaionImages;

    int fishMovementSpeed;
}

// Will be used to display
@property (nonatomic, retain) UIImageView *theFishImageView;
@property (nonatomic, retain) UIButton *theFishDeathView;

// Create setter / getter methods
@property (nonatomic, retain) NSString *name;
@property (readonly) int worthInDollars;
@property (readonly) int fishMovementSpeed;

-(id) initWith: (NSString *)theName andWeight: (float)theWeight andCookingTime: (float)theCookingTime andValue: (int)theValue andMovementSpeed: (int)speed;

-(CGRect) newFrameWithWidth:(int)width andHeight:(int)height;

-(void) killFish;

// Cooking methods
-(void) startCooking;
-(void) isDoneCooking;
-(void) isOverCooked;
-(void) sellFish;

@end
#导入
@接口Fish:NSObject
{
浮动烹饪时间;
浮重;
印度支那;
NSString*名称;
NSArray*动物图像;
int-fishMovementSpeed;
}
//将用于显示
@属性(非原子,保留)UIImageView*theFishImageView;
@属性(非原子,保留)UIButton*FishDeathView;
//创建setter/getter方法
@属性(非原子,保留)NSString*名称;
@属性(只读)int-worthInDollars;
@属性(只读)int fishMovementSpeed;
-(id)initWith:(NSString*)name和weight:(float)weight和cooking time:(float)cooking time和value:(int)value和movementspeed:(int)speed;
-(CGRect)新框架width:(int)width和height:(int)height;
-(b)杀人鱼;
//烹饪方法
-(无效)开始预订;
-(无效)烹饪;
-(无效)未煮熟;
-(无效)sellFish;
@结束
您编写了
UIControlEventTouchDown
而不是
UIControlEventTouchDown

您编写了
UIControlEventTouchDown
而不是
UIControlEventTouchDown

尝试

 -(void) sellFish:(id)sender
和(在selfish后面加上

试一试

和(在selfish后面加上


我想让人们知道我发现了这个错误(在苹果开发者论坛的帮助下)——这是一个内存泄漏。我试图向僵尸对象(即解除分配的对象)发送消息。我以为它是作为子视图添加而保留的,但我完全忘记了它是作为子视图添加的按钮,而不是包含按钮的类。所以类本身被释放了,按钮被保留了,这就是为什么我仍然可以按下它的原因

对于处理类似问题的其他人,请在plist.info文件中启用启用Zombie对象。这样,您将得到如下错误消息:“尝试向解除分配的对象发送操作”


感谢您尝试帮助我:)

我想让人们知道我发现了错误(在苹果开发者论坛的帮助下)-这是内存泄漏。我试图向僵尸对象(即解除分配的对象)发送消息。我以为它是作为子视图添加而保留的,但我完全忘记了它是作为子视图添加的按钮,而不是包含按钮的类。所以类本身被释放了,按钮被保留了,这就是为什么我仍然可以按下它的原因

对于处理类似问题的其他人,请在plist.info文件中启用启用Zombie对象。这样,您将得到如下错误消息:“尝试向解除分配的对象发送操作”


谢谢你试着帮我:)

嗯,我不确定我是否明白,伙计?我使用了UIControlEventTouchDown而不是UIControlEventTouchDown?:=)UIControlEventTouchUpInside是按钮使用的标准事件-答案中的代码示例是可以的,但解释是错误的。哈,不确定我是否理解?我使用了UIControlEventTouchDown而不是UIControlEventTouchDown?:=)UIControlEventTouchUpInside是按钮使用的标准事件-答案中的代码示例是可以的,但解释是错误的。请包括您尝试调用的sellFish方法。它是sellFish还是sellFish:?我添加了sellFish方法,它不带参数,因此应该与sellFish一样好。请注意-FishDeathView是Fish*对象指向的UIButton,所以我必须更改addTarget:self吗?我尝试将其更改为nil,但没有解决问题,因此我想知道是否还有其他东西可以尝试?[theFishDeathView addTarget:self action:@selector(sellFish)for ControlEvents:uicontrol事件触地];通过调试检查该行是否执行?它正在执行。我想知道这是否与将self指定为目标有关?因为我的意思是,FishDeathView(UIBUtton*)是Fish类的实例成员。方法是在Fish类中定义的,但我只是假设我应该说self,它会在UIButton*所在的类中查找方法?请包括您试图调用的sellFish方法。它是sellFish还是sellFish:?我添加了sellFish方法,它不带参数,因此应该与sellFish一样好。请注意-FishDeathView是Fish*对象指向的UIButton,所以我必须更改addTarget:self吗?我尝试将其更改为nil,但没有解决问题,因此我想知道是否还有其他东西可以尝试?[theFishDeathView addTarget:self action:@selector(sellFish)for ControlEvents:uicontrol事件触地];通过调试检查该行是否执行?它正在执行。我想知道这是否与将self指定为目标有关?因为我的意思是,FishDeathView(UIBUtton*)是Fish类的实例成员。方法是在Fish类中定义的,但我只是假设我应该说self,它会在UIButton*所在的类中查找方法?谢谢,但这也解决不了问题:(我也尝试过说fishDeathView.userInteractionEnabled=是,但仍然没有:(谢谢,但也解决不了问题:(我也试着说fishDeathView.userInteractionEnabled=是,但仍然没有:(
 -(void) sellFish:(id)sender
[theFishDeathView addTarget:self
                 action:@selector(sellFish:)
       forControlEvents:UIControlEventTouchUpInside];