为自定义UINavigationBar设置委托

时间:2016-05-17 11:00:44

标签: ios objective-c delegates 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”在类型为“UINavigationBar *”的对象上找不到但是它应该是类MyCustomNavigationBar的类型,它明确地具有委托:< / p>

@protocol MyCustomNavigationBarDelegate;

@interface MyCustomNavigationBar : UINavigationBar

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

@end

@protocol MyCustomNavigationBarDelegate <NSObject>

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

@end

1 个答案:

答案 0 :(得分:1)

cast your navigaton Bar to MyCustomNavigationBar

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