【发布时间】:2013-04-25 13:05:26
【问题描述】:
我在许多视图控制器中单击按钮时显示自定义 UIView(类似于 UIAlertView)。因此,我已将该代码包含在 NSObject 类中。我必须将 UITableView 显示为此自定义视图的子视图,并且我的 UITableView 单元格之一包含倒数计时器。我能够显示 CustomView 和 UITableView。但是,我无法在这个 customView 中实现 NSTimer。
显示自定义视图的代码:
+(NSMutableDictionary *) printPopup:(UIView *)inputView {
int i=20;
UIView *myCustomView = [[UIView alloc] initWithFrame:CGRectMake(20, 100, 280, 250)];
[myCustomView setBackgroundColor:[UIColor colorWithRed:(247/255.0) green:(239/255.0) blue:(218/255.0) alpha:1]];
UITableView *dynamicTable=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 280, 250) style:UITableViewStylePlain];
dynamicTable.tag=55;
[myCustomView addSubview:dynamicTable];
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// [cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
[cancelButton setBackgroundImage:[UIImage imageNamed:@"Delete3.png"] forState:UIControlStateNormal];
[cancelButton setFrame:CGRectMake(250, 0, 30, 30)];
cancelButton.tag=i+7;
[myCustomView addSubview:cancelButton];
[inputView addSubview:myCustomView];
[UIView animateWithDuration:0.4f animations:^{
[myCustomView setAlpha:1.0f];
}];
NSMutableDictionary *returnedDic=[[NSMutableDictionary alloc]init];
[returnedDic setObject:dynamicTable forKey:@"Tableview"];
[returnedDic setObject:cancelButton forKey:@"cancelButton"];
return returnedDic;
}
+ (void)dismissCustomView:(UIButton *)sender
{
[UIView animateWithDuration:0.2f animations:^{
[sender.superview setAlpha:0.0f];
}completion:^(BOOL done){
[sender.superview removeFromSuperview];
}];
}
当我试图在这个 NSObject 类中实现 NSTimer 时,我收到以下错误:
instance variable accessed in class method
我必须在这个类中调用 NSTimer。有没有其他选择。
请提出建议。
【问题讨论】:
-
您不能访问类方法中的任何实例变量(以“+”开头的方法)。但是,我在这两种方法中看不到任何看起来像实例变量的东西。您是否正在使用其他包含实例变量的类方法?
-
@Eric,我想在 printPopup 中添加这一行,gameTimer = [NSTimer timerWithTimeInterval:30 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:gameTimer forMode:NSDefaultRunLoopMode]; [runLoop 运行];
-
当我想添加这一行时,我遇到了指定的错误
标签: iphone objective-c nstimer