gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:16:53 25 4
gpt4 key购买 nike

我以编程方式创建一个 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);
}

所以我按下按钮,这里是异常(exception):

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;


}

最佳答案

问题在 -(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;


}

关于ios - 获得异常,即使我不应该使用 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17766303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com