質問

The thing I want to do is:

First, the TopPlacesViewController has a segue to the class SinglePlacePhotosViewController (a TableView controller).

I create a delegate in the TopPlacesViewController class, then use the prepareforSegue method to set the SinglePlacePhotosViewController as the delegate and implement the protocol method.

Then when I click a photo in the TopPlacesViewController (TableView controller), it calls the method TopPlacesViewController which should show some photos from that place.

But I kept getting this error:

[SinglePlacePhotosViewController setDelegate:]: unrecognized selector sent to instance 0xc94cc20

My TopPlacesViewController.h file:

@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];     
    }         
}  

Then in this I implement the delegate:

@interface SinglePlacePhotosViewController () <"TopPlacesViewControllerDelegate">    
- (void)topPlacesViewControllerDelegate:(TopPlacesViewController *)sender showPhoto:(NSArray *)photo    
{    
    self.photos = photo;    
}
役に立ちましたか?

解決

Yes exactly the error is obvious because you are calling the setter method (setDelegate:) of SinglePlacePhotosviewController but the @property (nonatomic,weak) id delegate; is in TopPlacesViewController.

you are using protocol here in wrong way. if u want pass the array of photos from TopPlacesViewController to SinglePlacePhotosviewController, simply assign the array from TopPlacesViewController to the array of SinglePlacePhotosviewController in prepareSegue method.

Protocols generally used for passing the reference of one class to another class, here u already have the instance of SinglePlacePhotosviewController (segue.destinationController) in TopPlacesViewController. If u want the reference of TopPlacesViewController in SinglePlacePhotosviewController then u have to make protocol in SinglePlacePhotosviewController and pass the self of TopPlacesViewController to the delegate protocol of SinglePlacePhotosviewController in prepare segue method as u r doing here. hope i have cleared your query, please let me know.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top