2016-09-23 67 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