Ios 自定义委托方法不能调用
当我单击按钮时,我第一次显示开始\游戏屏幕,此时使用xib显示popupview。在xib类中,我创建了委托方法。当我关闭popupview时,该时间调用委托方法,但不调用 这是我的代表课 .h文件 进口Ios 自定义委托方法不能调用,ios,objective-c,protocols,Ios,Objective C,Protocols,当我单击按钮时,我第一次显示开始\游戏屏幕,此时使用xib显示popupview。在xib类中,我创建了委托方法。当我关闭popupview时,该时间调用委托方法,但不调用 这是我的代表课 .h文件 进口 @protocol digbuttonalertdelegate; @interface digbuttonalert : UIViewController @property (weak, nonatomic) IBOutlet UIImageView *bg_image; @prope
@protocol digbuttonalertdelegate;
@interface digbuttonalert : UIViewController
@property (weak, nonatomic) IBOutlet UIImageView *bg_image;
@property (weak, nonatomic) IBOutlet UILabel *lbl_title;
@property (nonatomic, weak) id<digbuttonalertdelegate> delegate;
@end
@protocol digbuttonalertdelegate <NSObject>
@optional
-(void)digalertclose;
@end
.m File
#import "digbuttonalert.h"
#import "suggestion_alert.h"
#import "UIViewController+CWPopup.h"
#import "zoom_alert.h"
@interface digbuttonalert ()
{
bool status;
}
@end
@implementation digbuttonalert
- (void)viewDidLoad {
[super viewDidLoad];
status=0;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected)];
singleTap.numberOfTapsRequired = 1;
[self.bg_image setUserInteractionEnabled:YES];
[self.bg_image addGestureRecognizer:singleTap];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)close:(id)sender {
}
-(void)tapDetected{
NSLog(@"single Tap on imageview");
if(status==0)
{
self.lbl_title.text=@"As you walk you will discover the hidden map.This circle will show your progress.";
status=1;
}
else
{
[self dismissViewControllerAnimated:YES completion:nil];
if ([self.delegate respondsToSelector:@selector(digalertclose)]) {
[self.delegate digalertclose];
}
}
}
在这里,我想调用这个类的方法
#import "digbuttonalert.h"
@interface start_games () <MJSecondPopupDelegate,digbuttonalertdelegate>
{
- (void)viewDidLoad {
digbuttonalert *next=[[digbuttonalert alloc]init];
next.delegate=self;
next.modalTransitionStyle=UIWebPaginationModeRightToLeft;
next.modalPresentationStyle=17;
[self presentViewController:next animated:YES completion:nil];
}
- (void)digalertclose
{
[self StartTimer];
[[NSUserDefaults standardUserDefaults]setObject:@"false" forKey:@"alertstatus"];
}
在类中,您需要如下声明委托:
@interface YourViewController () < digbuttonalertdelegate >
将.m文件更改为以下内容:
@protocol digbuttonalertdelegate <NSObject>
@optional
-(void)digalertclose;
@end
@interface digbuttonalert : UIViewController
@property (weak, nonatomic) IBOutlet UIImageView *bg_image;
@property (weak, nonatomic) IBOutlet UILabel *lbl_title;
@property (nonatomic, weak) id<digbuttonalertdelegate> delegate;
@end
并遵循基本的教程步骤:请像这样在开始游戏控制器中实现digalertclose委托
-(void)digalertclose {
}
您将digalertclose设置为可选的,这就是为什么不会发生崩溃的原因。如果从digbuttonalertdelegate协议中删除可选关键字,您可以看到将发生崩溃,因为您尝试触发一个委托,但没有实现它
在digbuttonalert.h协议中定义了两次。
接口没有@end。
应该是
A.h级
@protocol YourProtocol <NSObject>
@optional
-(void)yourProtocolOptionalMethod
@end
@interface ClassA.h
@property (weak, nonatomic) id <YourProtocol> delegate;
@end
B.h类
#import "ClassA.h"
@interface ClassB <YourProtocol>
@end
现在设置委托之后,当您从ClassA调用委托时,它将触发ClassB中的协议实现方法
-(void)yourProtocolOptionalMethod {
NSlog(@"");
}
从您推digalertClose视图控制器的位置?@NinjaHattori在视图中加载puching digalertClose vc您可以共享该代码吗?@NinjaHattori我将添加代码如果其视图控制器,你应该用XIB初始化,或者从故事板初始化,或者重写视图控制器中的Init方法。我很惊讶。当你展示视图时,它会显示你的视图吗?@NinjaHattori如果他使用委托,那么他必须指定@interface YourViewController
-(void) someClassBMethod {
ClassA *classA = [ClassA alloc] init];
classA.delegate = self;
}
-(void)yourProtocolOptionalMethod {
NSlog(@"");
}