【发布时间】:2014-06-18 11:57:27
【问题描述】:
我有一个 ViewController 实例化 Trophy 类,它这样做:
#import "Trophy.h"
#import "WATrophiesViewController.h"
@implementation Trophy
-(id)initWithFrame:(CGRect)frame trophyName:(NSString *)trophyName trophyDesc:(NSString *)trophyDesc imageName:(NSString *)imageName viewController:(UIViewController *)controller
{
self = [super init];
if (self)
{
self.view = [[UIView alloc] initWithFrame:frame];
self.trophyName = trophyName;
self.trophyDesc = trophyDesc;
self.trophyImage = imageName;
self.controller = controller;
}
return self;
}
-(UIView *)drawView
{
// Imposto lo sfondo del frame della view
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"container"]];
// Imposto l'immagine del trofeo
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.trophyImage]];
CGRect imageFrame = CGRectMake(0.0f, 0.0f, 138.0f, 191.0f);
image.frame = imageFrame;
// La disegno
[self.view addSubview:image];
// Imposto il titolo del trofeo
UILabel *titolo = [[UILabel alloc] initWithFrame:CGRectMake(140.0f, 20.0f, 260.0f, 90.0f)];
titolo.text = self.trophyName;
titolo.numberOfLines = 0;
titolo.lineBreakMode = NSLineBreakByWordWrapping;
titolo.textColor = [UIColor whiteColor];
// Lo disegno
[self.view addSubview:titolo];
// Imposto la parte per lo share
UIView *shareDiv = [[UIView alloc] initWithFrame:CGRectMake(140.0f, 130.0f, 300.0f, 140.0f)];
UILabel *share = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 300.0f, 30.0f)];
share.text = @"SHARE";
share.textColor = [UIColor whiteColor];
[shareDiv addSubview:share];
UIButton *twitter = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 30.0f, 40.0f, 40.0f)];
[twitter setBackgroundImage:[UIImage imageNamed:@"tw-iphone"] forState:UIControlStateNormal];
[twitter addTarget:self action:@selector(twitter:) forControlEvents:UIControlEventTouchUpInside];
[shareDiv addSubview:twitter];
UIButton *fb = [[UIButton alloc] initWithFrame:CGRectMake(50.0f, 30.0f, 40.0f, 40.0f)];
[fb setBackgroundImage:[UIImage imageNamed:@"fb-iphone"] forState:UIControlStateNormal];
[fb addTarget:self action:@selector(fb:) forControlEvents:UIControlEventTouchUpInside];
[shareDiv addSubview:fb];
[self.view addSubview:shareDiv];
return self.view;
}
-(void)twitter:(id)sender
{
}
-(void)fb:(id)sender
{
}
@end
但是当我点击这两个按钮之一时,会发生错误。为什么?这是错误: 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSArrayM fb:]:无法识别的选择器发送到实例 0x16e9e7d0”
编辑: 这是堆栈跟踪
2014-06-18 14:02:55.953 Wikitude Approx[1027:60b] (
0 Wikitude Approx 0x000f5c57 -[Trophy drawView] + 1446
1 Wikitude Approx 0x000f48c9 -[WATrophiesViewController viewDidLoad] + 1352
2 UIKit 0x330f2a53 <redacted> + 518
3 UIKit 0x3319d30d <redacted> + 32
4 UIKit 0x3319d223 <redacted> + 230
5 UIKit 0x3319c801 <redacted> + 80
6 UIKit 0x3319c529 <redacted> + 572
7 UIKit 0x3319c299 <redacted> + 44
8 UIKit 0x3319c231 <redacted> + 184
9 UIKit 0x330ee305 <redacted> + 380
10 QuartzCore 0x32d6a31b <redacted> + 142
11 QuartzCore 0x32d65b3f <redacted> + 350
12 QuartzCore 0x32d659d1 <redacted> + 16
13 QuartzCore 0x32d653e5 <redacted> + 228
14 QuartzCore 0x32d651f7 <redacted> + 314
15 UIKit 0x330e6a27 <redacted> + 126
16 CoreFoundation 0x3088a039 <redacted> + 20
17 CoreFoundation 0x308879c7 <redacted> + 286
18 CoreFoundation 0x30887d13 <redacted> + 738
19 CoreFoundation 0x307f2769 CFRunLoopRunSpecific + 524
20 CoreFoundation 0x307f254b CFRunLoopRunInMode + 106
21 GraphicsServices 0x3575f6d3 GSEventRunModal + 138
22 UIKit 0x33151891 UIApplicationMain + 1136
23 Wikitude Approx 0x000f23e1 main + 116
24 libdyld.dylib 0x3b553ab7 <redacted> + 2
)
这就是我使用类的方式
self.scroll.contentSize = CGSizeMake(414.0f, 3000.0f);
// Creo il trofeo della prima visita
Trophy *trofeoPrimaVisita = [[Trophy alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 414.0f, 228.0f) trophyName:@"Hai lanciato per la prima volta l'applicazione" trophyDesc:@"Ho lanciato per la prima volta l'applicazione Hermes" imageName:@"trofeoaudio-guida-5" viewController:self];
UIView *trofeoView = [trofeoPrimaVisita drawView];
[self.scroll addSubview:trofeoView];
【问题讨论】:
-
我认为错误不在您发布的代码中。请发布堆栈跟踪。
-
看起来你实例化的 Trophy 对象已经被释放了,你需要保持对它的强引用。
-
-
WATrophiesViewController对您从drawView返回的视图做了什么?还有,Trophy的父类是什么? -
嗨菲利普我更新了第一篇文章
标签: ios nsobject unrecognized-selector