OGeek|极客世界-中国程序员成长平台

标题: ios - 如何在 Objective-C 的不同 View 中共享本地变量 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 20:57
标题: ios - 如何在 Objective-C 的不同 View 中共享本地变量

我现在正在做休闲游戏。
我想在推特上分享分数。
我的这个作品图片如下。

1.玩家玩游戏后有分数。
2.当游戏进入gameoverscene时,他们可以推推特按钮(使用social.framework)
3. 有“你得了xx分!!”这样的文字在推特显示(模态)中。
*我想把 xx 改成共享分数。

你能给我一些建议吗? 我是Objective-C的初学者。所以,简单的方法对我来说更好。 渐渐地,我想关注安全性和可扩展性。

[信息] *MainScene 和 GameOverScene 由 SKScene 制作

MainScene.m

@implementation MainScene {

//The score
NSInteger _score;

}

- (void)incrementScore
{
_score++;
}

GameOverScene.m

- (void)touchesBeganNSSet *)touches withEventUIEvent *)event
{

 if([node.name isEqualToString"twitterbutton"]){
    NSLog(@"self.delegate = %@",self.delegate);
    [self.delegate showShareScreen];

    //delegate to ViewController
    if (nil == self.delegate) NSLog(@"delegate is nil");
    }    
}

ViewController.m

-(void)showShareScreen
{
NSLog(@"showShareScreen");
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
    SLComposeViewController *tweetSheet = [SLComposeViewController

composeViewControllerForServiceType:SLServiceTypeTwitter];

    tweetSheet.modalPresentationStyle = UIModalPresentationFormSheet;
    tweetSheet.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

    [tweetSheet setInitialText"You got xx score"];
     NSLog(@"self = %@",self);
    [self presentViewController:tweetSheet animated:YES completion:nil];

 }
 else {
     NSLog(@"not sls type twitter");
 }
 }



Best Answer-推荐答案


在 ViewController 的委托(delegate)方法中更改您的行

[tweetSheet setInitialText"You got xx score"];

如下图;将分数包含在 NSString 的 stringWithFormat: 方法中

-(void)showShareScreenWithScoreNSInteger) score {
NSLog(@"showShareScreen");
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
    SLComposeViewController *tweetSheet = [SLComposeViewController

composeViewControllerForServiceType:SLServiceTypeTwitter];

    tweetSheet.modalPresentationStyle = UIModalPresentationFormSheet;
    tweetSheet.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

    [tweetSheet setInitialText:[NSString stringWithFormat"You got %d score",score]];
     NSLog(@"self = %@",self);
    [self presentViewController:tweetSheet animated:YES completion:nil];

 }
 else {
     NSLog(@"not sls type twitter");
 }
}

并从 GameOverScene 调用此委托(delegate)方法。 注意:当您在对象实例化之后创建游戏结束场景时,您可以设置 GameOverScene 的属性。所以只需声明一个属性

@property (assign , nonatomic) NSInteger score;

并设置它的值Using NSURLSession游戏快结束了。您可以从 MainScene score 属性中获取分数值。


NSURLSession

The NSURLSession class and related classes provide an API for downloading content via HTTP. This API provides a rich set of delegate methods for supporting authentication and gives your app the ability to perform background downloads when your app is not running or, in iOS, while your app is suspended.

关于ios - 如何在 Objective-C 的不同 View 中共享本地变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22728282/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4