views:

476

answers:

3

I am trying to call up a modal table view controller using presentModalViewController but I am not sure what to do about the delegate. The following code gives me an error:

MyRidesListView *controller = [[MyRidesListView alloc] init];
    controller.delegate = self;
    [self presentModalViewController:controller animated:YES];
    [controller release];

Error:

Request for member 'delegate' is something not a structure or union

Now, I realized there is no delegate property in my MyRidesListView class. So, how would I add a reference to my delegate there? What am I missing here?

A: 

Why do you think you need a delegate? Why not just remove the "controller.delegate = self" line. Otherwise you need to implement a delegate system the way I outline below or else make MyRidesListView a subclass of a viewcontroller that implements delegates.

It looks like you cut and pasted some sample code that uses a delegate, then substituted your own viewcontroller that doesn't provide a delegate. If you don't know what the delegate is for, then just delete that line.

I'll cut and paste some actual code from one of my test programs to show you how it's done:

from the Interface file:

Add a delegate instance variable to your class and make it a property so you can use the "blah.delegate = foo" syntax to set it.

@interface BAPClient : NSObject { CGSize imageSize; id delegate; }

@property (nonatomic, readonly) CGSize imageSize; @property (nonatomic, assign) id delegate; @end

// define the protocol spoken. (what the delegate must implement)

@protocol BAPClientDelegate - (void)addTile:(BAPTile *)tile; @end

in the implementation, you must call the delegate at the appropriate time:

  • (void)deliverTile:(BAPTile *) tile { NSLog(@"%s tile=%p",FUNCTION,tile); if ([self delegate]) [[self delegate] addTile:tile];

    [tile release]; }

Vagrant
+4  A: 

Generally delegates are properties defined as such:

id<NameOfDelegateProtocol> delegate;

And:

@property (nonatomic, assign) id<NameOfDelegateProtocol> delegate;

EDIT: You said your parent class is UITableViewController. You may have wanted to do this:

controller.tableView.delegate = self;
MrHen
What do you mean by Protocol?
Nic Hubbard
Delegates generally involve something like UITableViewDelegate. These are protocols. If all you wanted to do was set the table delegate you probably meant to set controller.tableView.delegate instead of controller.delegate. If not, I can add a more thorough explanation to the answer above.
MrHen
A: 

Try to set the delegate object by the setter

[controller setDelegate:self];

This often works wonders.

unset
That just gives me errors.
Nic Hubbard
Are you sure the controller got an delegate property?
unset
No it does not that is what I am try to learn how to add.
Nic Hubbard
@unset: That's exactly the same as using `controller.delegate = self`.
eman
@eman if I understood it right, that's only the case when the delegate property is public and the setter does not perform any additional actions. please correct me, if I'm wrong.
unset
@unset: `controller.delegate = self` calls `setDelegate`--the dot-syntax is just syntactic sugar.
eman
@eman thank you for this. I've just read it in the apple provided docs =)
unset