NSObject中无法识别的选择器

时间:2014-06-18 11:57:28

标签: ios nsobject unrecognized-selector

我有一个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];

2 个答案:

答案 0 :(得分:1)

强制引用trofeoPrimaVisita作为调用者的属性,而不是将其作为局部变量。

我假设您正在使用ARC,并且在点击按钮时已经释放了实现Trophy的对象(fb:)。

答案 1 :(得分:-2)

只需删除选择器中方法名称后面的冒号。比如用@selector(fb:)替换@selector(fb)。我希望这有帮助:)