为自定义 UINavigationBar 设置委托

Set delegate for a custom UINavigationBar

我正在尝试添加自定义元素,包括从我的 navigationBar 更改下拉列表和视图,因此我需要一个自定义元素。但是,当尝试将委托设置为我的视图控制器时,它会抛出错误。

@interface MyViewController : UIViewController <MyCustomNavigationBarDelegate>

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UINavigationController *nav = [[UINavigationController alloc] initWithNavigationBarClass:[MyCustomNavigationBar class] toolbarClass:[UIToolbar class]];
    nav.navigationBar.assessmentDelegate = self;
}

最后一行抛出错误 属性 'assessmentDelegate' not found on object of type 'UINavigationBar *' 但是它应该是 class MyCustomNavigationBar 显然有代表:

@protocol MyCustomNavigationBarDelegate;

@interface MyCustomNavigationBar : UINavigationBar

@property (weak, nonatomic) id <MyCustomNavigationBarDelegate> assessmentDelegate;

@end

@protocol MyCustomNavigationBarDelegate <NSObject>

-(void)presentViewController:(UIAlertController *)alert;

@end

将您的导航栏投射到MyCustomNavigationBar

MyCustomNavigationBar  *switchNav = (MyCustomNavigationBar *)nav.navigationBar;
switchNav.assessmentDelegate = self