-2

iOS プログラミングのスキルを証明するための簡単なプロジェクトを行っています。文字列を入力して送信をクリックすると、入力したテキストの下に新しいラベルが作成されるプログラムを実行しようとしています。送信する文字列が多いほど、相互の下にあるラベルが多くなります。初めて送信した後、何も起こりません。2 回目に文字列を送信すると、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
4

1 に答える 1