这比较早,但现在不是因为它无法识别下面第一个.m文件中的设置。我做了#import Setting.h但它仍然不起作用。请帮忙!
这是.h文件:
#import <UIKit/UIKit.h>
#import "EditNameViewController.h"
@interface SettingsViewController : UIViewController <EditNameDelegate>{
IBOutlet UIButton *froshsched;
IBOutlet UIButton *uppersched;
}
.m文件。它说设置和'设置未声明
#import "SettingsViewController.h"
#import "Settings.h"
@interface SettingsViewController ()
@end
@implementation SettingsViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(@"Settings", @"Settings");
self.tabBarItem.image = [UIImage imageNamed:@"spanner"];
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
-(IBAction)froshsched{
Settings *settings = [Settings sharedInstance];
settings.timelabelfirst = @"12:35";
settings.timelabelsecond = @"1:25";
[[Settings sharedInstance] save];
}
-(IBAction)uppersched{
Settings *settings = [Settings sharedInstance];
settings.timelabelfirst = @"12:10";
settings.timelabelsecond = @"1:00";
[[Settings sharedInstance] save];
}
-(IBAction)editclass{
EditNameViewController*vc2 = [[EditNameViewController alloc] init];
vc2.delegate = self;
[self presentModalViewController:vc2 animated:YES];
}
-(void)dismiss{
[self dismissModalViewControllerAnimated:YES];
}
@end
-(IBAction)editclass;
-(IBAction)froshsched;
-(IBAction)uppersched;
这是Settings.h
#import <Foundation/Foundation.h>
@interface Settings : NSObject
@property (nonatomic) NSString *redClassName, *orangeClassName, *yellowClassName, *greenClassName, *ltblueClassName, *dkblueClassName, *purpleClassName, *pinkClassName, *timelabelfirst, *timelabelsecond;
+(Settings*)sharedInstance;
-(void)save;
@end
最后这里是Settings.m文件,我觉得我遗漏了一些简单的东西,谢谢
#import "Settings.h"
@implementation Settings
@synthesize redClassName, orangeClassName, yellowClassName, greenClassName, ltblueClassName, dkblueClassName, purpleClassName, pinkClassName, timelabelfirst, timelabelsecond;
-(id)init{
self = [super init];
if(self){
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
redClassName = [defaults objectForKey:@"kredClass"];
orangeClassName = [defaults objectForKey:@"korangeClass"];
yellowClassName = [defaults objectForKey:@"kyellowClass"];
greenClassName = [defaults objectForKey:@"kgreenClass"];
ltblueClassName = [defaults objectForKey:@"kltblueClass"];
dkblueClassName = [defaults objectForKey:@"kdkblueClass"];
purpleClassName = [defaults objectForKey:@"kpurpleClass"];
pinkClassName = [defaults objectForKey:@"kpinkClass"];
timelabelfirst = [defaults objectForKey:@"ktime1"];
timelabelsecond = [defaults objectForKey:@"ktime2"];
}
return self;
}
+(Settings*)sharedInstance{
static Settings *sharedSettings;
if(!sharedSettings) sharedSettings = [[Settings alloc] init];
return sharedSettings;
}
-(void)save{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:redClassName forKey:@"kredClass"];
[defaults setObject:orangeClassName forKey:@"korangeClass"];
[defaults setObject:yellowClassName forKey:@"kyellowClass"];
[defaults setObject:greenClassName forKey:@"kgreenClass"];
[defaults setObject:ltblueClassName forKey:@"kltblueClass"];
[defaults setObject:dkblueClassName forKey:@"kdkblueClass"];
[defaults setObject:purpleClassName forKey:@"kpurpleClass"];
[defaults setObject:pinkClassName forKey:@"kpinkClass"];
[defaults setObject:timelabelfirst forKey:@"ktime1"];
[defaults setObject:timelabelsecond forKey:@"ktime2"];
[defaults synchronize];
}
@end
答案 0 :(得分:0)
您没有给我们EditNameController(及其委托)。无论如何,我复制并粘贴了上面的所有代码,并通过注释掉EditNameController相关的项目,它们编译得很好(当然这些行浮动在文件的底部:
-(IBAction)editclass;
-(IBAction)froshsched;
-(IBAction)uppersched;
您还没有在SettingsViewController.h的底部显示“@end”(我在编译之前添加了它)。