Madhubalan K
Madhubalan K

Reputation: 357

NStimer issue in Cocoa-Touch

My application needs to show a temporary message, so I created Toast like android myself. It works well for only one toast. I am using NSTimer to hide the message. If I display more than one toast the timer becomes a problem. Only the last added toast becomes hidden, others do not become hidden. How can I hide all toasts?

Code:

Remove function:

-(void)removeToast
{
 NSLog(@"removed");
 [self.view removeFromSuperview];
}

Timer start:

timer = [NSTimer scheduledTimerWithTimeInterval:(4.0f) 
                                         target:self  
                                       selector:@selector(xxxx)
                                       userInfo:nil repeats:NO];

I wrote this in above in separate NSObject class and created an object. I need some clarification on how run the two NSTimer simultaneously or keep track of all NSObjects.

Object creation in viewcontroller is @property(nonatomic,strong)Toast *toast;

Upvotes: 0

Views: 127

Answers (1)

andreamazz
andreamazz

Reputation: 4286

No need to keep the istance of timer in a ivar for your purpose. Use the userinfo: parameter to pass the view you mean to hide when the timer fires, like this:

[NSTimer scheduledTimerWithTimeInterval:(4.0f) 
                                     target:self  
                                   selector:@selector(xxxx)
                                   userInfo:yourView repeats:NO];

Then in your selector retrieve the view from the userInfo and hide it. You can find a working sample here, line 37.

Upvotes: 1

Related Questions