Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在方法中使用变量_Ios_Objective C_Xcode_Variables - Fatal编程技术网

Ios 在方法中使用变量

Ios 在方法中使用变量,ios,objective-c,xcode,variables,Ios,Objective C,Xcode,Variables,再说一次,我对目标c和Xcode还是新手。我正在构建一个小应用程序,需要在方法中使用变量。我不知道把它放在哪里,这样我可以在方法中使用它 我有一个启动整个过程的按钮,但是这个按钮的方法需要一个变量,这个变量只应该创建一次(因为它是一个随机数),然后保存,这样“按钮方法”就可以使用它来比较它。我应该把它放在哪里,这样我的变量在我的方法可以使用它时保持不变 Thx 像这样在.h文件中创建 #import <UIKit/UIKit.h> @interface GoogleMapsViewC

再说一次,我对目标c和Xcode还是新手。我正在构建一个小应用程序,需要在方法中使用变量。我不知道把它放在哪里,这样我可以在方法中使用它

我有一个启动整个过程的按钮,但是这个按钮的方法需要一个变量,这个变量只应该创建一次(因为它是一个随机数),然后保存,这样“按钮方法”就可以使用它来比较它。我应该把它放在哪里,这样我的变量在我的方法可以使用它时保持不变

Thx


像这样在.h文件中创建

#import <UIKit/UIKit.h>
@interface GoogleMapsViewController : UIViewController
@property int answer;//If it's int. Here mention your variable type(If it string @Property NSString * answer;)
@end

建议在实现类中存储私有变量。我们可以像这样在.m文件中定义变量

@interface ViewController (){
    NSString *stringTodefineAge;
}

@property (readonly, strong, nonatomic) ModelController *modelController;
@end

@implementation RootViewController
....

您可以使用您的方法所属的类实例的变量。请显示您的代码,而不是一般性代码。这是我的代码:-(iAction)guessButton:(id)发件人{NSLog(@“Answer=%i”,Answer);NSLog(@“Button Pressed”);float guess=[[self.guessTextField text]floatValue];NSLog(@“guess=%f”,guess);}这个方法应该得到这个变量->答案=arc4random()%100+1;只需在.h文件中创建变量或在viewDidLoad函数上方创建。您可以调用self.variablename之类的变量。请不要在注释中添加信息。你的问题。Xcode告诉我->类型名称需要一个说明符或限定符,一切都解决了!!!非常感谢。现在唯一的问题是Xcode希望我建立一个开发团队。它不会让我编译没有,你知道为什么吗?你需要点击左边顶部的文件名。这是您的项目名称(只需转到目标)。通常,您需要选择您的开发人员帐户,请参见“签名”和“选择团队”中的链接。
- (IBAction)guessButton:(id)sender { 
    NSLog(@"Answer = %i", self.answer); 
    NSLog(@"Button Pressed"); 
    float guess = [[self.guessTextField text] floatValue]; 
    NSLog(@"Guess = %f", guess); 
}
@interface ViewController (){
    NSString *stringTodefineAge;
}

@property (readonly, strong, nonatomic) ModelController *modelController;
@end

@implementation RootViewController
....