我正在做一個簡單的項目來證明我的iOS編程技巧。我正在嘗試執行一個程序,在該程序中輸入一個字符串,單擊「提交」,它將在下面用您輸入的文本創建一個新標籤。您提交的字符串越多,您在對方下的標籤就越多。第一次提交後,沒有任何反應。如果我第二次提交一個字符串,我得到一個BAD_EXEC錯誤。你能幫我解決嗎?簡單的數據庫(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
它在哪裏崩潰?至少發佈堆棧跟蹤。還要注意,'NSUserDefaults'不是一個數據庫,真的不應該用於存儲應用程序的多於配置狀態;上次打開的位置,用戶選項等等。 – bbum
https://developer.apple.com/library/ios/referencelibrary/GettingStarted/RoadMapiOS/chapters/Introduction.html –
http://i.imgur.com/AaMUBjD.png – Sam