我正在做一个简单的项目来证明我的 iOS 编程技能。我正在尝试做一个程序,您可以在其中输入一个字符串,单击提交,它会在下面创建一个带有您输入的文本的新标签。您提交的字符串越多,彼此下方的标签就越多。我第一次提交后,没有任何反应。如果我第二次提交字符串,我会收到 BAD_EXEC 错误。你能帮帮我吗?
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize infoLabel;
@synthesize field;
@synthesize saveButton;
NSString *labelString = @""; // this string will hold all of my strings together
NSString *separator = @"|<->|"; // this is the separator that will separate strings in labelString
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
[infoLabel release];
[field release];
[saveButton release];
[super dealloc];
}
- (IBAction)saveButton:(id)sender // when submit button is clicked
{
if(!([field.text isEqualToString:@""])) // check if anything was entered as the string
{
if([labelString isEqualToString:@""]) labelString = field.text; // if nothing is in stored in strings, write the current input
else // if there already are some strings in there
{
NSString *temp = @"";
temp = [NSString stringWithFormat:@"%@%@%@", labelString, separator, field.text]; // prepare a new labelString to hold all my old strings + new one
labelString = temp; // replace prepared string with old one
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; // saving to database
[defaults setObject:labelString forKey:@"saveString"];
NSArray *labelText = [labelString componentsSeparatedByString:separator]; // create array of separated strings
UILabel *label[[labelText count]]; // create array of labels
for(int i = 0; i < [labelText count]; i++) // cycle through all labels
{
label[i] = [[UILabel alloc] initWithFrame:CGRectMake(0, i * 20 + 20, 320, 20)]; // create a label for each string that will be on a certain position (descending)
label[i].text = labelText[i]; // set text for labol
}
}
}
@end