警报中游戏点的社交共享(Xcode)

时间:2014-05-12 12:15:16

标签: ios nstimer social

我刚开始编写应用程序等等。所以我的第一个应用程序是一个带有计时器功能的“简单”游戏。我的问题是这样的:我想给用户一个功能,他可以在Twitter和Facebook上分享他的积分,但游戏点的输出总是0 ..我想解决这个问题并需要你的帮助。< br />

截图

http://techstern.de/app/screenshot1.png
http://techstern.de/app/screenshot3.png


*对不起,我来自德国,所以这里是翻译:

“KlickMich” - &gt; “TapMe”
“Zeit” - &gt; “时代”
“Punktestand” - &gt; “点”
“Die Zeit ist um” - &gt; “时间到了”
“Du hast 11 Punkte erzielt” - &gt; “你得了11分” “Auf Facebook teilen” - &gt; “在Facebook上分享”
“Auf Twitter teilen” - &gt; “在Twitter上分享”
“Nochmal spielen” - &gt; “再玩一次”
“Habe gerade 0 Punkte in KlickMich erreicht ......” - &gt; “只是在TapMe中得了0分......”


期待您的反应:)

忠实的你 罗宾


代码:ViewController.h

#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


代码:ViewController.m

#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

2 个答案:

答案 0 :(得分:1)

我查看了您的代码。您首先重置计时器,然后发送得分以在Twitter上分享。

首先分享Twitter,然后你必须像这样调用setupGame方法:

 - (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
 }

并将此方法称为-(void)postToTwitter,而不是- (IBAction)postToTwitter。因为没有与该方法的连接。你正在调用该方法而不是用按钮动作连接。

答案 1 :(得分:0)

这是你要求的答案。请仔细阅读:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
  if (buttonIndex == 0)
  {
    [self setupGame];
    NSLog(@"Cancel..");
  }
  else if (buttonIndex == 1)
  {
    NSLog(@"Facebook..");
    [self postToFacebook];
  }
  else if (buttonIndex == 2)
  {
    NSLog(@"Twitter..");
    [self postToTwitter];
  }
}
- (void)postToTwitter
{
   SLComposeViewController *tweetSheet=[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
//     if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
//     {
     SLComposeViewControllerCompletionHandler __block    completionHandler=^(SLComposeViewControllerResult result){

      [tweetSheet dismissViewControllerAnimated:YES completion:nil];

        switch(result){
            case SLComposeViewControllerResultCancelled:
            default:
            {
                NSLog(@"Cancel");
                [self setupGame];
            }
                break;
            case SLComposeViewControllerResultDone:
            {
                NSLog(@"Post");
                [self setupGame];
            }
                break;
        }
    };

    [tweetSheet setInitialText:[NSString stringWithFormat:@"Habe gerade %li Punkte in KlickMich erreicht...",(long)count]];
    [tweetSheet setCompletionHandler:completionHandler];
    [self presentViewController:tweetSheet animated:YES completion:nil];
  //}

 }