Vinodh
Vinodh

Reputation: 5268

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

I have designed to class to have common code along project to show alert with title, message, a button titles, Which is working fine with objective C code.

But I want to utilise the same code in one of my swift project, am unable to call this method and provide other button titles

Note that am unable to access like

CustomAlertView.initWithTitle...... 

Upvotes: 1

Views: 687

Answers (2)

Rahul Mayani
Rahul Mayani

Reputation: 3831

Use this...

CustomAlertView.h file

@interface CustomAlertView : UIAlertView

+(CustomAlertView *)sharedInstance;

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

@end

CustomAlertView.m file

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

& then call your method...

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

Upvotes: 0

JAL
JAL

Reputation: 42459

It should look something like this:

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

I'm not sure what CustomAlertView is. If that's your class, replace UIAlertView with CustomAlertView in the initializer.

otherButtonTitles is a comma separated list of Strings:

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

You don't need to use a singleton like in Rahul's answer.

Assuming your CustomAlertView.h file looks like this:

#import <UIKit/UIKit.h>

@interface CustomAlertView : UIAlertView

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

@end

You can import CustomAlertView.h into your bridging header and initialize the class like this in Swift 3:

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

Upvotes: 1

Related Questions