2015-08-25 43 views
0

此代碼顯示異常顯示異常:在單擊一行

'NSInvalidArgumentException', reason: '-[SelectHospitalViewController returncityname:]: unrecognized selector sent to instance

.h file 
-(void)returncityname:(NSString*)CityList; 

//.m file 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if(cell==nil) 
    { 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    if (tableView==self.searchDisplayController.searchResultsTableView) 
    { 



     cell.textLabel.text = [self.SearchCityResult objectAtIndex:indexPath.row]; 
    } 
    else 
    { 
     cell.textLabel.text = self.CityList[indexPath.row]; 
    } 


    cell.textLabel.text = [_CityList objectAtIndex:indexPath.row]; 
    return cell; 

} 
-(void)back 
{ 
    [self.navigationController popViewControllerAnimated:YES]; 
} 


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)IndexPath 
    { 
    if (tableView==self.searchDisplayController.searchResultsTableView) 
    { 
     [self.delegate returncityname:[self.SearchCityResult objectAtIndex:IndexPath.row]]; 

    } 
    else 
    { 

     [self.delegate returncityname:[_CityList objectAtIndex:IndexPath.row]]; 
    } 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

在這段代碼中導致錯誤,我不能找出問題。 任何人都可以幫助我?

+0

主張給rowClick提供代碼和returncityname方法 –

+0

放例外休息的實例變量點,讓我知道,也讓我知道self.SearchCityResult和_cityList是包含字符串只有 – Spynet

+0

其他 { [self.delegate returncityname: [_CityList objectAtIndex:IndexPath.row]]; } 它在這一點上顯示異常.. –

回答

0

我假設self.delegate是您的自定義協議。如果self.delegate沒有實現「returncityname:」。如果您調用[self.delegate returncityname:xx],將導致此異常。

如果「returncityname:」是可選的協議方法,則必須在調用之前檢測它是否實現。像:

if ([self.delegate respondsToSelector:@selector(returncityname:)]) { 
    xxx 
} 
0

如下設置你的委託:

@property (nonatomic) id<YourDelegateClass> delegate; 

所以,你可以調用正確的方法。

0

也許你有一個協議/委託像這樣:

@protocol SelectHospitalDelegate : NSObject 
-(void)returncityname:(NSString*)CityList; 
@end 

而且您的視圖控制器需要該委託

@interface SelectHospitalViewController : UIViewController 
@property(strong) SelectHospitalDelegate delegate; 
@end 
相關問題