开发者

using a protocol & delegate to pass data between views

I have almost finished working my way through this tutorial here Its really informative and has helped me understand how protocols and delegates work together for passing data around.

However I have one warning that poping up when I try to tell my subview that the mainview is its delegate. its sending back this warning

"+setDelegate:' not found (return type defaults to 'id')"

Everything was on track up till that point and I am just woundering what this error means and if it is anything to do with the way I have implemented my code.

below is where the warning is happening...

- (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;
//...

Any help would be greatly appreciated.

Updated, this is how I set my delegate up 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];
}
//..
开发者_StackOverflow中文版

I hope this better clarifies what I am doing.


You are calling an instance method on a class.

This line:

[VehicleResultViewController setDelegate:self];

should be:

[vehicleResultViewController setDelegate:self];

The first line calls setDelegate on VehicleResultViewController, which is the name of the class. Since setDelegate is not a class method the compiler complains.

The corrected line calls setDelegate on vehicleResultViewController, which is an instance of the class that you allocated. This will work because setDelegate is an instance method.


It seems that you have declared a setter for delegate as a class method, indicated by the + in the warning.

I'm guessing you have this declared somewhere...

+ (void)setDelegate:(id <SomeProtocol>)aDelegate when it should be - (void)setDelegate:(id <SomeProtocol>)aDelegate

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜