2017-01-02 157 views
1

我有第一个显示start_game屏幕比当我点击按钮时,显示popupview使用xib.in xib类我有创建委托method.when我关闭弹出视图时调用委托方法,但没有调用自定义委托方法不呼叫

这里是我的委托类 .h文件中 #进口

@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) 
    { 
     [email protected]"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"]; 
     } 
+0

从正在推动digalertClose视图控制器后? – Darshana

+0

@NinjaHattori in viewdidload puching digalert关闭vc – iosdv

+0

你能分享一下这段代码吗? – Darshana

回答

-1

在你的类,你需要声明的委托是这样的:

@interface YourViewController() <digbuttonalertdelegate> 
+0

声明委托不会解决此问题。 – Darshana

+0

@NinjaHattori如果他使用委托,那么他必须指定'@interface YourViewController()''和'obj.delgate = self'。 **教程 - > ** https://www.tutorialspoint.com/ios/ios_delegates.htm –

0

变化.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 

并遵循基本教程步骤:enter link description here

0

请执行digalertclose代表start_games这样的控制器

-(void)digalertclose { 
} 

您使digalertclose可选这就是为什么如果您从digbuttonalertdelegate协议中删除optional关键字不会发生崩溃,您可以看到,因为您试图触发委托但不实施它会发生崩溃。

+0

我会尝试代码 – iosdv

0
  1. 在你的digbuttonalert.h协议中定义了两次。
  2. 接口没有@end。

应该像

ClassA.h

@protocol YourProtocol <NSObject> 

@optional 
-(void)yourProtocolOptionalMethod 

@end 

@interface ClassA.h 

@property (weak, nonatomic) id <YourProtocol> delegate; 

@end 

ClassA.m

-(void) someClassAMethod { 
    [self.delegate yourProtocolOptionalMethod]; 
} 

ClassB.h

#import "ClassA.h" 
@interface ClassB <YourProtocol> 

@end 

柯乐sB.m

-(void) someClassBMethod { 
    ClassA *classA = [ClassA alloc] init]; 
    classA.delegate = self; 
} 

现在设定的委托时,你会调用从ClassA的委托,然后它会触发协议的实现方法在ClassB的

-(void)yourProtocolOptionalMethod { 
    NSlog(@""); 
}