setDelegate 到我的自定义视图控制器落下无法识别的选择器

Posted

技术标签:

【中文标题】setDelegate 到我的自定义视图控制器落下无法识别的选择器【英文标题】:setDelegate to my custom view controller fall with unrecognized selector 【发布时间】:2014-12-10 20:54:22 【问题描述】:

我在 StoryBoard 上有一个带有自定义类的视图控制器。代码如下:

BGMDatePickerViewController.h

#import <UIKit/UIKit.h>

//declare a protocol with name
@protocol DatePickerViewControllerDelegate        //declare a delegate so that this class can notify another class when a user selects a chart
//protocol methods
//- (void)userDataChangedWithUsername;

@end


//declare a class with inheritance
@interface BGMDatePickerViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate>


//create a public property
@property (weak, nonatomic) id<DatePickerViewControllerDelegate> delegate;   //property to store the delegate


@end

我想在另一个带有委托的 ViewController 上显示这个 ViewController。这里的代码: BGMChartViewController.h

#import <UIKit/UIKit.h>
#import "BGMDatePickerViewController.h"             

//declare a class with inheritance
@interface BGMChartViewController : UIViewController <UIActionSheetDelegate, DatePickerViewControllerDelegate>  //conforms UIActionSheetDelegate and my custom DatePickerViewControllerDelegate protocols

//create a public IB methods
- (IBAction)showDateEntryForm:(id)sender;           

@end

BGMChartViewController.m

#import "BGMChartViewController.h"

@interface BGMChartViewController ()

//create a private properties
@property (strong, nonatomic) UIPopoverController *datePickerPopover;
@end


@implementation BGMChartViewController

- (void)viewDidLoad

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


- (void)viewDidAppear:(BOOL)animated

    [super viewDidAppear:NO];


- (void)didReceiveMemoryWarning

    [super didReceiveMemoryWarning];



- (IBAction)showDateEntryForm:(id)sender

    DDLogVerbose(@"%s: has been started...", __FUNCTION__);

    BGMDatePickerViewController *datePickerVC = [self.storyboard instantiateViewControllerWithIdentifier:@"dateEntryFormVC"];

    datePickerVC.delegate = self;   //HERE THE FALL!!!!!

    self.datePickerPopover = [[UIPopoverController alloc] initWithContentViewController:datePickerVC];

    //define the popover size
    self.datePickerPopover.popoverContentSize = CGSizeMake(320.0, 400.0);

    //let’s display it
    [self.datePickerPopover presentPopoverFromRect:[(UIButton *)sender frame]
                                            inView:self.view
                          permittedArrowDirections:UIPopoverArrowDirectionAny
                                          animated:YES];



@end

在我标记它的那一行上:

-[BGMDatePickerViewController setDelegate:]: unrecognized selector sent to instance 0x78a3bcf0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[BGMDatePickerViewController setDelegate:]: unrecognized selector sent to instance 0x78a3bcf0'

如果我评论这个问题行,我可以看到我的自定义视图控制器,这意味着我可以使用 instantiateViewControllerWithIdentifier 标记... 我不明白我做错了什么?

【问题讨论】:

【参考方案1】:

修改为下面一行并检查:-

@protocol DatePickerViewControllerDelegate<NSObject>

【讨论】:

虽然这是一个很好的更改,但不会影响错误。 不幸的是它没有帮助。请问有什么想法吗? (((

以上是关于setDelegate 到我的自定义视图控制器落下无法识别的选择器的主要内容,如果未能解决你的问题,请参考以下文章

带有自定义视图控制器的 iOS 共享扩展

从 nib 文件在自定义视图中设置自定义委托

如何调用我的自定义警报控制器功能以显示在其他视图控制器中?

带有 nib 子视图的自定义 UIVIew 无

我希望我的视图控制器具有相同的自定义 UIView 组合,它们可以相互通信

表格视图中的自定义单元格未显示?