views:

13

answers:

1

I have a common UIActionSheet which I use across about 10 different view/view controllers. I'm wondering if it's possible to use UIActionSheet from the app delegate in order to prevent code duplication?

So far my attempts to use an action sheet from the delegate haven't worked, I suspect my problem lies when calling the showInView method - do I need to instantiate an object of my view controller then use viewController.view here? If so how can I then tell which view called the action sheet method from the delegate?

A: 

I was having the same problem, and I recently figured out a way to fix it in my app. The key for me was to make my app delegate class an extension of UIViewController rather than NSObject. (I think UIViewController is a subclass of NSObject anyway, so this shouldn't affect your app too much.)

In other words, change the main implementation line in your app delegate interface file from something like this:

@interface YourAppDelegate : NSObject <UIApplicationDelegate, UIActionSheetDelegate> {

To this:

@interface YourAppDelegate : UIViewController <UIApplicationDelegate, UIActionSheetDelegate> {

You should now be able to use the showInView: method with your action sheet within your app delegate implementation:

[yourActionSheet showInView:self.view];  
c_phlat