objective-c - iOS 获取用户输入并将其存储为全局变量

标签 objective-c ios xcode

<分区>

我正在尝试制作一个测试应用程序,当用户单击一个按钮时,文本会切换到其他内容,如果再次单击它,它会返回,但我也希望能够添加一个部分,用户可以在其中输入他/她的名字,所以它说,隐藏的信息是:或者其他什么...

我的代码是:

- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//button.backgroundColor = [UIColor redColor];
button.titleLabel.textColor=[UIColor blackColor];
button.frame = CGRectMake(25, 100, 275, 60);
[button setTitle:@"Press this button to reveal the text!" forState:UIControlStateNormal];
[button addTarget:self action:@selector(button_method:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)button_method:(UIButton *)button {
NSString *test = @"I am learning Objective-C for the very first time! Also, this is my first ever variable!";
// handle button press
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(25, 25, 275, 60)];
label.text = test;
label.numberOfLines = 0;
label.lineBreakMode = UILineBreakModeWordWrap;
//label.lineBreakMode = NSLineBreakByWordWrapping; //iOS 6 only
[self.view addSubview:label];
[button setTitle:@"You have pressed the button!" forState:UIControlStateNormal];
[button addTarget:self action:@selector(change_again:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)change_again:(UIButton *)button {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(25, 25, 275, 60)];
label.text = @"You have found the next piece of text!";
label.numberOfLines = 0;
label.lineBreakMode = UILineBreakModeWordWrap;
//label.lineBreakMode = NSLineBreakByWordWrapping; //iOS 6 only
[self.view addSubview:label];
[button setTitle:@"Keep pressing!" forState:UIControlStateNormal];
[button addTarget:self action:@selector(button_method:) forControlEvents:UIControlEventTouchUpInside];

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

我希望这不会被认为过于本地化!

木克曼

PS:一个附带的问题,我将如何创建一个字符串,例如 NSString *var4 = var.var2.var3;

什么是连接字符?在 JavaScript 中是 + 在 PHP 中是 .

最佳答案

您可能想使用@property 来存储文本输入。属性类似于实例变量,但它们通过添加每当检索 (get) 或分配 (set) ivar 时检索的 getter 和 setter 来引入另一层抽象。目前你所有的变量只存在于你的方法中。属性将允许从不同的方法访问这些变量。这是速成类:

属性在 @interface 部分声明(使用头文件获取可从其他类访问的属性,使用主文件获取只能从您的类访问的属性)。

    @property (strong, nonatomic) NSString *userInput;

@implementation 部分必须编写您自己的 getter 和 setter,或者您可以使用 synthesize 自动创建您的 getter 和 setter:

    @synthesize userInput = _userInput;

从这里您可以使用该属性。

    //Assigning the property
    self.userInput = @"Some text";

    //Retrieving the property
    label.text = self.userInput;

如果您想了解有关属性的所有信息,请查看 Apple 的 documentation .

关于属性的最后一件事:因为它们是实例变量,所以它们与类的实例相关联,因此它们不会在程序启动之间持续存在。

对于文本输入,请尝试 UITextField。

如果您刚刚开始 iOS 编程,我建议您 Stanford's CS193p on iTunes U .我很确定他在第 2 课中编写计算器应用程序时使用了自定义 getter 并详细说明了您想要编写一个的原因。

关于objective-c - iOS 获取用户输入并将其存储为全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13576326/

上一篇:iphone - 在 ios 中的应用内购买

下一篇:ios - 使用 ActivityViewController 的 iOS 6 上的 Twitter 预览表以英文显示而不是语言设备

相关文章:

ios - CGContextShowTextAtPoint 即使在不同的迭代中也有固定的大小?

ios - 在 UICollectionViewController 中按下 UICollectionViewCell 后无响应

ios - 使用 for 循环创建按钮? swift

swift - 真实设备上的 SKTileMap 物理错误

iphone - 这个崩溃报告是什么意思?

ios - 无法在 Xcode 10.2 中使用 Cocoapods 分解父类(super class)

ios - 无法访问 Swift 子类中的 oc 类别方法

ios - UiCollcetionViewCell 特定的 segue 连接 TableView 显示 TableView 内的特定内容

ios - 通过 IndexPath 从 CollectionViewCell 转到另一个 View Controller

ios - 128 位 AES 加密在 Objective-C 中返回 null