[英]using a protocol & delegate to pass data between views
我在這里完成了本教程的工作,幾乎完成了它的工作,它確實提供了很多信息,並幫助我了解了協議和委托如何一起工作以傳遞數據。
但是,當我嘗試告訴子視圖主視圖是其委托時,我有一個警告彈出。 它發回這個警告
“找不到+ setDelegate:”(返回類型默認為“ id”)
到那時為止,一切都在進行之中,而我只是在弄糟該錯誤的含義以及是否與實現我的代碼的方式有關。
以下是發生警告的地方...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
//--- Idendify selected indexPath (section/row)
if (indexPath.section == 0) {
//--- Get the subview ready for use
VehicleResultViewController *vehicleResultViewController = [[VehicleResultViewController alloc] initWithNibName:@"VehicleResultViewController" bundle:nil];
// ...
//--- Sets the back button for the new view that loads
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Back" style: UIBarButtonItemStyleBordered target:nil action:nil] autorelease];
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:vehicleResultViewController animated:YES];
[VehicleResultViewController setDelegate:self]; //<<-- warning here says -->> Method'+setDelegate:' not found (return type defaults to 'id')
switch (indexPath.row)
{
case 0: vehicleResultViewController.title = @"Manufacture";
[vehicleResultViewController setRequestString:@"manufacture.php"]; //sets the request string in searchResultsViewController
break;
//...
任何幫助將不勝感激。
更新后,這就是我將我的代理設置為SecondView.h的方式
//...
@interface VehicleResultViewController : UITableViewController <NSXMLParserDelegate> {
//..
id <PassSearchData> delegate;
}
//..
@property (retain) id delegate;
@end
SecondView.m
//..
@synthesize delegate;
//..
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[self delegate] setVehicleSearchFields:vehicleCellTextLabel];
}
//..
我希望這能更好地闡明我在做什么。
您正在類上調用實例方法。
這行:
[VehicleResultViewController setDelegate:self];
應該:
[vehicleResultViewController setDelegate:self];
第一行在VehicleResultViewController上調用setDelegate,這是類的名稱。 由於setDelegate不是類方法,因此編譯器會抱怨。
更正后的行在vehicleResultViewController上調用setDelegate,這是您分配的類的實例。 這將起作用,因為setDelegate是實例方法。
似乎您已將delegate
的setter聲明為類方法,在警告中用+
表示。
我猜你在某處聲明了這個...
+ (void)setDelegate:(id <SomeProtocol>)aDelegate
何時應- (void)setDelegate:(id <SomeProtocol>)aDelegate
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.