菜鸟教程小白 发表于 2022-12-12 20:57:59

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


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

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

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

<p>[信息]
*MainScene 和 GameOverScene 由 SKScene 制作</p>

<p>MainScene.m</p>

<pre><code>@implementation MainScene {

//The score
NSInteger _score;

}

- (void)incrementScore
{
_score++;
}
</code></pre>

<p>GameOverScene.m</p>

<pre><code>- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

if(){
    NSLog(@&#34;self.delegate = %@&#34;,self.delegate);
    ;

    //delegate to ViewController
    if (nil == self.delegate) NSLog(@&#34;delegate is nil&#34;);
    }   
}
</code></pre>

<p>ViewController.m</p>

<pre><code>-(void)showShareScreen
{
NSLog(@&#34;showShareScreen&#34;);
if ()
{
    SLComposeViewController *tweetSheet = [SLComposeViewController

composeViewControllerForServiceType:SLServiceTypeTwitter];

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

    ;
   NSLog(@&#34;self = %@&#34;,self);
    ;

}
else {
   NSLog(@&#34;not sls type twitter&#34;);
}
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在 ViewController 的委托(delegate)方法中更改您的行</p>

<blockquote>
<p>;</p>
</blockquote>

<p>如下图;将分数包含在 NSString 的 <em>stringWithFormat:</em> 方法中</p>

<pre><code>-(void)showShareScreenWithScore:(NSInteger) score {
NSLog(@&#34;showShareScreen&#34;);
if ()
{
    SLComposeViewController *tweetSheet = [SLComposeViewController

composeViewControllerForServiceType:SLServiceTypeTwitter];

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

    ];
   NSLog(@&#34;self = %@&#34;,self);
    ;

}
else {
   NSLog(@&#34;not sls type twitter&#34;);
}
}
</code></pre>

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

<pre><code>@property (assign , nonatomic) NSInteger score;
</code></pre>

<p>并设置它的值<a href="https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/UsingNSURLSession.html" rel="noreferrer noopener nofollow">Using NSURLSession</a>游戏快结束了。您可以从 MainScene score 属性中获取分数值。</p>

<hr/>

<p><em>NSURLSession</em></p>

<blockquote>
<p>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.</p>
</blockquote></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在 Objective-C 的不同 View 中共享本地变量,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/22728282/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/22728282/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在 Objective-C 的不同 View 中共享本地变量