gpt4 book ai didi

ios - Objective-C:委托(delegate)系统在 Xcode 中无法正常工作

转载 作者:行者123 更新时间:2023-11-28 20:21:26 31 4
gpt4 key购买 nike

我有一个委托(delegate)系统,它隐藏了一个按钮,然后将按钮隐藏或不隐藏的地方保存在 NSUserDefault 中。但是由于某种原因它不起作用任何人都可以帮忙吗?

这里我有关卡的完整代码....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 

最佳答案

我认为你在委托(delegate)团中遗漏了一些东西,一旦看到这个

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

委托(delegate)类.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:)。

希望对您有所帮助..

关于ios - Objective-C:委托(delegate)系统在 Xcode 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15764422/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com