OGeek|极客世界-中国程序员成长平台

标题: ios - 为自定义 UINavigationBar 设置委托(delegate) [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 16:31
标题: ios - 为自定义 UINavigationBar 设置委托(delegate)

我正在尝试添加自定义元素,包括从我的 navigationBar 更改的下拉菜单和 View ,因此我需要一个自定义元素。但是,当尝试将委托(delegate)设置为我的 View Controller 时,它会引发错误。

@interface MyViewController : UIViewController <MyCustomNavigationBarDelegate>

@implementation MyViewController

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

最后一行抛出错误 Property 'assesmentDelegate' not found on object of type 'UINavigationBar *' 但是它应该是类的类型 MyCustomNavigationBar 显然有委托(delegate):

@protocol MyCustomNavigationBarDelegate;

@interface MyCustomNavigationBar : UINavigationBar

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

@end

@protocol MyCustomNavigationBarDelegate <NSObject>

-(void)presentViewControllerUIAlertController *)alert;

@end



Best Answer-推荐答案


将您的导航栏转换到 MyCustoms 导航栏

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

关于ios - 为自定义 UINavigationBar 设置委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37274218/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4