1
-(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...... 
JAL
  • 41,701
  • 23
  • 172
  • 300
Vinodh
  • 5,262
  • 4
  • 38
  • 68
  • `CustomAlertView.initWithTitle......` – Bista Sep 23 '16 at 11:25
  • i tried it's not working @Mr.UB – Vinodh Sep 23 '16 at 11:27
  • This initializer is from UIAlertView, which is deprecated since iOS 9. Maybe it's better to just go for UIAlertController instead? From this answer http://stackoverflow.com/a/24022764/1638166 it would seem that this initializer for UIAlertView never worked. – johnyu Sep 27 '16 at 08:06

2 Answers2

1

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")
JAL
  • 41,701
  • 23
  • 172
  • 300
  • CustomAlertView is an objective C class and not an swift class – Vinodh Sep 28 '16 at 01:51
  • @Vinodh can you post the full interface and implementation of the class? – JAL Sep 28 '16 at 02:10
  • @Vinodh what happens if you use the code I posted above and replace `UIAlertView` with `CustomAlertView`? – JAL Sep 28 '16 at 02:39
  • please read my question carefully , i have a class in obj c and i want access the class in method to show alert . Please let me know how to do it . – Vinodh Sep 28 '16 at 02:58
  • @Vinodh Right, so the first step would be to import that class through a bridging header. The second step would be to call CustomAlertView like I posted above: `CustomAlertView(title: "Title", message: "Message", delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "OtherButton1", "OtherButton2")` – JAL Sep 28 '16 at 04:09
  • @Vinodh see my updated answer for Swift 3. You don't need to use a singleton like in Rahul's answer. – JAL Sep 28 '16 at 13:09
  • @JAL I think category class is no need to use a singleton, but `CustomAlertView` is not a category class. So, @Vinodh use of singleton like my answer. – Rahul Mayani Sep 28 '16 at 13:54
  • @Rahul `CustomAlertView` is not a category, it is a subclass of `UIAlertView`. – JAL Sep 28 '16 at 13:54
  • @JAL Yeah, my side doesn't work. So, How to access this method. – Rahul Mayani Sep 28 '16 at 13:59
  • 1
    This is the correct answer. @Vinodh, you apparently don't understand how the bridging works. Once you have done what @JAL suggests (adding your Objective-C class's header to the bridging header) you can use the `init` from Swift by calling `CustomAlertView(title: ...`. Don't try to call `CustomAlertView.initWithTitle......` that doesn't make sense in Swift (init methods look just differently and the bridging adapts them accordingly). That would call a class method called `init...`, which, obviously doesn't exist. – Gero Sep 29 '16 at 13:38
0

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

Rahul Mayani
  • 3,761
  • 4
  • 25
  • 40
  • There is no reason to use a singleton to expose this method to Swift. – JAL Sep 28 '16 at 13:12
  • Reason is instance method not a direct access to another class. – Rahul Mayani Sep 28 '16 at 13:27
  • Re your edit on my answer: `UIAlertView` works perfectly fine in with Swift 3.0, and is your only option if you are targeting iOS 7.x. Did you even try my code? – JAL Sep 28 '16 at 13:31
  • And the instance method *is* accessible if placed in the header properly like in my answer, which is what I'm assuming the OP is doing as well. – JAL Sep 28 '16 at 13:32
  • `swift 3.0 deprecated UIAlertView class` this is horribly incorrect. `UIAlertView` is deprecated in iOS 9, **not** Swift 3.0. See the [Apple Documentation](https://developer.apple.com/reference/uikit/uialertview). – JAL Sep 28 '16 at 13:34
  • I don't think you understand how deprecations work. Xcode doesn't cause the deprecation, the iOS SDK level does. Change the Deployment Target of an app in Xcode 8 to 7.0, you will no longer get a warning that `UIAlertView` is deprecated. – JAL Sep 28 '16 at 13:40
  • Yeah, you are right, but I,m using ios9 to a higher level SDK so, a warning is thrown.... – Rahul Mayani Sep 28 '16 at 13:45