Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-28-2012, 05:18 PM   #1 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 130
rebello95 is on a distinguished road
Default Timer for Multiple XIBs?

Hey, I'm making a game that goes across a lot of interface files. I'm working on coding it so that there's a timer counting across all of them. If this code is just completely terrible, please let me know lol. Here's what I have:

GameTimerViewController.h

@interface GameTimerViewController : UIViewController {

int time;
NSTimer *maintimer;

IBOutlet UILabel *mylabel;

}

- (IBAction)newgame;
- (void)updatetime;

-(IBAction)save;

-(IBAction)switchPageid)sender;

@end

GameTimerViewController.m

#import "GameTimerViewController.h"
#import "Page2.h"

@implementation GameTimerViewController

- (void) awakeFromNib {

time = 0;

}

- (IBAction)newgame {
maintimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updatetime) userInfo:nil repeats:YES];

}

- (void) updatetime {

time = time +1;
if (time <=0) {

[maintimer invalidate];
//game over

}

mylabel.text = [NSString stringWithFormat:@"%i" , time];
}

-(IBAction)save {

NSString *savestring = mylabel.text;

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

[defaults setObject:savestring forKey:@"savedstring"];

[defaults synchronize];

[maintimer invalidate];

}

-(IBAction)switchPageid)sender {

Page2 *p2 =[[Page2 alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController2 animated:YES];

}

Page2.h

@interface Page2 : UIViewController {

int time;
NSTimer *maintimer;
IBOutlet UILabel *label2;

}

-(IBAction)load;

@end

Page2.m

@implementation Page2

-(IBAction)load {

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSString *loadstring = [defaults objectForKey:@"savedstring"];

[label2 setText:loadstring];

maintimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updatetime) userInfo:nil repeats:YES];
}

- (void) updatetime {

time = time +1;
if (time <=0) {

[maintimer invalidate];
//game over

}

label2.text = [NSString stringWithFormat:@"%i" , time];

}
rebello95 is offline   Reply With Quote
Reply

Bookmarks

Tags
nstimer, timer, timer multiple xib, xcode

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 386
8 members and 378 guests
apatsufas, comicool, dansparrow, husthlj, LunarMoon, mer10, Murphy, pbart
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,677
Threads: 94,127
Posts: 402,916
Top Poster: BrianSlick (7,990)
Welcome to our newest member, husthlj
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:06 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0