accessing variables/functions from subclass in Objective C
I am having a problem with accessing public variable 'activity', which is a UIActivityIndicatorView
type, see class declaration below in QuickStartViewController.h
:
@interface QuickStartViewController : UIViewController <ABPeoplePickerNavigationControllerDelegate> {
@public
IBOutlet UIActivityIndicatorView *activity;
}
@property (nonatomic, retain) UIActivityIndicatorView *activity;
@end
The function is called from another class:
#import "QuickStartViewController.h"
@interface NumberValidator : QuickStartViewController....
See 开发者_运维知识库below:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[activity startAnimating];
NSLog(@"This function is called, but [activity startAnimating] still doesn't work...");
}
Note: [activity startAnimating]
works fine when called within the QuickStartViewController
class.
Do you have any suggestions as to why [activity startAnimating]
is not working?
The IBOutlet
macro indicates that the UIActivityIndicatorView
will be constructed and assigned when an instance of QuickStartViewController
or NumberValidator
are instantiated via NSBundle's +loadNibNamed:owner:options:
or calling UIViewController's initWithNibName:bundle:
If you are not instantiating your NumberValidator
via it's nib, then the activity property will not be assigned. If you are constructing it via a nib, then you have not assigned the outlet with an appropriate UIActivityIndicatorView
in Interface Builder, by CTRL+Dragging your UIActivityIndicatorView
to your controller.
I would start by setting a breakpoint in -connectionDidFinishLoading: and verifying that activity is not nil.
This probably works - i.e. the activity indicator starts animating. There may be another problem, though - the GUI is not refreshed until you stop processing the connectionDidFinishLoading
method, and therefore it seems that [activity startAnimating]
doesn't work. (You can test this by not calling [activity stopAnimating] - it should show up eventually.)
See e.g. this thread (connectionDidFinishLoading - how to force update UIView?) and my response.
Is it (a) not compiling, (b) crashing when it hits there, or (c) just not doing anything? My suspicion is that it's (c), and it's because you don't have an activity indicator there. Try logging the value of activity to the console, and verify its a valid object.
Thanks for the quick responses.
The connectionDidFinishLoading does successfully execute, and I have placed NSLogs to confirm. However, the startAnimating doesn't.
Note:
If I do [activity startAnimating]; within the following then it works...:
QuickStartViewController.m (not NumberValidator.m):
- (IBAction)showPicker:(id)sender {
[activity startAnimating];
...
}
精彩评论