1

我想做的是:

首先,它与类(控制器)TopPlacesViewController有一个segue 。SinglePlacePhotosViewControllerTableView

我在类中创建了一个委托TopPlacesViewController,然后使用该prepareforSegue方法将其设置SinglePlacePhotosViewController为委托并实现协议方法。

然后,当我单击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;    
}
4

1 回答 1

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 方法。希望我已经清除了您的查询,请告诉我。

于 2012-06-30T06:03:09.063 回答