Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending data between ViewController with JASidePanels

Tags:

ios

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.

like image 884
pbeaumier Avatar asked Feb 23 '26 23:02

pbeaumier


2 Answers

There are two parts to solving your problem:

  1. first showing center panel when user select a row:

[self.viewController showCenterPanelAnimated:YES]; // add this method to your tableView row

  1. passing a message back to center panel with new instruction, this can be done by creating delegate or notification. to keep it simple i will use Notification:

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);
}
like image 114
Shams Ahmed Avatar answered Feb 26 '26 12:02

Shams Ahmed


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.

like image 38
user2330922 Avatar answered Feb 26 '26 13:02

user2330922



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!