I’m following Stanford cs193p lesson 7 about using SplitViewControllers and protocols.
I’m doing the same exact stuff the professor is doing (checked multiple times) but I get this error over and over.
I defined a protocol in SplitViewBarButtonItemPresenter.h
#import <UIKit/UIKit.h>
@protocol SplitViewBarButtonItemPresenter <NSObject>
@property (nonatomic,strong) UIBarButtonItem *splitViewBarButtonItem;
@end
in my master CalculatorViewController.h
@interface CalculatorViewController : UIViewController <UISplitViewControllerDelegate>
in CalculatorViewController.m
-(id <UISplitViewControllerDelegate>)splitViewBarButtonItemPresenter
{
id detailVC = [self.splitViewController.viewControllers lastObject];
if(![detailVC conformsToProtocol:@protocol(SplitViewBarButtonItemPresenter)]){
detailVC = nil;
}
return detailVC;
}
- (void)awakeFromNib
{
[super awakeFromNib];
super.splitViewController.delegate = self;
}
and later when I try to set the barButtonItem
-(void)splitViewController:(UISplitViewController *)svc
willHideViewController:(UIViewController *)aViewController
withBarButtonItem:(UIBarButtonItem *)barButtonItem
forPopoverController:(UIPopoverController *)pc
{
barButtonItem.title = self.title;
[self splitViewBarButtonItemPresenter].splitViewBarButtonItem = barButtonItem;
}
I keep getting the error in the title as if I didn’t properly declare the protocol and the delegate. I really don’t know where else to look for errors since I’m following what the Stanford’s professor does line by line, letter by letter.
The error is in this line, specifically:
[self splitViewBarButtonItemPresenter].splitViewBarButtonItem = barButtonItem;
First of all, you have an infinite recursion in your code:
The method calls itself in the return statement and there’s no abort condition.
Second, even if that method would return a value, it would return a
BOOLbut it’s supposed to return anid<UISplitViewControllerDelegate>.Third, either the return type of the method is wrong or the name is misleading. You have a protocol called
SplitViewBarButtonItemPresenterand the name of the method issplitViewBarButtonItemPresenter. So from the name I would expect it to return anid<SplitViewBarButtonItemPresenter>not anid<UISplitViewControllerDelegate>.Last but not least, your
SplitViewBarButtonItemPresenterprotocol is not implemented by theCalculatorViewController.So there are a lot of problems with your code and I would suggest to check that even more often.