0

I am new in iPhone app. I want to show a mesage at each 2 seconds when a button is pressed For that I am using this code.

This code is work only one time. That means call only one time. Can you help me to solve this problem.

-(IBAction)fortunecookieAction:(id)sender
{
    [self performSelector:@selector(showfortune) withObject:nil afterDelay:2.0];
}

-(void)showfortune
{
    int number=arc4random()%5;
    switch (number) {
        case 0:
            fortunelabel.text=@"A holiday takes you back to the summer of '69";
            break;
        case 1:
            fortunelabel.text=@"A meal turns erotic muffin";
            break;
        case 2:
            fortunelabel.text=@"A massage brings";
            break;
        case 3:
            fortunelabel.text=@"A letter in the pa special delivery";
            break;
        case 4:
            fortunelabel.text=@"A spillage tuoo";
            break;

        default:
            break;  
    }   
}
Stuart
  • 36,683
  • 19
  • 101
  • 139
sai
  • 479
  • 10
  • 30

2 Answers2

0

if you want to call that function every 2 seconds use a NSTimer.
You will need scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: for creating a scheduled timer

Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
thomas
  • 5,637
  • 2
  • 24
  • 35
0

Your best bet is probably to use an NSTimer. You can set one up to call an action method every two seconds really easily:

In your interface (MyViewController.h), declare an NSTimer property:

@interface MyViewController : UIViewController

@property (nonatomic, retain) NSTimer *myTimer;

@end


And then in your implementation:

@synthesize myTimer;

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSTimer *newTimer = [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(repeatingTimerFired:) userInfo:nil repeats:YES];
    self.myTimer = newTimer;
    [newTimer release];
}

- (void)viewDidUnload
{
    [super viewDidUnload];

    if ([myTimer isValid]) {
        [myTimer invalidate];
    }    
}

- (void)dealloc
{
    [myTimer release], myTimer = nil;

    [super dealloc];
}

- (void)repeatingTimerFired:(NSTimer *)sender
{
    int number=arc4random()%5;
    switch (number) {
        case 0:
            fortunelabel.text=@"A holiday takes you back to the summer of '69";
            break;
        case 1:
            fortunelabel.text=@"A meal turns erotic muffin";
            break;
        case 2:
            fortunelabel.text=@"A massage brings d";
            break;
        case 3:
            fortunelabel.text=@"A letter in the pa special delivery";
            break;
        case 4:
            fortunelabel.text=@"A spillage tuoo";
            break;

        default:
            break;
}

If you want this timer to start displaying messages after the user presses a button, simply move the timer creation into a button action method.

Stuart
  • 36,683
  • 19
  • 101
  • 139
  • @sai: If you want to invalidate it at a later time then you will need to hold onto a reference to the timer using a declared property. Make sure it's invalidated inside `viewDidUnload`, and release is in `dealloc`, otherwise the timer (and your view controller, as the target) won't be removed from the run loop. – Stuart Sep 15 '11 at 11:37
  • @sai: I have edited my answer to include details of how to do this (see sample code). – Stuart Sep 15 '11 at 11:44
  • Understood,But my app is custamaticaly craeted Tab bar based application .I din'nt know why viewDidUnload method is not calling...Thanks a lot – sai Sep 15 '11 at 11:58
  • @sai: Why does it matter that your app is tab-based? – Stuart Sep 15 '11 at 11:59
  • - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item { if([self.selectedViewController isKindOfClass:[HomeViewController class]]) { [self.selectedViewController viewWillDisappear:YES]; } if (item == home) { UIViewController *homeVC = [viewControllers objectAtIndex:0]; [self.selectedViewController.view removeFromSuperview]; [homeVC viewWillAppear:YES]; [self.view addSubview:homeVC.view]; [homeVC.view setFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height-49 )]; self.selectedViewController = homeVC; } – sai Sep 15 '11 at 12:03
  • Argh! That is pretty useless to me! If you need to post additional code, edit your original question. – Stuart Sep 15 '11 at 12:14