ios - Objective-c 从类方法调用方法

标签 ios objective-c

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

+[ActiveVC goToDashBoard]: unrecognized selector sent to class 0x112010

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[ActiveVC goToDashBoard]: unrecognized selector sent to class 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];

}

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

最佳答案

您需要创建类的实例或将类转换为单例。例如:[[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

关于ios - Objective-c 从类方法调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41717858/

相关文章:

ios - 关于iOS开发。当我在我的 iPhone6 上运行我的项目时,启动屏幕没有显示,但是,它在模拟器 iPhone6 上运行时显示

ios - 从另一个没有参数的方法调用 IBAction

iphone - objective-c后台进程应用

ios - 如何禁用 iOS 键盘上的小数点键

iphone - 在 iOS 中如何输出我的 sqlite3 数据库表结构?

ios - react native iOS 深度链接;如何在 url 中使用 `@` 字符

ios - 在未通过 segue 连接的 UIViewController 和 UITableViewController 之间传递数据

iphone - 如何拆分由 ","分隔的字符串中的项目

objective-c - Swift 中是否有工厂方法的约定?

iOS - 不同 View Controller 的不同屏幕方向