Reputation: 509
I'm using the SASlideMenu library to implement a left slide menu in my iOS ARC storyboard app. I have added three View Controllers called with the [self performSegueWithIdentifier:@"segueID" sender:self];
code, and the slide menu works fine, but every time it create a NEW INSTANCE of a View Controller while I need to cache each controller content (example, in View Controller 1 we have a text field: If I write a word, after changing VC I must find again same word!). This is generic Storyboard:
This is my UITableView delegate, in SASlideMenuViewController:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:
UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
}
if (indexPath.row == 0) {
cell.textLabel.text = @"ViewController 1";
}
if (indexPath.row == 1) {
cell.textLabel.text = @"ViewController 2";
}
if (indexPath.row == 2) {
cell.textLabel.text = @"ViewController 3";
}
cell.textLabel.font = [UIFont systemFontOfSize:14];
cell.textLabel.textColor = [UIColor blackColor];
cell.imageView.image = [UIImage imageNamed:@"image.png"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.row==0)
{
[self performSegueWithIdentifier:@"segueID1" sender:self]; // I NEED TO RETRIEVE SAME VIEW CONTROLLER 1 CONTENT AFTER ANOTHER VIEW CONTROLLER CALL!
}
if(indexPath.row==1)
{
//[self performSegueWithIdentifier:@"segueID2" sender:nil]; // I NEED TO RETRIEVE SAME VIEW CONTROLLER 2 CONTENT AFTER ANOTHER VIEW CONTROLLER CALL!
}
if(indexPath.row==2)
{
//[self performSegueWithIdentifier:@"segueID3" sender:nil]; // I NEED TO RETRIEVE SAME VIEW CONTROLLER 3 CONTENT AFTER ANOTHER VIEW CONTROLLER CALL!
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
And this is a screenshot of slide menu in app:
In the author tutorial I can read: "If you are using dynamic cell prototype or you are using static cells and you want to cache the content view controller, assign an identifier that will be returned in the segueIdForIndexPath: method linked to the desired indexPath" but I'm losing my head and I cannot get the precise point.. please can u provide the precise code to resolve this? There is something else to add, but where? I need to reproduce this situation: EVERYTIME I call a View Controller from the left menu I MUST retrieve it identical as I have leaved before another previous VC call. Thanks!
Upvotes: 1
Views: 910
Reputation: 46
In order to have your code working you need to implement two methods of the SASlideMenuDataSource protocol and you have to initialize the datasource property of your ViewController. Moreover you have to avoid to directly invoke performSegueWithIdentifier and you should avoid to implement tableView:didSelectRowAtIndexPath: method, because otherwise caching will not work.
The first method to implement is configureMenuButton:. You need it because you need the menu button in the content ViewController you are sliding in. One possibility is to copy the icons provided in the example project and add them to your project:
-(void) configureMenuButton:(UIButton *)menuButton{
menuButton.frame = CGRectMake(0, 0, 40, 29);
[menuButton setImage:[UIImage imageNamed:@"menuicon.png"] forState:UIControlStateNormal];
[menuButton setBackgroundImage:[UIImage imageNamed:@"menu.png"] forState:UIControlStateNormal];
[menuButton setBackgroundImage:[UIImage imageNamed:@"menuhighlighted.png"] forState:UIControlStateHighlighted];
[menuButton setAdjustsImageWhenHighlighted:NO];
[menuButton setAdjustsImageWhenDisabled:NO];
}
Than you need to implement segueIdForIndexPath:. The method returns the segueId associated to the indexPath of the row of the menu, looking at your code should be something like:
-(NSString*) segueIdForIndexPath:(NSIndexPath *)indexPath{
if(indexPath.row==0){
return @"vista1";
} else if (indexPath.row==1){
return @"vista2";
}else if(indexPath.row==2){
return @"vista3";
}
return @"vista1";
}
Finally you have to correctly initialize the dataSource property of your ViewController:
-(id) initWithCoder:(NSCoder *)aDecoder{
if (self = [super initWithCoder:aDecoder]) {
self.slideMenuDataSource = self;
}
return self;
}
I hope you will find the answer useful.
Upvotes: 3