【问题标题】:can't call class method with parameter不能用参数调用类方法
【发布时间】:2014-06-17 18:39:19
【问题描述】:

我无法从视图控制器调用类方法。类方法将 BOOL 作为输入。 Xcode 允许我在没有参数的情况下调用它(这显然会崩溃),但是当我传递参数时会报错。

以下是相关代码:

// RLI.h
@interface RLI : NSObject <CLLocationManagerDelegate>
...
+(BOOL)askForLocationAccess;
end

// RLI.m
@implementation RLI
...
+(BOOL)askForLocationAccess:(BOOL)ask {
    ...
    return answer;
}
end

// CLOnboardingViewController.m
@implementation CLOnboardingViewController
...
-(IBAction)allowGPSAccess:(id)sender {
//    NSLog(@"%@", [sender currentTitle]);
    BOOL ask = [[sender currentTitle] isEqualToString:gpsLabel];
    [RLI askForLocationAccess:YES]; // No known class method for selector 'askForLocationAccess:'
    [RLI askForLocationAccess]; // no complaint but crashes with "unrecognized selector sent to a class"
    [RLI askForLocationAccess:ask]; // what I intend to do

    ...
}
end

如果需要更多代码,请告诉我,但这似乎应该足够简单。我查看了一堆“没有已知的选择器类方法”问题,但其中大多数要么是拼写错误,要么是调用实例方法。

【问题讨论】:

    标签: ios objective-c class-method


    【解决方案1】:

    我认为这是因为您的头类具有没有参数的方法。 这就是您的 .h 文件中的内容:

    +(BOOL)askForLocationAccess;
    

    改成:

    +(BOOL)askForLocationAccess:(BOOL)ask;
    

    【讨论】:

    • 我以为我已经尝试过了,但显然没有。我知道这一定很简单!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 2014-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多