I just started programming Apps and so on. So my first App is a "simple" Game with a timer function. My problem is this: I want to give the user a function with it he can share his points on Twitter and Facebook, but the output of the game points are always 0.. I want to fix this and need your help for this.
http://techstern.de/app/screenshot1.png
http://techstern.de/app/screenshot3.png
"KlickMich" -> "TapMe"
"Zeit" -> "Time"
"Punktestand" -> "Points"
"Die Zeit ist um" -> "Time is up"
"Du hast 11 Punkte erzielt" -> "You scored 11 Points"
"Auf Facebook teilen" -> "Share on Facebook"
"Auf Twitter teilen" -> "Share on Twitter"
"Nochmal spielen" -> "Play again"
"Habe gerade 0 Punkte in KlickMich erreicht..." -> "Just scored 0 points in TapMe..."!
Looking forward to your reactions :)
Yours faithfully
Robin
#import <UIKit/UIKit.h>
#import "ViewController.m"
@interface ViewController : UIViewController<UIAlertViewDelegate> {
IBOutlet UILabel *scoreLabel;
IBOutlet UILabel *timerLabel;
NSInteger count;
NSInteger seconds;
NSTimer *timer;
int time;
}
- (IBAction) buttonPressed;
- (void)setupGame;
- (void)subtractTime;
- (IBAction)postToTwitter;
- (IBAction)postToFacebook;
@end
#import "ViewController.h"
#import <UIKit/UIAlertView.h>
#import <Social/Social.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self setupGame];
}
- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)buttonPressed{
count++;
scoreLabel.text = [NSString stringWithFormat:@"Punktestand: %li",(long)count];
}
- (void)setupGame{
seconds = 15;
count = 0;
timerLabel.text = [NSString stringWithFormat:@"Zeit: %li",(long)seconds];
scoreLabel.text = [NSString stringWithFormat:@"Punktestand: %li",(long)count];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:@selector(subtractTime)
userInfo:nil
repeats:YES];
}
- (void)subtractTime{
seconds--;
timerLabel.text = [NSString stringWithFormat:@"Zeit: %li",(long)seconds];
if (seconds == 0) {
[timer invalidate];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Die Zeit ist um"
message:[NSString stringWithFormat:@"Du hast %li Punkte erzielt",(long)count]
delegate:self
cancelButtonTitle:@"Nochmal spielen"
otherButtonTitles:@"Auf Facebook teilen", @"Auf Twitter teilen", nil];
[alert show];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
[self setupGame];
if (buttonIndex == 1)
{
[self postToTwitter];
}
else if (buttonIndex == 2)
{
[self postToFacebook];
}
}
- (IBAction)postToTwitter{
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController
composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:[NSString stringWithFormat:@"Habe gerade %li Punkte in KlickMich erreicht...",(long)count]];
[self presentViewController:tweetSheet animated:YES completion:nil];
}
}
- (IBAction)postToFacebook{
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[controller setInitialText:[NSString stringWithFormat:@"Habe gerade %li Punkte in KlickMich erreicht...",(long)count]];
[self presentViewController:controller animated:YES completion:Nil];
}
}
@end
I review your code. You reset your timer first, then you are send score to share on Twitter
.
First share on Twitter
and then you have to call setupGame
method like this :
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1)
{
[self postToFacebook];
}
else if (buttonIndex == 2)
{
[self postToTwitter];
}
[self setupGame]; // after share you can reset
}
And call this method like -(void)postToTwitter
not like - (IBAction)postToTwitter
. because there is no connection to that method. you are calling that method not connect with a button action.