2012-06-30 46 views
1

我想要做的事情是:委託錯誤

首先,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;  
} 
+0

segue.destinationViewController的定義沒有-setDelegate。 – Vervious

+0

實際上,我只是看着它。 – Pan

+0

這很奇怪。 SinglePlacePhotosViewController是否定義了它? – Vervious

回答

2

是的確切的錯誤是顯而易見的,因爲你正在調用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方法。 希望我已清除您的查詢,請讓我知道。

+0

設置了@property,因爲我已晚回放。是的,我明白你的意思了。我知道我可以使用segue.destinationController來獲取對singleplacePhotoViewController的引用,但我只是想嘗試委託來獲取此引用。所以,我改變了這樣的prepareforsegue:[segue.destinationViewController setDelegate:segue.destinationViewController];但是,它仍然沒有工作。你能說出原因嗎?非常感謝你。 – Pan

+0

你好潘,首先你使用setDelegate方法,它不屬於你的destinationController這就是爲什麼你會得到錯誤「[SinglePlacePhotosViewController setDelegate:]:無法識別的選擇器」。你可以通過委託方法來做到這一點,你必須聲明這個TopPlacesViewControllerDelegate協議在SinglePlacePhotosViewController中,而不是在TopPlacesViewController.try中聲明它,它會幫助你。實際上,setDelegate意味着你正在調用在SinglePlacePhotosViewController中聲明的一些名爲「delegate」的屬性的setter方法。 – Deepak

+0

啊,最後我看到我的問題。十分感謝。 – Pan