1

我是 XCode 编码的新手,我尝试了很多来保存信息,但没有成功。我用谷歌搜索,但我仍然没有找到任何东西。

我采用了一个名为 classNames 的可变数组,并尝试从文本字段 (classField) 中保存信息,但它没有存储在数组中。

我还有另一个问题:我正在使用具有 iOS 5.1 的 XCode,并且我升级到具有 iOS 6.1 的 Xcode,但它给了我一个错误,说“连接中断”并且没有任何按钮或功能起作用,但它适用于具有 iOS 的 XCode 5.1 我需要帮助,谢谢。

#import "ViewController.h"

    @interface ViewController ()

    @end

    @implementation ViewController
    @synthesize orignalClass;
    @synthesize otherClassName;
    @synthesize editClassLabel;
    @synthesize displayLabel;
    @synthesize doSomethingButton;
    @synthesize classField;
    @synthesize testField;
    @synthesize classFieldFireTheme;
    @synthesize testFieldFireTheme;

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        _classNames = [[NSMutableArray alloc] init];
        _classNames = [defaults objectForKey:@"classNames"];
        //NSLog(@" %@", [defaults objectForKey:@"classNames"]);


    }


- (void)viewDidUnload
{
    [self setDoSomethingButton:nil];
    [self setClassField:nil];
    [self setTestField:nil];
    [self setClassFieldFireTheme:nil];
    [self setTestFieldFireTheme:nil];
    [self setDisplayLabel:nil];
    [self setOrignalClass:nil];
    [self setEditClassLabel:nil];
    [self setOtherClassName:nil];
    [self setTestClass:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (IBAction)classtextFieldDoneEdtiting:(id)sender {
    [sender resignFirstResponder];
}
- (IBAction)testClassTextFieldDoneEdtiting:(id)sender {
    [sender resignFirstResponder];
}
- (IBAction)TestTextFieldDoneEdtiting:(id)sender {
    [sender resignFirstResponder];
}
- (IBAction)ChangeClassTextFieldDoneEdtiting:(id)sender {
    [sender resignFirstResponder];
}
- (IBAction)ChangeClassToTextFieldDoneEdtiting:(id)sender {
    [sender resignFirstResponder];
}

- (IBAction)addClass:(id)sender {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    _classNames = [defaults objectForKey:@"classNames"];
    _classSize = [_classNames count];
    NSLog(@" %d", [_classNames count]);

    if(_classSize <=9){

        [_classNames insertObject: classField.text atIndex: [_classNames count]];
        NSLog(@"ClassNames#2::: %@", _classNames);
        NSLog(@"textfieldvalue::: %@", classField.text);
        [defaults setObject:_classNames forKey:@"classNames"];
        [[NSUserDefaults standardUserDefaults] synchronize];

        NSLog(@" %d", [_classNames count]);
        NSLog(@"ClassNames#3::: %@", _classNames);

   }else{
        displayLabel.text = @"You exceeded the limit of classes being stores!!!!";
   }

}
4

2 回答 2

4

The problem is that when an array comes out of NSUserDefaults it is not a mutable array. Even if what you put in was an NSMutableArray, what comes out is an ordinary NSArray. You need to use mutableCopy on that array to make it mutable. Now you can store into it, and then put it back into NSUserDefaults.

于 2013-04-10T15:54:59.523 回答
1

此代码无法工作。您在方法中声明defaultsaddClass:然后尝试从viewDidLoad方法中访问它,这是您无法做到的。viewDidLoad在like的顶部添加一行

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

如果您需要更多帮助,我们需要有关您遇到的错误的更多信息。

于 2013-04-10T15:53:32.490 回答