I want to stack a UIViewController Board on top of a UIViewController Menu in order to create a Facebook-like side menu. This menu should contain a UITextView.
So far I can drag the Board View side ways and the Menu appears underneath it. Great. But there’s an issue with the UITextView inside the menu. When I click it the app crashes with a BAD_EXC… exception. It seems like an issue with the UITextView Delegate.
Here’s how I currently set it.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//Board
board = [[BoardViewController alloc] init];
[self.window setRootViewController:board];
//Menu
MenuViewController* menu = [[MenuViewController alloc]init];
menu.textView.delegate = menu;
[self.window addSubview:menu.view];
//Window
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
Note: when I set the textView delegate to board and implement the delegate methods there it works but that really seems to be the wrong place to implement the menu textview methods to me. The delegate of the menu should be in the menu class itself.
How to set the menu’s textView delegate correctly to the menu class?
And then, poof: the menu view controller isn’t referenced again and is deallocated by ARC. The view is retained by the window so it will look like everything is ok until the text view tries to send its delegate message to an object which was long since deallocated. This is the cause of your
EXC_BAD_ACCESScrash.The simple, slack solution would be to define a property in your app delegate for the menu view controller.
and then store the menu there
The proper solution and the one I recommend is that you look up UIViewController containment and implement your own custom view controller container that looks after this special arrangement of view controllers.
Just to briefly outline: You would have a subclass of
UIViewControllerwith two properties, one for the board view controller and one for the menu view controller. It would have a scroll view and would be responsible for the sliding action and any communication that needed to be passed from the board to the menu and vis versa. This container would also be responsible for loading the board and the menu view controllers and inserting their views into the correct places in its own view. If the board view controller needs to be swapped out for another board then the container would be responsible for this also.