【发布时间】:2016-09-23 11:24:31
【问题描述】:
-(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;
}
我已经设计了在项目中使用通用代码来显示带有标题、消息、按钮标题的警报,这与目标 C 代码配合得很好。
但我想在我的一个 swift 项目中使用相同的代码,无法调用此方法并提供其他按钮标题
请注意,我无法访问喜欢
CustomAlertView.initWithTitle......
【问题讨论】:
-
CustomAlertView.initWithTitle...... -
我试过它不起作用@Mr.UB
-
这个初始值设定项来自 UIAlertView,自 iOS 9 以来已被弃用。也许改用 UIAlertController 会更好?从这个答案stackoverflow.com/a/24022764/1638166看来,这个 UIAlertView 的初始化程序似乎从来没有工作过。
标签: ios swift uialertview swift3 uialertcontroller