Ramu Pasupuleti
Ramu Pasupuleti

Reputation: 886

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. I have to display UITableView as a subview to this custom view, and one of my UITableView cell contains countdown timer. I am able to display the CustomView and also UITableView. But, I am not able to implement NSTimer in this customView.

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:

instance variable accessed in class method

I have to call the NSTimer in this class. Is there any alternatives.

Please suggest.

Upvotes: 0

Views: 390

Answers (2)

Eric
Eric

Reputation: 4061

Re: you comment. You can't call self in a class method unless that method you're referencing is a class method also. Your NSTimer timerWithInterval: method violates that. I'm guessing timerFired: is an instance method? Easiest way to say it is that two (instance & variable methods) can't be intertwined from within the same class.

Upvotes: 0

aLevelOfIndirection
aLevelOfIndirection

Reputation: 3522

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.

Upvotes: 1

Related Questions