【问题标题】:Objective-c calling a method from class methodObjective-c 从类方法调用方法
【发布时间】:2017-01-18 11:09:30
【问题描述】:

我正在尝试从类方法访问实例方法。我收到此错误

+[ActiveVC goToDashBoard]:无法识别的选择器发送到类 0x112010

*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“+ [ActiveVC goToDashBoard]: 无法识别的选择器发送到类 0x112010'

我的代码

+ (void) removeClosedVisitor:(NSString *) visitorID{

    for (NSInteger i = activelist.count - 1; i >= 0 ; i--) {
        ActiveItemObject *item = [activelist objectAtIndex:i];
        if ([visitorID isEqualToString:item.VisitorId]) {
            NSLog(@"Removing Visitor from Active List -- %@", visitorID);
            [activelist removeObjectAtIndex:i];
            //[self.incommingTable reloadData];

//            NSDictionary *activeDictionary = [[NSDictionary alloc] init];
//            activeDictionary = [activelist mutableCopy];
//            
//            [[NSNotificationCenter defaultCenter]
//             postNotificationName:@"PassData"
//             object:nil
//             userInfo:activeDictionary];

            [[self class] goToDashBoard];
        }
    }
}



- (void) goToDashBoard{
    NSLog(@"Segue to Dashboard");
    UITabBarController *dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"id_tabView"];
    [dvc setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
    [self presentViewController:dvc animated:YES completion:nil];

}

谁能帮我解决这个问题。 tnx。

【问题讨论】:

  • - (void) goToDashBoard 是一个实例方法。如果要使其成为类方法,无需实例即可访问,请将签名更改为+ (void) goToDashBoard

标签: ios objective-c


【解决方案1】:

您需要创建类的实例或将类转换为单例。例如:[[ActiveVC sharedInstance] goToDashBoard];

创建单例类的方法如下:

首先,创建一个新文件并将其从NSObject 子类化。随便命名,我们将在这里使用CommonClass。 Xcode 现在将为您生成 CommonClass.h 和 CommonClass.m 文件。

在您的CommonClass.h 文件中:

#import <Foundation/Foundation.h>

@interface CommonClass : NSObject {
}
+ (CommonClass *)sharedObject;
@property NSString *commonString;
@end

在您的CommonClass.m 文件中:

#import "CommonClass.h"

@implementation CommonClass

+ (CommonClass *)sharedObject {
    static CommonClass *sharedClass = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedClass = [[self alloc] init];
    });
    return sharedClass;
}

- (id)init {
    if (self = [super init]) {
        self.commonString = @"this is string";
    }
    return self;
}

@end

【讨论】:

    【解决方案2】:

    如果你想调用实例方法,那么你需要一个实例变量,所以创建这个类的实例变量并调用它。

    【讨论】:

      【解决方案3】:

      goToDashBoard 设为类方法。由于您没有在此处创建任何实例,因此如果它不是类方法,则无法执行。

      + (void) goToDashBoard
      

      【讨论】:

        【解决方案4】:

        您实际上在任何地方都有实例吗?如果没有,您将不得不创建一个:

        [self.sharedInstance goToDashBoard]
        
        [[self alloc] init] goToDashBoard]
        

        我假设您确实有一个实例,因为它看起来像一个视图控制器。在这种情况下,我建议您将实例传递给静态方法。

        + (void) removeClosedVisitor:(NSString *) visitorID viewController: (xxx) viewController {
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-02-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-12
          • 2023-03-29
          • 2012-01-21
          • 2011-05-26
          相关资源
          最近更新 更多