我想在另一個控制器中創建一個UITableViewController,並且從該控制器傳遞一個方法。我已經讀過這可以通過使用@selector來實現。現在,我嘗試了以下內容:在另一個類中執行一個選擇器
TimeController.m
- (void)choseTime{
SelectOptionController *selectController = [[SelectOptionController alloc] initWithArray:[Time SQPFetchAll] andSelector:@selector(timeSelected)];
[self.navigationController pushViewController:selectController animated:true];
}
- (void) timeSelected{
NSLog(@"Time selected!");
}
SelectOptionController.h
@interface SelectOptionController : UITableViewController
@property (nonatomic, strong) NSMutableArray *dataset;
@property (nonatomic) SEL selectedMethod;
-(id)initWithArray: (NSMutableArray *) myArray andSelector: (SEL) selectedMethod;
SelectOptionController.m
- (id)initWithArray: (NSMutableArray *) myArray andSelector: (SEL) selectedMethod{
self = [super initWithStyle:UITableViewStyleGrouped];
if(self) {
self.dataset = myArray;
self.selectedMethod = selectedMethod;
}
return self;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self performSelector:self.selectedMethod];
[self.navigationController popViewControllerAnimated:true];
}
然而,當細胞被選中,以下異常被拋出:
-[SelectOptionController timeSelected]: unrecognized selector sent to instance 0x1450f140
我在這裏做錯了什麼?任何幫助將不勝感激。
尋求調試幫助的問題(「爲什麼這個代碼不工作?」)必須包含所需的行爲,特定的問題或錯誤以及在問題本身中重現問題所需的最短代碼。 –