0

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

在这里,我有 levelSelector 代码...

。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 
4

2 回答 2

2

我认为你在代表团中遗漏了一些东西,一旦看到这个

委托类.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

委托类.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

测试视图控制器.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

测试视图控制器.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

对不起,我不知道你的主题是什么,但这里的 delegte 方法 (hideUnhidebutton:) 必须调用。

希望对你有帮助。。

于 2013-04-02T13:39:34.677 回答
1

检查事项

创建 levelComplete 实例时

[levelCompleteInstance setDelegatePpty:self];

同步 NSUserDefaults

- (BOOL)synchronize
于 2013-04-02T13:34:59.697 回答