iPhone - accessing unknown getter method in a class
I have this class that has a delegate method. Its delegate has a navigationController.
I am trying to send a message to this navigati开发者_如何学GoonController from inside that class. Something like:
[delegate.navigationController setNavigationBarHidden:NO];
I am receiving the message: error: accessing unknown 'navigationController' getter method
when I am inside the delegate, I can access its navigationController using
self.navigationController
How do I set the getter on that class, so this will work?
thanks.
navigationController
is a property of a typical UIViewController. If you cast the delegate
to UIViewController*, the error will go away:
[((UIViewController *)delegate).navigationController setNavigationBarHidden:NO];
Note that you'd better make sure that delegate
is indeed a UIViewController instance. Otherwise, your app will crash.
EDIT: Off the top of my head, there are three ways to cast C pointers, using the above example:
((UIViewController *) delegate).navigationController;
Why nested braces? Because (UIViewController *) delegate.navigationController
is equivalent to (UIViewController *) (delegate.navigationController)
due to operator precedence.
[(UIViewController *) delegate navigationController];
Here we just cast delegate to UIViewController and send it a message.
UIViewController *myViewController = (UIViewController *) delegate;
[myViewController navigationController]; // or myViewController.navigationController;
All of them are functionally equivalent. I'd go out on a limb and say it's a matter of style which one to choose.
What is the type of the delegate in the class that is calling this method. If it is id
then that's why it doesn't know about the navigationController method. You can cast the delegate to the class it really is if it's the same every time, and import that classes header file so it knows that there is a member navigationController. It's also possible you didn't set the navigationController as a property in the delegate file.
精彩评论