Вопрос

This is a very interesting problem that I have. My app had an issue with presentViewController:animated:completion:, and dismissViewControllerAnimated:completion:. What happened is the presentViewController gets called, and based off of server information, it's possible that the View Controller will be dismissed. I would get an error saying "Can't dismiss before it's fully presented" (Animation is set to YES).

I implemented a queue to handle the present, and dismiss calls. This works perfect, and I'm really happy with this solution. Then I realized another problem, what if I accidentally call Apple's methods directly (I have a method called myPresentViewController:animated:completion:, and myDismissViewControllerAnimated:completion: that handle the queuing).

Is there a way for me to set up a warning method if I call Apple's methods directly? I've tried creating a Category (I've also tried an Extension), defining Apple's present, and dismiss methods, and adding a deprecated message to them. Neither of these ways worked. I've thought about swizzling the methods, but that doesn't work because if the swizzled method adds it to the queue, how will it know when to call apple's implementation? I realize that the warning will show up in the one spot where I need to call Apple's method, but I can use #pragma to inhibit warnings for the two lines I need to.

Это было полезно?

Решение

Create a view controller base class that extends UIViewController. Then make sure all of your view controllers extend this base class. Do the same for UITableViewController if needed.

In these base classes you can implement the two methods and add your deprecation flag to the method declarations in the .h file.

TGViewController.h

@interface TGViewController : UIViewConrtoller

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion NS_DEPRECATED_IOS(5_0, 6_0);
- (void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^)(void))completion NS_DEPRECATED_IOS(5_0, 6_0);

@end

TGViewController.m

@implementation TGViewController

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion {
    [super presentViewController:viewControllerToPresent animated:flag completion:completion];
}

- (void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^)(void))completion {
    [super dismissControllerAnimated:flag completion:completion];
}

@end

No pragmas needed.

You can also use these base classes to add any other app-level features you might want.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top