Bellow我有一个隐藏按钮的委托系统,然后保存在NSUserDefault
中隐藏按钮的位置。但是由于某些原因,任何人都可以帮忙吗?
这里我有levelComplete代码...... .H
#import <UIKit/UIKit.h>
#import "levelComplete.h"
#import "LevelSelector.h"
@interface levelComplete : UIViewController{
}
@property (nonatomic, strong) id<CustomDelegate> delegatePpty;
@end
的.m
@implementation levelComplete
@synthesize delegatePpty;
-(void)someAction
{
[self.delegatePpty hideUnhidebutton:YES];//Call the delegate method to execute
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self someAction]; // Here I call my action
}
@end
这里有leveSelector代码......
·H
@protocol CustomDelegate <NSObject>
-(void)hideUnhidebutton:(BOOL)value;
@end
#import <UIKit/UIKit.h>
#import "levelComplete.h"
#import "LevelSelector.h"
@interface LevelSelector : UIViewController <CustomDelegate>{
}
@property (nonatomic, strong) UIButton *level1;
@end
的.m
@implementation LevelSelector
@synthesize level1;
-(void)hideUnhidebutton:(BOOL)value
{
[self.level1 setHidden:value];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSNumber numberWithBool:value] forKey:@"YourVariableKey"];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL restoredBoolValue = [[defaults objectForKey:@"YourVariableKey"] boolValue];
}
@end
提前致谢!!!
修改
Thanks for all of your help I have now got the code to correctly function
答案 0 :(得分:2)
我认为你在代表团遗漏了一些东西,一旦看到这个
delegateclass.h
#import <UIKit/UIKit.h>
@protocol CustomDelegate <NSObject>
-(void)hideUnhidebutton:(BOOL)value;
@end
@interface delegateclass : UIViewController <CustomDelegate>{
id<CustomDelegate> delegate;
}
@property(retain) id delegate;
-(void)someMethod;
@end
delegateclass.m
#import "delegateclass.h"
@implementation delegateclass
@synthesize delegate;
-(void)someMethod {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL restoredBoolValue = [[defaults objectForKey:@"YourVariableKey"] boolValue];
[[self delegate] hideUnhidebutton:restoredBoolValue];
}
@end
TestViewController.h
#import <UIKit/UIKit.h>
#import "delegateclass.h"
@interface TestViewController : UIViewController{
delegateclass *delegateclassObj;
}
@property (nonatomic, strong) id<CustomDelegate> delegatePpty;
@property (nonatomic, strong) UIButton *level1;
@end
TestViewController.m
#import "TestViewController.h"
@implementation TestViewController
@synthesize delegatePpty,level1;
- (void)viewDidLoad
{
[super viewDidLoad];
delegateclassObj = [[delegateclass alloc]init];
[delegateclassObj setDelegate:self];
[delegateclassObj someMethod];
}
//Implementaion for Delegatemethod......
-(void)hideUnhidebutton:(BOOL)value
{
[self.level1 setHidden:value];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSNumber numberWithBool:value] forKey:@"YourVariableKey"];
}
@end
抱歉,我确切知道你的主题是什么,但是这里的delegete方法(hideUnhidebutton :)必须要调用。
希望它对你有帮助..
答案 1 :(得分:1)
要检查的事项
创建levelComplete的实例时
[levelCompleteInstance setDelegatePpty:self];
同步NSUserDefaults
- (BOOL)synchronize