简体   繁体   English

在NSObject类中使用NSTimer

[英]Using NSTimer in NSObject class

I am displaying a custom UIView (which is like UIAlertView) on clicking a button in many view controllers.So, I have included that code in NSObject class. 在许多视图控制器中单击按钮时,我正在显示一个自定义UIView(类似于UIAlertView)。因此,我已将该代码包含在NSObject类中。 I have to display UITableView as a subview to this custom view, and one of my UITableView cell contains countdown timer. 我必须将UITableView显示为该自定义视图的子视图,并且我的UITableView单元格之一包含倒数计时器。 I am able to display the CustomView and also UITableView. 我能够显示CustomView以及UITableView。 But, I am not able to implement NSTimer in this customView. 但是,我无法在此customView中实现NSTimer。

Code to display custom view: 显示自定义视图的代码:

+(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];
    }];

}

When I am trying to implement the NSTimer in this NSObject class, I am getting the following error: 当我尝试在此NSObject类中实现NSTimer时,出现以下错误:

instance variable accessed in class method

I have to call the NSTimer in this class. 我必须在此类中调用NSTimer。 Is there any alternatives. 有没有其他选择。

Please suggest. 请提出建议。

The syntax you are using for defining your methods is that of class methods (the little + before the return type). 用于定义方法的语法是类方法的语法(返回类型前的小号+ )。 If you want instance methods they should be prefixed with a minus sign - . 如果要使用实例方法,则应在它们前面加上减号- Since you seem to require access to instance variables, your methods should probably be instance methods. 由于您似乎需要访问实例变量,因此您的方法可能应该是实例方法。

Note that instance methods still have access to all of the static "class" variables. 请注意,实例方法仍然可以访问所有静态“类”变量。

Re: you comment. 回复:你发表评论。 You can't call self in a class method unless that method you're referencing is a class method also. 除非您要引用的方法也是类方法,否则您不能在类方法中调用self Your NSTimer timerWithInterval: method violates that. 您的NSTimer timerWithInterval:方法违反了该规定。 I'm guessing timerFired: is an instance method? 我猜想timerFired:是实例方法吗? Easiest way to say it is that two (instance & variable methods) can't be intertwined from within the same class. 最简单的说法是,两个(实例和变量方法)不能在同一个类中交织在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM