自定义委托方法不调用

时间:2017-01-02 06:18:49

标签: ios objective-c protocols

我首先显示start_game屏幕,而不是当我点击按钮时,使用xib.in xib类显示popupview我有创建委托方法。当我关闭popupview时,调用委托方法但不调用

这是我的委托课程     .h文件     #import

@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"];
        }

4 个答案:

答案 0 :(得分:0)

使用以下内容更改.m文件:

<ul>
  <li></li>
  <li class="a">1</li>  <!-- I want to target this li -->
  <li class="a">2</li>
  <li class="a">3</li>
  <li></li>
  <li class="b">4</li>  <!-- I want to target this li -->
  <li class="b">5</li>
  <li class="b">6</li>
</ul>

并按照基本教程步骤进行操作:enter link description here

答案 1 :(得分:0)

请在digalertclose控制器中实施start_games代表

-(void)digalertclose {
}

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

答案 2 :(得分:0)

  1. 在你的digbuttonalert.h协议中定义了两次。
  2. 界面没有@end。
  3. 应该像

    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
    

    ClassB.m

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

    现在在您从ClassA调用委托时设置委托后,它将触发ClassB中的协议实现方法

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

答案 3 :(得分:-1)

在你的课上,你需要像这样声明委托:

@interface YourViewController () < digbuttonalertdelegate >