Reputation: 17695
UIAlertView
is widely used in the code.
I would like to check all alert view message before displaying it.
I have written a method in a category of UIAlertView
.
Question:
Note - I have already a method for this, but I don't want to change the code manually in all the places, instead I am looking for a sleek solution if that is possible such that I change in one place (like override a method)
Upvotes: 1
Views: 275
Reputation: 1026
Create a category on UIAlertView and provide a method that checks first if the message exists, then shows:
@implementation UIAlertView (OnlyShowIfMessageExists)
- (void)override_show
{
if(self.message.length)
{
[self override_show];
}
}
It is calling override_show and not show, because those methods will be swizzled.
Implement the +(void)load method as well in the category, and swizzle your method with the show method:
+(void)load
{
SEL origSel = @selector(show);
SEL overrideSel = @selector(override_show);
Method origMethod = class_getInstanceMethod(UIAlertView.class, origSel);
Method overrideMethod = class_getInstanceMethod(UIAlertView.class, overrideSel);
if(class_addMethod(UIAlertView.class, origSel, method_getImplementation(overrideMethod), method_getTypeEncoding(overrideMethod)))
{
class_replaceMethod(UIAlertView.class, overrideSel, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
}
else
{
method_exchangeImplementations(origMethod, overrideMethod);
}
}
@end
Now, all calls to show on a UIAlertView will be using your method override_show.
http://www.mikeash.com/pyblog/friday-qa-2010-01-29-method-replacement-for-fun-and-profit.html
Upvotes: 1