【问题标题】:Unrecognized selector in NSObjectNSObject 中无法识别的选择器
【发布时间】: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


【解决方案1】:

保持对 trofeoPrimaVisita 的强引用作为调用者的属性,而不是将 is 作为局部变量。

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

【讨论】:

  • 我想过,但后来我必须在 WATrophiesViewController 中生成多个 Trophy 对象,所以我不能这样做。
  • 我不确定您所说的“必须生成”是什么意思,但如果您想避免崩溃,则需要强烈引用该对象。如果您需要其中的许多,请使用数组或集合或字典......任何有意义的东西。
  • 我使用for来实例化并生成X个奖杯视图,所以如果我声明一个强属性,这将被最后一个对象填充。
  • 然后将 strong 属性设为一个数组并将Trophy 对象添加到其中。另一种选择是完全摆脱Trophy,并将按钮处理程序和视图创建逻辑放入最终显示视图的控制器中。
  • 谢谢,我使用 NSMutableArray 解决了一个强引用问题,我在其中添加了所有 Trophy 对象。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2011-11-13
    • 2018-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多