iOS接受用户输入并将其存储为全局变量

时间:2012-11-27 02:01:08

标签: 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.
}

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

muqman

PS:附带问题,如何创建NSString *var4 = var.var2.var3;

等字符串

加入角色是什么?在JavaScript中它是+在PHP中.

3 个答案:

答案 0 :(得分:3)

您可能希望使用@property来存储文本输入。属性就像实例变量,但它们通过添加在检索(获取)或分配(设置)ivars时检索的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。我很确定他使用了一个自定义的getter,并且在他在第二讲中写一个计算器应用程序时,你想要写一个原因。

答案 1 :(得分:1)

有两种使用全局变量的方法。首先,您可以在标头或实现方法的Interface部分中设置全局变量(仅在该类中以及当前会话期间可用)。这是一个例子:

//  ViewController.m
#import "ViewController.h"
@interface ViewController ()
{
    NSString *nameOfGlobalVariable;
}
@end

或者,在iOS中,您可以使用名为NSUserDefaults的存储系统存储默认值。 NSUserDefaults允许您随时,在任何类中以及在任何应用程序会话期间设置和访问默认值。 默认值存储在应用程序包中的PLIST文件中。以下是设置默认值的方法:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@"YourText" forKey:@"NameOfDefault"];
[defaults synchronize];

您还可以浏览NSUserDefaults方法并尝试更改setObject之类的setBool。检索默认值非常相似 - 只需定义NSUserDefaults,然后使用[objectForKey]方法检索它。我会让你把这个部分弄清楚,因为当你复制和粘贴时,编程并不好玩;)

祝你好运!

答案 2 :(得分:0)

Just try something like that:-

// In .h file, define your enum...
enum
{
MDefaultButtonText = 1,
MToggleButtonText
};

// In .m file, define your string if the text not changed...
#ifndef MDefaultButtonTextString
#define MDefaultButtonTextString "This is default string"
#endif

#ifndef MToggleButtonTextString
#define MToggleButtonTextString "This is toggled string"
#endif

/*
Please note, you can also used:-
const NSString *testStr = @"your required string";
// Feel free if you think to know more...
*/

Now, in viewDidLoad:-
[yourButton setTag:1];
[yourButton addTarget:self action:@selector(someProcedure:) forControlEvents:UIControlEventTouchUpInside];

Now, you do need of implementing your click event...
-(void)someProcedure:(id)sender
{
// Do your stuff here...

switch([sender tag])
{
case MDefaultButtonText://sender.tag = 1
     [sender setTag:2];
     [sender setTitle:MToggleButtonTextString forState:UIControlStateNormal];
     // In alert set Message to = MDefaultButtonTextString
     break;
case MToggleButtonText:////sender.tag = 2
     [sender setTag:1];
     [sender setTitle:MDefaultButtonTextString forState:UIControlStateNormal];
     // In alert set Message to = MToggleButtonTextString
     break;
default:
     //if you required...
     break;
}
}

Please cross check the required message accordingly to your requirement.Hope, it'll sort your issue.

Any concern, just get back to me. :)