即使我不应该使用UIButton获得异常

时间:2013-07-20 20:15:01

标签: ios objective-c uibutton action

我以编程方式创建一个UIButton:

UIButton *yesButton= [[UIButton alloc] init];
[yesButton addTarget:self
           action:@selector(buttonClicked:)
 forControlEvents:UIControlEventTouchUpInside];
[yesButton setTitle:@"ok" forState:UIControlStateNormal];
yesButton.frame = CGRectMake(width*0.09, height*0.82, width*0.1, height*0.05);

所以我有一个名为buttonClicked

的方法
-(void) buttonClicked:(id)sender {
     NSLog(@"%@",((UIButton *)sender).titleLabel);
}

所以我按下按钮,这是例外:

2013-07-20 23:13:10.989 Game[9593:c07] -[HudView buttonClicked:]: unrecognized selector sent to instance 0x10592640
2013-07-20 23:13:16.565 Game[9593:c07] Uncaught exception: -[HudView buttonClicked:]: unrecognized selector sent to instance 0x10592640
2013-07-20 23:13:16.567 Game[9593:c07] Stack trace: (
    0   CoreFoundation                      0x0259f02e __exceptionPreprocess + 206
    1   libobjc.A.dylib                     0x01e50e7e objc_exception_throw + 44
    2   CoreFoundation                      0x0262a4bd -[NSObject(NSObject) doesNotRecognizeSelector:] + 253
    3   CoreFoundation                      0x0258ebbc ___forwarding___ + 588
    4   CoreFoundation                      0x0258e94e _CF_forwarding_prep_0 + 14
    5   libobjc.A.dylib                     0x01e64705 -[NSObject performSelector:withObject:withObject:] + 77
    6   UIKit                               0x002922c0 -[UIApplication sendAction:to:from:forEvent:] + 96
    7   UIKit                               0x00292258 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
    8   UIKit                               0x00353021 -[UIControl sendAction:to:forEvent:] + 66
    9   UIKit                               0x0035357f -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 578
    10  UIKit                               0x003526e8 -[UIControl touchesEnded:withEvent:] + 546
    11  UIKit                               0x002c1cef -[UIWindow _sendTouchesForEvent:] + 846
    12  UIKit                               0x002c1f02 -[UIWindow sendEvent:] + 273
    13  UIKit                               0x0029fd4a -[UIApplication sendEvent:] + 436
    14  UIKit                               0x00291698 _UIApplicationHandleEvent + 9874
    15  GraphicsServices                    0x02f5adf9 _PurpleEventCallback + 339
    16  GraphicsServices                    0x02f5aad0 PurpleEventCallback + 46
    17  CoreFoundation                      0x02514bf5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
    18  CoreFoundation                      0x02514962 __CFRunLoopDoSource1 + 146
    19  CoreFoundation                      0x02545bb6 __CFRunLoopRun + 2118
    20  CoreFoundation                      0x02544f44 CFRunLoopRunSpecific + 276
    21  CoreFoundation                      0x02544e1b CFRunLoopRunInMode + 123
    22  GraphicsServices                    0x02f597e3 GSEventRunModal + 88
    23  GraphicsServices                    0x02f59668 GSEventRun + 104
    24  UIKit                               0x0028effc UIApplicationMain + 1211
    25  Game                                0x000028c4 main + 164
    26  Game                                0x000027d5 start + 53
)

任何帮助?

编辑: 这是我继承自HudView类的类:

@implementation askDownloadHudView

-(id) initWithView:(UIView *)view {
    int height = (view.frame.size.height/4.8)*1.5;
    int width = (view.frame.size.width/1.142);
    self = [super initWithView:view animated:YES Height:height Width:width];
    self.text = @"Asking download";

    UILabel *textLabel = [[UILabel alloc] init];

    textLabel.text= @"Do you want to download the level? download size is about 7mb";
    textLabel.numberOfLines = 0 ;
    [textLabel setFrame:CGRectMake(0,0,self.width.intValue,height*0.8)];
    textLabel.backgroundColor=[UIColor clearColor];
    textLabel.textColor=[UIColor whiteColor];
    textLabel.textAlignment=NSTextAlignmentCenter;

    UIButton *yesButton= [[UIButton alloc] init];
    [yesButton addTarget:self
               action:@selector(buttonClicked:)
     forControlEvents:UIControlEventTouchUpInside];
    [yesButton setTitle:@"ok" forState:UIControlStateNormal];
    yesButton.frame = CGRectMake(width*0.09, height*0.82, width*0.1, height*0.05);

    UIButton *noButton= [[UIButton alloc] init];
    [noButton addTarget:self action:@selector(myButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [noButton setTitle:@"no" forState:UIControlStateNormal];
    noButton.frame = CGRectMake(width*0.8, height*0.82, width*0.15, height*0.05);

    [self addSubview:yesButton];
    [self addSubview:noButton];
    [self addSubview:textLabel];






    return self;
}

-(void) buttonClicked:(id)sender {
    //NSLog(@"%@",((UIButton *)sender).titleLabel);
}


@end

编辑2:HudView的实现:

-(HudView *) initWithView:(UIView *)view animated:(BOOL)animated Height:(int)height Width:(int)width {
    self=[super init];

    self= [[HudView alloc] initWithFrame:CGRectMake(
                                                    roundf(view.bounds.size.width - width) / 2.0f,
                                                    roundf(view.bounds.size.height - height) / 2.0f,
                                                    width,
                                                    height)];

self.height=[NSNumber numberWithInt:height];
    self.width=[NSNumber numberWithInt:width];
    self.opaque = NO;
    [view addSubview:self];
    //view.userInteractionEnabled = NO;
    return self;


}

2 个答案:

答案 0 :(得分:2)

问题出在-(HudView *) initWithView:(UIView *)view animated:(BOOL)animated Height:(int)height Width:(int)width。您不应该分配HudView的实例。如果这样做,将返回HudView的实例而不是子类。您必须将其更改为以下内容。

-(HudView *) initWithView:(UIView *)view animated:(BOOL)animated Height:(int)height Width:(int)width {
    self=[super initWithFrame:CGRectMake(roundf(view.bounds.size.width - width) / 2.0f,
                                         roundf(view.bounds.size.height - height) / 2.0f,
                                         width,
                                         height)];

self.height=[NSNumber numberWithInt:height];
    self.width=[NSNumber numberWithInt:width];
    self.opaque = NO;
    [view addSubview:self];
    //view.userInteractionEnabled = NO;
    return self;


}

答案 1 :(得分:1)

我需要更改您的代码才能使用

UIButton *yesButton=  [UIButton buttonWithType:UIButtonTypeRoundedRect];

并添加

[self.view addSubview:yesButton];

然后它奏效了。

你可能正在点击另一个按钮?