我是XCode编码的新手,我尝试了很多来保存信息,但它没有成功。我用Google搜索,但我仍然没有找到任何东西。
我使用一个名为classNames的可变数组,并试图从文本字段(classField)中保存信息,但它不存储在数组中。
我有另一个问题:我正在使用具有iOS 5.1的XCode,并且我升级到具有iOS 6.1的Xcode,但是它给我一个错误,说“连接中断”并且没有任何按钮或功能工作,但它适用于具有iOS 5.1的XCode 我需要帮助,谢谢。
#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!!!!";
}
}
答案 0 :(得分:4)
问题是,当一个数组来自NSUserDefaults时,它不是一个可变数组。即使你输入的是一个NSMutableArray,出来的是一个普通的NSArray。您需要在该数组上使用mutableCopy
以使其可变。现在你可以存储它,然后把它放回NSUserDefaults。
答案 1 :(得分:1)
此代码无法使用。您在defaults
方法中声明addClass:
,然后尝试从viewDidLoad
方法访问它,这是您无法做到的。在viewDidLoad
的顶部添加一行,如
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
如果您需要更多帮助,我们需要您提供有关您所遇到错误的更多信息。