如何在 swift 中访问 Customalertview objective C instancetype 方法

How to access Customalertview objective C instancetype method in swift

-(instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...
{
    va_list args;
    va_start(args, otherButtonTitles);
    NSMutableArray *otherButtonsArray = [[NSMutableArray alloc] init];
    for (NSString *arg = otherButtonTitles; arg != nil; arg = va_arg(args, NSString*))
    {
        [otherButtonsArray addObject:arg];
    }
    va_end(args);

    if (POST_iOS8) {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >70120
        self = [super init];
        alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];

        int buttonIndex = 0;
        if(cancelButtonTitle)
        {
            CustomAlertAction *cancelAction =[CustomAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                if(delegate)
                {
                    if ([delegate respondsToSelector:@selector(alertView:clickedButtonAtIndex:)]) {
                        [delegate alertView:self clickedButtonAtIndex:((CustomAlertAction*)action).buttonIndex];
                    }
                }
            }];
            [cancelAction setButtonIndex:buttonIndex];
            [alertController addAction:cancelAction];
            buttonIndex++;
        }

        for (NSString *otherButton in otherButtonsArray)
        {
            CustomAlertAction *otherAction =[CustomAlertAction actionWithTitle:otherButton style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                if(delegate)
                {
                    if ([delegate respondsToSelector:@selector(alertView:clickedButtonAtIndex:)]) {
                        [delegate alertView:self clickedButtonAtIndex:((CustomAlertAction*)action).buttonIndex];
                    }
                }
            }];
            [otherAction setButtonIndex:buttonIndex];
            [alertController addAction:otherAction];
            buttonIndex++;
        }

#endif

    }
    else
    {
        self = [super initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil];
        for (NSString *otherButton in otherButtonsArray)
        {
            [self addButtonWithTitle:otherButton];
        }
    }
    return self;
}

我已经设计 class 在项目中使用通用代码来显示带有标题、消息、按钮标题的警报,这与 objective C 代码一起工作正常。

但我想在我的一个 swift 项目中使用相同的代码,无法调用此方法并提供其他按钮标题

注意我无法访问like

CustomAlertView.initWithTitle...... 

它应该看起来像这样:

UIAlertView(title: "Title", message: "Message", delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "OtherButton1", "OtherButton2")

我不确定 CustomAlertView 是什么。如果那是你的 class,请在初始化程序中将 UIAlertView 替换为 CustomAlertView

otherButtonTitles 是逗号分隔的字符串列表:

public convenience init(title: String, message: String, delegate: UIAlertViewDelegate?, cancelButtonTitle: String?, otherButtonTitles firstButtonTitle: String, _ moreButtonTitles: String...)

您不需要像 Rahul 的回答那样使用单例。

假设您的 CustomAlertView.h 文件如下所示:

#import <UIKit/UIKit.h>

@interface CustomAlertView : UIAlertView

-(instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...;

@end

您可以将 CustomAlertView.h 导入桥接 header 并在 Swift 3:

中像这样初始化 class
CustomAlertView(title: "Title", message: "Message", delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "Other1", "Other2")

使用这个...

CustomAlertView.h 文件

@interface CustomAlertView : UIAlertView

+(CustomAlertView *)sharedInstance;

//your method
-(instancetype)initWithTitle:(NSString *)title ...

@end

CustomAlertView.m 文件

#import "CustomAlertView.h"

@implementation CustomAlertView

-(id)init
{
    if (self = [super init])
    {
    }
    return self;
}

+ (CustomAlertView *) sharedInstance {

    static dispatch_once_t pred = 0;

    __strong static CustomAlertView * _sharedObject = nil;

    dispatch_once(&pred, ^{
        _sharedObject = [[self alloc] init];
    });

    return _sharedObject;
}

//your method
-(instancetype)initWithTitle:(NSString *)title ...

&然后调用你的方法...

CustomAlertView.sharedInstance().initWithTitle ......