iphone setRightBarButtonItem from AppDelegate
I am trying to set the right button of the navbar from within the appDelegate, the code compiles and executes without error but the button does not appe开发者_开发百科ar.
Any assistance would be appreciated.
Thanks.
NSAutoreleasePool *apool = [[NSAutoreleasePool alloc] init];
UIActivityIndicatorView *aiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
UIBarButtonItem *activityButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aiv];
[aiv startAnimating];
[aiv release];
[self.navigationController.navigationItem setRightBarButtonItem:activityButtonItem animated:YES];
[activityButtonItem release];
[apool release];
First a few pointers.
You use an NSAutoReleasePool
in circumstances where you are creating a lot of autoreleased objects in a short time, e.g. in a loop. Quoting Apple:
If your application creates a lot of temporary autoreleased objects within the event loop, however, it may be beneficial to create “local” autorelease pools to help to minimize the peak memory footprint.
Since you are not creating any autoreleased objects it's not necessary to have the NSAutoReleasePool
.
Normally you would put the code to set the buttons of the navigation bar in viewDidLoad
of the UIViewController
that should respond to the button. When UINavigationController
pushes a new UIViewController
on to its stack it will ask that controller for whatever buttons should be displayed.
Let's say the root view controller of your UINavigationController
is the default generated RootViewController
. Then you should put the code in viewDidLoad
of RootViewController
. Or perhaps in viewDidAppear:
since you are animating the insertion.
UIActivityIndicatorView *aiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
UIBarButtonItem *activityButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aiv];
[aiv startAnimating];
[aiv release];
[self.navigationItem setRightBarButtonItem:activityButtonItem animated:YES];
[activityButtonItem release];
If you really must have the code in the app delegate that it can be accomplished by:
UIActivityIndicatorView *aiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
UIBarButtonItem *activityButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aiv];
[aiv startAnimating];
[aiv release];
UIViewController *rootController = [self.navigationController topViewController];
rootController.navigationItem.rightBarButtonItem = activityButtonItem;
[activityButtonItem release];
Robert thanks for the advice.
I have taken out the autorelease and appreciate should normally be used in the ViewDidLoad and can confirm works perfectly when used there to start the activity animation.
The issue i have is:
i can use the example code in the relevant view controller to start the animation
UIActivityIndicatorView *aiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
UIBarButtonItem *activityButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aiv];
[aiv startAnimating];
[aiv release];
[self.navigationItem setRightBarButtonItem:activityButtonItem animated:YES];
[activityButtonItem release];
but i have a singleton object for IAP which is called from various view controllers within my app and i need to be able to stop the activity animation - think need to just set the rightBarButton = nil but struggling to work out how the singleton object can tell the corresponding view controller to stop the animation.
I thought about trying to use something like:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (IAP_complete:) name: IAP_completeNotification object:nil];
but haven't figured out the complete process start to finish. Appreciate any help you can offer on;
- viewController setting the rightBarItem and starting the animation (done)
- setting up NSNotificationCenter to listen for IAP completed (if this is the best way to achieve)
- setting up the singleton object NSNotificationCenter to broadcast the IAP is complete
To expand on this
I am using the MKStoreKit 3 by Mugunth Kumar @ http://blog.mugunthkumar.com/coding/introducing-mkstorekit-–-version-3/
I have modified this to suit my needs but simply put;
In my VC i use the following to initiate the in app purchase
//display activity indicator
UIActivityIndicatorView *aiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
UIBarButtonItem *activityButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aiv];
[aiv startAnimating];
[aiv release];
[self.navigationItem setRightBarButtonItem:activityButtonItem animated:YES];
[activityButtonItem release];
//begin in app purchase
[[MKStoreManager sharedManager] buyFeature:appID];
Once this is complete i need to tell the VC that this is complete.
in the file MKStoreManager.m
-(void) provideContent: (NSString*) productIdentifier forReceipt:(NSData*) receiptData
i unlock or download content here but need to stop the activity indicator also which is where i am missing the plot.
Thanks, Mike
精彩评论