MapMapM 2013-03-28 02:16 采纳率: 0%
浏览 5303
已采纳

在objective-c中使用全局/静态变量

保存用作方法的变量,然后在另一个方法中调用。
实现时会不会用到全局/外部/静态变量?如果需要应该怎么用?

我试过用全局和静态,但是都失败了。

代码中用来保存newX 和 newY 的信息。

-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
...
    int newX = (int)(Button.center.x + valueX);
    int newY = (int)(Button.center.y + valueY);
...
}

然后在这里调用:

-(IBAction)clicked:(id)sender

{
    randX = arc4random() % 320;
    randY = arc4random() % 548;

    CGPoint randNewPlace = CGPointMake(randX, randY);
    Rand.center = randNewPlace;
    if (newX == randX || newY  == randY)
    {
        [Rand sendActionsForControlEvents:UIControlEventTouchUpInside];
    }
}
  • 写回答

2条回答 默认 最新

  • 哈希Map 2013-03-28 02:36
    关注

    你是做Android转iOS的么?在Objc中没有Java里全局变量的这个概念,只能够通过单例实现类似的效果。
    创建一个GloubVariables对象保存需要保存的内容:

    //////////////////////////////////////////////////////////////////////////
    GloubVariables.h
    @interface GloubVariables : NSObject
    {
        int newX;
        int newY;
    }
    
    @property(assin,nonatomic) int newX;
    @property(assin,nonatomic) int newY;
    +(GloubVariables *)sharedInstance;
    
    @end
    //////////////////////////////////////////////////////////////////////////
    GloubVariables.m
    #import "GloubVariables.h"
    @implementation GloubVariables
    @synthesize newX;
    @synthesize newY;
    
    static GloubVariables *instance_;
    +(GloubVariables *)sharedInstance
    {
        @synchronized(self)
        {
            if(instance_ == nil)
            {
                instance_ = [[GloubVariables alloc] init];
            }
        }
        return instance_;
    }
    @end
    //////////////////////////////////////////////////////////////////////////
    

    然后需要用到的地方:

    [GloubVariables sharedInstance].newX
    [GloubVariables sharedInstance].newY
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • gaoXxxing 2013-03-28 05:51
    关注

    这样
    声明属性:

    @property(nonatomic,weak) int newX;
    @property(nonatomic,weak) int newY;
    
    -(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
    {
    ...
        self.newX = (int)(Button.center.x + valueX);
        self.newY = (int)(Button.center.y + valueY);
    ...
    
    }
    
    -(IBAction)clicked:(id)sender
    
    {
       randX = arc4random() % 320;
       randY = arc4random() % 548;
    
       CGPoint randNewPlace = CGPointMake(randX, randY);
       Rand.center = randNewPlace;
    
       if (self.newX == randX || self.newY  == randY)
       {
          [Rand sendActionsForControlEvents:UIControlEventTouchUpInside];
       }
    

    }

    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Qt 不小心删除了自带的类,该怎么办
  • ¥15 我需要在PC端 开两个抖店工作台客户端.(语言-java)
  • ¥15 有没有哪位厉害的人可以用C#可视化呀
  • ¥15 可以帮我看看代码哪里错了吗
  • ¥15 设计一个成绩管理系统
  • ¥15 PCL注册的选点等函数如何取消注册
  • ¥15 问一下各位,为什么我用蓝牙直接发送模拟输入的数据,接收端显示乱码呢,米思齐软件上usb串口显示正常的字符串呢?
  • ¥15 Python爬虫程序
  • ¥15 crypto 这种的应该怎么找flag?
  • ¥15 代码已写好,求帮我指出错误,有偿!