-(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;
}
我设计了类以在项目中使用通用代码来显示带有标题、消息、按钮标题的警报,这与 objective-c 代码一起工作良好。
但我想在我的一个 swift 项目中使用相同的代码,无法调用此方法并提供其他按钮标题
注意我无法访问like
CustomAlertView.initWithTitle......
最佳答案
它应该看起来像这样:
UIAlertView(title: "Title", message: "Message", delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "OtherButton1", "OtherButton2")
我不确定 CustomAlertView
是什么。如果这是您的类(class),请在初始化程序中将 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
导入到您的桥接 header 中,并在 Swift 3 中像这样初始化类:
CustomAlertView(title: "Title", message: "Message", delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "Other1", "Other2")
关于ios - 如何快速访问 Customalertview objective-c 实例类型方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39659603/