gpt4 book ai didi

iphone - 如何使用全局/静态变量 OBJ-C

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:04:40 24 4
gpt4 key购买 nike

我希望保存用于一种方法的变量,然后在应用程序的另一种方法中调用它。这与全局/外部/静态变量有关吗?如果是这样,我想知道它将如何设置。我尝试使用全局和静态但没有成功。

我正在尝试保存 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];
}
}

谢谢。

最佳答案

按照下面的操作

声明属性

@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];
}
}

关于iphone - 如何使用全局/静态变量 OBJ-C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15666079/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com