【问题标题】:How to access Customalertview objective C instancetype method in swift如何快速访问Customalertview目标C实例类型方法
【发布时间】: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


【解决方案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...)

您不需要像 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 导入到您的桥接头中,并在 Swift 3 中像这样初始化类:

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

【讨论】:

  • CustomAlertView 是一个客观的 C 类,而不是一个 swift 类
  • @Vinodh 你能贴出类的完整接口和实现吗?
  • @Vinodh 如果您使用我上面发布的代码并将UIAlertView 替换为CustomAlertView,会发生什么?
  • 请仔细阅读我的问题,我在 obj c 中有一个类,我想在方法中访问该类以显示警报。请让我知道该怎么做。
  • 这是正确答案。 @Vinodh,您显然不了解桥接的工作原理。一旦你完成了@JAL 建议的操作(将你的 Objective-C 类的标题添加到桥接头中),你可以通过调用 CustomAlertView(title: ... 来使用来自 Swift 的 init。不要尝试调用在 Swift 中没有意义的 CustomAlertView.initWithTitle......(init 方法看起来只是不同,并且桥接会相应地调整它们)。这将调用一个名为init... 的类方法,显然它不存在。
【解决方案2】:

使用这个...

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

【讨论】:

  • 没有理由使用单例将此方法暴露给 Swift。
  • 原因是实例方法不是直接访问另一个类。
  • 重新编辑我的答案:UIAlertView 在 Swift 3.0 中工作得非常好,如果您以 iOS 7.x 为目标,这是您唯一的选择。你有没有试过我的代码?
  • 如果像我的回答一样正确放置在标题中,实例方法 is 可访问,这就是我假设 OP 也在做的事情。
  • swift 3.0 deprecated UIAlertView class 这是非常不正确的。 UIAlertView 在 iOS 9 中被弃用,不是 Swift 3.0。请参阅Apple Documentation
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-12
  • 1970-01-01
  • 1970-01-01
  • 2019-01-11
  • 2011-09-15
  • 1970-01-01
  • 2015-07-09
相关资源
最近更新 更多