2016-09-23 46 views
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; 
} 

我设计类Customalertview目标C instancetype方法有共同的代码一起项目来显示警报与标题,消息,按钮标题,这是工作的罚款与客观的C代码。如何访问迅速

但我想利用我迅速的项目之一相同的代码,我无法调用此方法,并提供其他按钮标题

注意,我无法访问像

CustomAlertView.initWithTitle...... 
+0

'CustomAlertView.initWithTitle ......' –

+0

我想这是不工作@先生 – Vinodh

+0

这个初始化程序来自UIAlertView,它从iOS 9开始不推荐使用。也许最好是只用UIAlertController来代替它。从这个答案http://stackoverflow.com/a/24022764/1638166它似乎这个UIAlertView初始化器从未工作。 – johnyu

回答

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

你不需要在拉胡尔的回答使用单等。

假设你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到您的桥接报头和初始化像这样的类斯威夫特3:

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

CustomAlertView是一个客观的C类,而不是一个快速的类 – Vinodh

+0

@Vinodh你可以发布该类的完整接口和实现吗? – JAL

+0

@Vinodh如果你使用我上面发布的代码并用'CustomAlertView'替换'UIAlertView',会发生什么? – JAL

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

+0

没有理由使用单例将此方法暴露给Swift – JAL

+0

原因是实例方法不能直接访问另一个类 – Rahul

+0

重新编辑我的答案:'UIAlertView'完美适用于Swift 3.0 ,如果你的目标是iOS 7.x.你甚至可以试试我的代码吗? – JAL