Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable/Disable menuitem #125

Open
mradlmaier opened this issue Jun 14, 2014 · 6 comments
Open

Enable/Disable menuitem #125

mradlmaier opened this issue Jun 14, 2014 · 6 comments

Comments

@mradlmaier
Copy link

Is it possible to enable/disable a menuitem programmatically depending on some value?
If yes, how?

@mradlmaier
Copy link
Author

I have tried several things:

  • Implemented "shouldPerformSegueWithIdentifier" in my MenuViewController. This method is calles (as NSLog shows), but segues are always performed independent of returning YES or NO.

  • Modified "didSelectRowAtIndexPath" in SASlideMenuViewController as follows:

    NSUserDefaults defaults = [NSUserDefaults standardUserDefaults];
    NSString
    segueId = [self.slideMenuDataSource segueIdForIndexPath:indexPath];

    NSLog(@"didSelectRowAtIndexPath");
    NSLog(@"login_active: %hhd", [defaults boolForKey:@"login_active"]);
    NSLog(@"segueId: %@", segueId);

    if ([defaults boolForKey:@"login_active"]) {
    NSLog(@"perform segue");
    [self loadContentAtIndexPath:indexPath];
    } else if(![defaults boolForKey:@"login_active"] && ![segueId isEqualToString:@"ausloggen"]){
    NSLog(@"perform logout");
    [self loadContentAtIndexPath:indexPath];
    } else if (![defaults boolForKey:@"login_active"] && [segueId isEqualToString:@"ausloggen"]){
    NSLog(@"DONT perform logout");
    return;
    }

This had no effect neither. The method is always called, but either it always causes the segues or always doesn't cause segue no matter what the value of [defaults boolForKey:@"login_active"] is...

@stefanoa
Copy link
Owner

Have you tried to use the UITableViewDelegate method:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath

If you return nil tha cell should not be selectable and the segue should not be fired.

@mradlmaier
Copy link
Author

I will try that, thanks
Cheers,
Michael

Von meinem iPhone gesendet

Am 15.06.2014 um 12:57 schrieb stefanoa [email protected]:

Have you tried to use the UITableViewDelegate method:

  • (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
    If you return nil tha cell should not be selectable and the segue should not be fired.


Reply to this email directly or view it on GitHub.

@mradlmaier
Copy link
Author

I implemented it like you suggested:

  • (NSIndexPath )tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString
    segueId = [self.slideMenuDataSource segueIdForIndexPath:indexPath];
    NSLog(@" ");
    NSLog(@"willSelectRowAtIndexPath");
    NSLog(@"login_active: %hhd", [defaults boolForKey:@"login_active"]);
    NSLog(@"segueId: %@", segueId);
    NSLog(@"indexPath: %@", indexPath);
    if ([defaults boolForKey:@"login_active"]) {
    // perform all segues
    NSLog(@"login_active:YES, perform all segues");
    return indexPath;
    } else {
    // perform all segues except "ausloggen"
    if ([segueId isEqualToString:@"ausloggen"]) {
    NSLog(@"login_active:NO, DONT perform ausloggen/logout");
    return nil;
    } else {
    NSLog(@"login_active:NO, perform this segue");
    return indexPath;
    }
    }
    }

But:
If I disable the logout, terminate the app, restart the app, the app will correctly start with out displaying the login controller. Then I enable the logout in the settings controller, and then try to logout, it does not perform the segue as it should, although the logs show, that the user defaults are correctly set, and the indexPath is the one of the logout cell, and even the respective if - branch is executed.
Note: I am using an unwind segue for the logout, because I want that the stack of view controllers is correctly cleared (I fear memory leaking/memory growth unlimited).

The idea is that I have a login to secure sensitive data stored in the app, but that the user can opt out of logging in, if he chooses, too.

@stefanoa
Copy link
Owner

If you have a simple project that I can test I will have a look at it.

@mradlmaier
Copy link
Author

This project is not simple, it has many View Controllers. I would have to setup an example.

But what I have found, is, if I use a normal segue instead of an unwind segue, it works.

My theory is, that unwind segues can only segue to previously presented/executed view controllers, not to any view controller in the storyboards. At least that is my observation:

--> User logins and out --> this works

--> User logins, disables login, closes app, opens app, (no login view controller presented), enables login --> unwind segue doesn't work
--> User logins, disables login, brings app into background, brings app into foreground again, enables login --> unwind segue works

Obviously unwind segue to the login vc only works, if the login vc was previously open.

I wanted to use unwind segues, because my app has many view controllers, and I wanted that, when the user logs out, memory occupied by a huge stack of controllers is freed ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants