如何在swift

时间:2016-09-23 11:24:31

标签: ios swift uialertview swift3 uialertcontroller

-(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;
}

我已经设计为在项目中使用公共代码来显示带有标题,消息,按钮标题的警报,这对于目标C代码工作正常。

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

请注意,我无法访问

CustomAlertView.initWithTitle...... 

2 个答案:

答案 0 :(得分:1)

看起来应该是这样的:

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

我不确定CustomAlertView是什么。如果这是您的课程,请在初始化程序中将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导入到桥接标头中并在Swift 3中初始化类:

CustomAlertView(title: "Title", message: "Message", delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "Other1", "Other2")

答案 1 :(得分:0)

使用此...

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 ......