在类别中响应UIAlertViewDelegate

时间:2015-07-09 08:19:45

标签: ios objective-c delegates uialertview objective-c-category

我有一个像这样的简单类

·H

@interface DJMyClass : NSObject
+ (void)someFunction;
@end

的.m

#import "DJJavaFunction+UIAlert.h"

@interface DJMyClass ()
@property (nonatomic, strong) NSString* someString;
@end
@implementation DJMyClass
+(void)functionWithName:(NSString *)functionName
                 callbackId:(int)callbackId
                       args:(NSArray *)args
{
    DJMyClass *newFunction = [[DJMyClass alloc] init];
    newFunction.function = functionName;
    newFunction.callbackId = callbackId;
    newFunction.args = args;

    [newFunction processFunction];
}
- (void)processFunction
{
  [self showAlertWithTitle:title message:message buttons:buttons inputField:inputField callbackId:_callbackId];
}
@end

现在我已经创建了这个类的不同扩展,我在这个类中导入和使用它。

·H

@interface DJMyClass (UIAlert) <UIAlertViewDelegate>

- (void)showAlertWithTitle:(NSString *)title
                   message:(NSString *)message
                   buttons:(NSArray *)buttons
                inputField:(NSDictionary *)inputField
                callbackId:(int)callbackId;

@end

的.m

@implementation DJJavaFunction (UIAlert)

- (void)showAlertWithTitle:(NSString *)title
                   message:(NSString *)message
                   buttons:(NSArray *)buttons
                inputField:(NSDictionary *)inputField
                callbackId:(int)callbackId
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                        message:message
                                                       delegate:self
                                              cancelButtonTitle:nil
                                              otherButtonTitles:nil];
        // Add the buttons
        for (NSString *btn in buttons)
        {
            [alert addButtonWithTitle:btn];
        }

        // Add text field
        if (!inputField)
        {
            alert.alertViewStyle = UIAlertViewStyleDefault;
        }
        else
        {
            alert.alertViewStyle = UIAlertViewStylePlainTextInput;

            UITextField *textField = [alert textFieldAtIndex:0];
            textField.placeholder = inputField[@"placeholder"];
            textField.text = inputField[@"text"];
        }

        alert.delegate = self;
        alert.tag = 44;
        [alert show];
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

@end

这一切都很好,除了永远不会调用UIAlertViewDelegate方法。 甚至可以从类别中响应UIAlertViewDelegate调用吗?

由于

0 个答案:

没有答案