为什么我会得到无法识别的选择器?

时间:2014-12-22 16:21:42

标签: ios objective-c

我运行我的应用程序,它从解析服务器加载表视图中的图像。我更新了代码,现在这是我的错误: - [UIImageView setDelegate:]:无法识别的选择器发送到实例0x156b6890

我最终可以解决我的问题但是 我不知道如何开始尝试解决这个问题请帮忙。

这是我的.m:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if (section == self.objects.count) {
    return nil;
}
static NSString *CellIdentifier = @"SectionHeaderCell";
PFTableViewCell *sectionHeaderView = [tableView   dequeueReusableCellWithIdentifier:CellIdentifier];

    //  PFImageView *profileImageView = (PFImageView *)[sectionHeaderView viewWithTag:1];
UILabel *userNameLabel = (UILabel *)[sectionHeaderView viewWithTag:2];
UILabel *titleLabel = (UILabel *)[sectionHeaderView viewWithTag:3];

PFObject *photo = [self.objects objectAtIndex:section];

PFUser *user = [photo objectForKey:@"whoPosted"];
// PFFile *profilePicture = [user objectForKey:@"ProfilePicture"];
NSString *title = photo[@"title"];


userNameLabel.text = user.username;
titleLabel.text = title;



//profileImageView.file = profilePicture;
//[profileImageView loadInBackground];


//follow button
FollowButton *followButton = (FollowButton *)[sectionHeaderView viewWithTag:4];
followButton.delegate = self;
followButton.sectionIndex = section;

if (!self.followingArray || [user.objectId isEqualToString:[PFUser currentUser].objectId])       {
    followButton.hidden = YES;
}
else {
    followButton.hidden = NO;
    NSInteger indexOfMatchedObject = [self.followingArray indexOfObject:user.objectId];
    if (indexOfMatchedObject == NSNotFound) {
        followButton.selected = NO;
    }
    else {
        followButton.selected = YES;
    }
  }

   return sectionHeaderView;
   }

当我立即运行应用程序时,它会加载一个没有任何信息的图像,它只是说加载并且从不加载任何其他内容。我的应用程序应该至少加载5个图像,至少一些文本用于。我不确定我改变了什么创造了这个。感谢。

我的故事板中的跟随按钮有一个FollowButton类,这里是.m:     #import" FollowButton.h"

@implementation FollowButton

- (id)initWithCoder:(NSCoder *)aDecoder {
if (self == [super initWithCoder:aDecoder]) {
    [self addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}


- (void) buttonPressed {
[self.delegate followButton:self didTapWithSelectionIndex:self.sectionIndex];

}


@end

2 个答案:

答案 0 :(得分:1)

正如你在评论中被告知的那样,问题在于以下两点:

FollowButton *followButton = (FollowButton *)[sectionHeaderView viewWithTag:4];
followButton.delegate = self;

问题是你的viewWithTag:4原来是UIImageView - 而UIImageViews没有代表。显然你在这里找错了视图。检查您的标签编号!


进一步评论:

  • 你可以它是一个FollowButton(你的代码确实如此),但这并不意味着它是一个。它就是它 - 真正的含义。换句话说,当你将这个东西投射到FollowButton时,你会不小心撒谎。

  • 错误消息提及setDelegate:的原因是设置某些delegate属性与调用其setDelegate:方法相同。

希望信息可以帮助您在将来追踪同样的错误,因为您将会看到很多错误。我猜这个"无法识别的选择器"是最常见的运行时错误。学会爱它(并修复它)。

答案 1 :(得分:0)

请考虑以下代码行:

PFTableViewCell *sectionHeaderView = [tableView   dequeueReusableCellWithIdentifier:CellIdentifier];
...
FollowButton *followButton = (FollowButton *)[sectionHeaderView viewWithTag:4];
followButton.delegate = self;

(FollowButton *)的演员阵容不安全!至少,检查一下:

UIView *view4 = [sectionHeaderView viewWithTag:4];
if([view4 isKindOfClass:[FollowButton class]]) {
    FollowButton *followButton = (FollowButton *) view4;
    followButton.delegate = self;
} else {
    NSLog(@"I got the wrong view: %@", view4);
}

在我编写的成千上万行iOS代码中,我惊讶地发现做出类型转换很少是正确的。它们是许多错误的根源,如果你没有办法让事情恢复正确,那么它就是一个可能的警示标志。并且总是检查你的类型转换,否则运行时错误难以诊断......

相关问题