I am trying to implement JASidePanels where CenterViewController slides and reveals LeftViewControler which contains a TableView. Once the user selects a row in the TableView, I'd like the CenterView to regain it's position by sliding back and also have a method (within CenterViewController) do be called with a parameter from LEftViewController to update the CenterView. Can somebody please help my with this?
Thank you.
There are two parts to solving your problem:
[self.viewController showCenterPanelAnimated:YES]; // add this method to your tableView row
in your left panel class:
// Add to your tableView row method
NSNotification *msg = [NSNotification notificationWithName:@"leftPanelMsg" object:@"Hello"];
[[NSNotificationCenter defaultCenter] postNotification:msg];
in your center panel class: add Observer in viewDidLoad and another method when message is passed back:
- (void)viewDidLoad {
[super viewDidLoad];
// method listen to meesssage with specfic name and calls selector when it get hit
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(msgResponder:) name:@"leftPanelMsg" object:nil];
}
-(void)msgResponder:(NSNotification *)notification {
NSLog(@"name:%@ object:%@", notification.name, notification.object);
}
If you want to use delegates, then you have to add a protocol to the LeftViewController.h file, e.g.
@protocol LeftViewControllerDelegate <NSObject>
-(void)useThisValue:(NSString *)value;
@end
@interface LeftViewController : UITableViewController
@property (weak, nonatomic) id <LeftViewControllerDelegate> delegate;
@end
In your tableView:didSelectRowAtIndexPath: method, you can then add
[self.delegate useThisValue:menu[indexPath.row]];
Your CenterViewController will become the delegate of the LeftViewController, so in your CenterViewController.h file, import the LeftViewController.h and add:
@interface CenterViewController : UIViewController <LeftViewControllerDelegate>
- (void)useThisValue:(NSString *)value;
In your CenterViewController.m file, import both the AppDelegate.h and your RootViewController.h file. In its viewDidLoad method, you have to get a reference to the current instances of your JASidePanelController subclass (RootViewController) and the leftViewController:
RootViewController *rootViewController = (RootViewController *)[[[[UIApplication sharedApplication] delegate] window] rootViewController];
LeftViewController *leftViewController = (LeftViewController *)rootViewController.leftPanel;
and then make the CenterViewController the delegate of the LeftViewController:
leftViewController.delegate = self;
[super viewDidLoad];
Implement the delegate method however you wish, for example:
- (void) useThisValue:(NSString *)value
{
self.label.text = value;
}
I have to acknowledge and thank Kevin McNeish here for pointing out to me how to properly get the instances of the RootViewController and the LeftViewController in order to get the delegate pattern to work with JASidePanels.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With