【问题标题】:Programmatically create UIButton - won't call action?以编程方式创建 UIButton - 不会调用操作?
【发布时间】:2012-01-22 09:55:21
【问题描述】:

我有一个自定义类,该类有一个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 文件。很明显theFishDeathView是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

【问题讨论】:

  • 请包含您尝试调用的 sellFish 方法。是卖鱼还是卖鱼:?
  • 我添加了 sellFish 方法,它不需要任何参数,所以应该可以像 sellFish 一样
  • 请注意 - theFishDeathView,是一个由 Fish * 对象指向的 UIButton,所以我必须更改 addTarget: self 吗?我尝试将其更改为 nil 但这并没有解决它,所以我想知道那里是否还有其他尝试?
  • [theFishDeathView addTarget:self action:@selector(sellFish) forControlEvents:UIControlEventTouchDown];通过调试检查此行是否执行?
  • 它正在被执行。我想知道它是否与将 self 指定为目标有关?因为我的意思是,theFishDeathView(UIBUtton *)是 Fish 类的实例成员。并且该方法是在该 Fish 类中定义的,但我只是假设我应该说 self 并且它会在 UIButton * 所在的类中查找方法?

标签: objective-c uibutton selector target-action


【解决方案1】:

试试

 -(void) sellFish:(id)sender

和(在 sellFish 后面加上:

[theFishDeathView addTarget:self
                 action:@selector(sellFish:)
       forControlEvents:UIControlEventTouchUpInside];

【讨论】:

  • 谢谢,但这也不能解决问题:(我也试过说 theFishDeathView.userInteractionEnabled = YES 但仍然没有:(
【解决方案2】:
  [theFishDeathView addTarget:self
                 action:@selector(sellFish)
       forControlEvents:UIControlEventTouchUpInside];

你写的是UIControlEventTouchDown而不是UIControlEventTouchDown

【讨论】:

  • 嗯,不知道我理解那里的伙伴?我使用 UIControlEventTouchDown 不是 UIControlEventTouchDown? :=)
  • UIControlEventTouchUpInside 是用于按钮的标准事件 - 答案中的代码示例是可以的,但解释是错误的。
【解决方案3】:

我想让人们知道我发现了错误(在 Apple 开发者论坛的帮助下) - 这是内存泄漏。我试图向僵尸对象(即已释放的对象)发送消息。我认为它是通过将其添加为子视图来保留的,但我完全忘记了它是我作为子视图添加的 BUTTON,而不是包含该按钮的类。所以类本身被释放,按钮被保留,他是我仍然可以按下它的原因。

对于其他处理类似问题的人,请打开 plist.info 文件中的 Zombie Objects Enabled 事物。这样,您将收到如下错误消息:“尝试向已释放对象发送操作”。

感谢您尝试帮助我:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-09
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多