【问题标题】:The delegate error委托错误
【发布时间】:2012-06-30 04:42:15
【问题描述】:

我想做的是:

首先,TopPlacesViewControllerSinglePlacePhotosViewController 类(TableView 控制器)有一个转义。

我在TopPlacesViewController类中创建了一个delegate,然后使用prepareforSegue方法将SinglePlacePhotosViewController设置为delegate并实现protocol方法。

然后当我点击TopPlacesViewController(TableView 控制器)中的一张照片时,它会调用TopPlacesViewController 方法,该方法应该会显示该位置的一些照片。

但我一直收到此错误:

[SinglePlacePhotosViewController setDelegate:]:无法识别的选择器发送到实例 0xc94cc20

我的TopPlacesViewController.h 文件:

@class TopPlacesViewController;  

@protocol TopPlacesViewControllerDelegate    
- (void)topPlacesViewControllerDelegate:(TopPlacesViewController *)sender
                            showPhotos:(NSArray *)photo;   
@end

@interface TopPlacesViewController : UITableViewController

@property (nonatomic,weak) id <TopPlacesViewControllerDelegate> delegate;   
@end   

TopPlacesViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{                                                                                  
    NSDictionary *place = [self.places objectAtIndex:indexPath.row];   
    self.singlePlacePhotos = [FlickrFetcher photosInPlace:place maxResults:50];   
    [self.delegate topPlacesViewControllerDelegate:self showPhotos:self.singlePlacePhotos];  
    [self performSegueWithIdentifier:@"Flickr Photos" sender:self];   
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{   
    if([segue.identifier isEqualToString:@"Flickr Photos"]) {                                  
          [segue.destinationViewController setDelegate:self];     
    }         
}  

然后在此我实现委托:

@interface SinglePlacePhotosViewController () <"TopPlacesViewControllerDelegate">    
- (void)topPlacesViewControllerDelegate:(TopPlacesViewController *)sender showPhoto:(NSArray *)photo    
{    
    self.photos = photo;    
}

【问题讨论】:

  • segue.destinationViewController 的定义没有-setDelegate。
  • 其实我只是查了一下。
  • 这很奇怪。 SinglePlacePhotosViewController 定义了吗?
  • 是的,您能显示更多SinglePlacePhotosViewController 的@interface 代码吗?看起来您的委托缺少 @property。
  • 我已经为代理设置了@property

标签: objective-c delegates cs193p


【解决方案1】:

是的,错误很明显,因为您调用的是 SinglePlacePhotosviewController 的 setter 方法 (setDelegate:),但调用的是 @property (nonatomic,weak) id 委托;在 TopPlacesViewController 中。

您在这里以错误的方式使用协议。 如果您想将照片数组从 TopPlacesViewController 传递给 SinglePlacePhotosviewController, 只需将 TopPlacesViewController 中的数组分配给 prepareSegue 方法中的 SinglePlacePhotosviewController 数组即可。

协议通常用于将一个类的引用传递给另一个类,这里​​你已经在 TopPlacesViewController 中有 SinglePlacePhotosviewController (segue.destinationController) 的实例。如果你想在 SinglePlacePhotosviewController 中引用 TopPlacesViewController,那么你必须在 SinglePlacePhotosviewController 中创建协议,并将 TopPlacesViewController 的 self 传递给 SinglePlacePhotosviewController 的委托协议,就像你在这里所做的那样准备 segue 方法。 希望我已经清除了您的查询,请告诉我。

【讨论】:

  • 抱歉我的重播迟了。是的,我明白你说的。我知道我可以使用 segue.destinationController 来获取对 singleplacePhotoViewController 的引用,但我只想尝试委托来获取此引用。所以,我像这样改变prepareforsegue:[segue.destinationViewController setDelegate:segue.destinationViewController];但是,它仍然没有工作。你能说说为什么吗?非常感谢。
  • 你好潘,首先你正在使用不属于你的destinationController的setDelegate方法,这就是你得到错误的原因“[SinglePlacePhotosViewController setDelegate:]:unrecognized selector”。是的,你可以通过委托方法做到这一点为此,您必须在 SinglePlacePhotosViewController 中声明此 TopPlacesViewControllerDelegate 协议,而不是在 TopPlacesViewController 中声明它。试试这将帮助您。实际上 setDelegate 意味着您正在调用某个名为“delegate”的属性的 setter 方法,该属性在 SinglePlacePhotosViewController 中声明。
  • 啊,终于看到我的问题了。非常感谢你。
  • 我刚刚发现我犯了一个愚蠢的错误。大声笑,是的,它有帮助。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多