2013-07-20 36 views
0

進出口創造一個UIButton編程:獲取例外不應該用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; 


} 
+0

嘗試取出NSLog。 –

+0

@AbdullahShafique沒有幫助 –

+0

創建按鈕並將其賦予'self'作爲目標的對象與實現'buttonClicked:'的對象相同。 –

回答

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; 


} 
+0

謝謝你的工作!我不知道我應該分配,但聽起來有點奇怪,但我想我會習慣它。再次感謝! –

1

我需要改變你的代碼,使用

UIButton *yesButton= [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

並添加

[self.view addSubview:yesButton]; 

然後它工作。

您可能正在點擊另一個按鈕?

+1

我不認爲buttonWithType與此有任何關係。我查了一下,確實沒有改變任何東西。是的,我正在輸入正確的按鈕。 –

+0

我的回答回答了你原來的問題,在「編輯」之前你添加了[self addSubview:yesButton] ;.因此,在原始問題中,問題按鈕從未添加到視圖中。在UIButton類參考中,buttonWithType是「創建按鈕」下的唯一列表,buttonType屬性是隻讀的。在我爲我的答案創建的應用程序中,如果使用[[UIButton alloc] init]創建按鈕,即使使用[view addSubview:yesButton],它也不會出現。我質疑,如果您是「敲擊」(不是輸入)正確的按鈕,因爲在您提供的代碼中,按鈕不可見。 –