• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何在 Objective-C 的不同 View 中共享本地变量

[复制链接]
菜鸟教程小白 发表于 2022-12-12 20:57:59 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap