我正在尝试从类方法访问实例方法。我收到这个错误
+[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/