使用协议&委托在视图之间传递数据

时间:2011-09-27 01:52:26

标签: iphone ios uitableview delegates protocols

我几乎已经完成了本教程here的工作 它确实提供了丰富的信息,并帮助我了解协议和代表如何协同工作以传递数据。

但是,当我尝试告诉我的子视图主视图是其委托时,我有一个警告弹出。它发回这个警告

  

“+ setDelegate:'not found(返回类型默认为'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];
}
//..

我希望这能更清楚地说明我在做什么。

2 个答案:

答案 0 :(得分:3)

您正在类上调用实例方法。

这一行:

[VehicleResultViewController setDelegate:self];

应该是:

[vehicleResultViewController setDelegate:self];

第一行调用VehicleResultViewController上的setDelegate,这是该类的名称。由于setDelegate不是编译器抱怨的类方法。

更正的行调用vehicleResultViewController上的setDelegate,它是您分配的类的实例。这将起作用,因为setDelegate是一个实例方法。

答案 1 :(得分:1)

您似乎已将delegate的setter声明为类方法,由警告中的+表示。

我猜你在某个地方声明了这个...

+ (void)setDelegate:(id <SomeProtocol>)aDelegate应该是- (void)setDelegate:(id <SomeProtocol>)aDelegate