Highlight active link in SWRevealViewController

跟風遠走 提交于 2020-01-24 12:06:05

问题


I'm using SWRevealViewController in my project to create a slide out menu for my app.

Is it possible to highlight the currently active link in the menu which the slide out holds?

I've tried sending a page reference identifier to the sidebarviewcontroller but it seems like this has no effect as the view is only loaded once when the app launches and is then simply shown and hidden.

Any help would be much appreciated.

Cheers


回答1:


I'm not sure that this is actually a question related to SWRevealViewController. Most likely your rear/bottom/menu view is really a UITableViewController. If that is that case then the solution is simple. Request that the UITableViewController remember its selected state.

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    self.clearsSelectionOnViewWillAppear = NO;
}

This solution comes from: UITableView doesn't keep row selected upon return




回答2:


I ended up doing this using NSNotification center. In each view controller's viewWillAppear I added...

// Send notification containing page reference to be picked up by sidebar view controller
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"homePage" forKey:@"page"];
[[NSNotificationCenter defaultCenter] postNotificationName: @"activePage" object:nil userInfo:userInfo];

Which was picked up in the SidebarViewController viewDidLoad...

// Pick up the page reference notification and trigger receivePageReference function
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(receivePageReference:)
                                             name:@"activePage"
                                           object:nil];

Then in the receivePageReference action...

-(void)receivePageReference:(NSNotification *)notification{

I reset all labels to a non-bold font

// Reset all labels to non-bold font
UILabel *homeLabel = (UILabel *)[self.view viewWithTag:12];
homeLabel.font = [UIFont fontWithName:@"Colaborate-Thin" size:19];

UILabel *liveLabel = (UILabel *)[self.view viewWithTag:13];
liveLabel.font = [UIFont fontWithName:@"Colaborate-Thin" size:19];

UILabel *racesLabel = (UILabel *)[self.view viewWithTag:14];
racesLabel.font = [UIFont fontWithName:@"Colaborate-Thin" size:19];

etc..

and set the label with the corresponding page reference to bold...

NSDictionary *notificationInfo = notification.userInfo;
if ([[notificationInfo objectForKey:@"page"] isEqualToString:@"homePage"]){
    UILabel *homeLabel = (UILabel *)[self.view viewWithTag:12];
     homeLabel.font = [UIFont fontWithName:@"Colaborate-Medium" size:19];
}


来源:https://stackoverflow.com/questions/23698043/highlight-active-link-in-swrevealviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!