关于ios:用singleton调用类方法中的实例方法

Calling instance method in class method with Singleton

我已经读过在类方法中调用实例方法是使用单例方法的最佳方法。

我试过了,但没用。我试图调用@selector()中的void方法,但我不能。请问我的问题在哪里?

h

1
2
+ (void)normalizeCell:(UITableViewCell *)cell withLables:(NSArray *)lbls sx:(CGFloat)sx widths:(NSArray *)widths;
- (void)edit;

m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
+ (void)normalizeCell:(UITableViewCell *)cell withLables:(NSArray *)lbls sx:(CGFloat)sx widths:(NSArray *)widths
{

    static PozisyonOzetiTableCell *sharedMyManager = nil;
static BOOL initialized = NO;

if(!initialized)
{
    initialized = YES;
    sharedMyManager = [[PozisyonOzetiTableCell alloc] init];
}
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

//// I have tried edit & [sharedMyManager edit]
[button addTarget:self action:@selector(edit) forControlEvents:UIControlEventTouchUpInside];

[button setImage:[UIImage imageNamed:@"settingKey.png"] forState:UIControlStateNormal];
button.frame = CGRectMake(-23, 0, 70, 40);
[cell.contentView addSubview:button];
}

- (void)edit
{
    NSLog(@"CLICKED");
}

在+方法中,不能将self用于该puspose,因为类方法中的self返回Class而不是实例。

代替

[button addTarget:self action:@selector(edit) forControlEvents:UIControlEventTouchUpInside];

具有

[button addTarget:sharedMyManager action:@selector(edit) forControlEvents:UIControlEventTouchUpInside];

当然,如果您的sharedmymanager是静态字段。并确保在调用该方法之前已初始化SharedmyManager。