0

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;

Almo
  • 15,538
  • 13
  • 67
  • 95
Madhubalan K
  • 357
  • 5
  • 21

1 Answers1

1

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.

andreamazz
  • 4,256
  • 1
  • 20
  • 36