委托错误
我想要做的事情是:委托错误
首先,TopPlacesViewController
有SEGUE到类SinglePlacePhotosViewController
(一TableView
控制器)。
我在TopPlacesViewController
类中创建一个委托,然后使用prepareforSegue
方法将SinglePlacePhotosViewController
设置为委托并实现协议方法。
然后,当我点击TopPlacesViewController
(TableView控制器)中的一张照片时,它会调用方法TopPlacesViewController
,该方法应显示来自该位置的一些照片。
但我一直收到此错误:
[SinglePlacePhotosViewController setDelegate:]: unrecognized selector sent to instance 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;
}
是的确切的错误是显而易见的,因为你正在调用SinglePlacePhotosviewController的setter方法(setDelegate :),但是@property(nonatomic,weak)id delegate;在TopPlacesViewController中。
您在这里以错误的方式使用协议。 如果你想将TopPlacesViewController中的照片数组传递给SinglePlacePhotosviewController, 只需将TopPlacesViewController中的数组分配给prepareSegue方法中的SinglePlacePhotosviewController数组即可。
通常用于将一个类的引用传递给另一个类的协议,这里您已经有了TopPlacesViewController中的SinglePlacePhotosviewController(segue.destinationController)的实例。如果你想在SinglePlacePhotosviewController中使用TopPlacesViewController的引用,那么你必须在SinglePlacePhotosviewController中创建协议,并将TopPlacesViewController的self传递给SinglePlacePhotosviewController的代理协议,以便像在这里一样准备segue方法。 希望我已清除您的查询,请让我知道。
设置了@property,因为我已晚回放。是的,我明白你的意思了。我知道我可以使用segue.destinationController来获取对singleplacePhotoViewController的引用,但我只是想尝试委托来获取此引用。所以,我改变了这样的prepareforsegue:[segue.destinationViewController setDelegate:segue.destinationViewController];但是,它仍然没有工作。你能说出原因吗?非常感谢你。 – Pan
你好潘,首先你使用setDelegate方法,它不属于你的destinationController这就是为什么你会得到错误“[SinglePlacePhotosViewController setDelegate:]:无法识别的选择器”。你可以通过委托方法来做到这一点,你必须声明这个TopPlacesViewControllerDelegate协议在SinglePlacePhotosViewController中,而不是在TopPlacesViewController.try中声明它,它会帮助你。实际上,setDelegate意味着你正在调用在SinglePlacePhotosViewController中声明的一些名为“delegate”的属性的setter方法。 – Deepak
啊,最后我看到我的问题。十分感谢。 – Pan
segue.destinationViewController的定义没有-setDelegate。 – Vervious
实际上,我只是看着它。 – Pan
这很奇怪。 SinglePlacePhotosViewController是否定义了它? – Vervious