Make disappear buttons based on login

Asked

Viewed 68 times

0

I have the problem in changing Toolbar buttons (Barbutton Items) depending on whether you are logged in or not.

Tableviewcontroller:

- (void)viewDidLoad {
   [super viewDidLoad];

   User *userObj = [[User alloc] init];

   if([userObj userAuthenticated]){

     [self showLoginButtons];
   }
   else{

     [self showLogoutButtons];

 }

[self.tableView reloadData];

}

Show buttons when login:

- (void) showLoginButtons{

    NSArray *toolbarButtonsLogin = [NSMutableArray arrayWithObjects: _addButton, _editButton, _flexibleSpace,  _logOutButton, nil];

    //[_myToolbar setItems: toolbarButtonsLogin animated:NO]; // com esta linha também não resulta
    [_myToolbar setItems: toolbarButtonsLogin animated:NO];
    NSLog(@"loginBttons");
}

Show buttons available for users logout:

- (void) showLogoutButtons{

    NSArray *toolbarButtonsLogin = [NSMutableArray arrayWithObjects: _flexibleSpace,  _logInButton, nil];

    //[_myToolbar setItems: toolbarButtonsLogin animated:NO]; // com esta linha também não resulta
    [_myToolbar setItems: toolbarButtonsLogin animated:NO];
    NSLog(@"logoutBttons");
}

Logout:

- (IBAction)LogOut:(UIBarButtonItem *)sender {

   User *userObj = [[User alloc] init];
   [userObj logout];

   [self showLogoutButtons];

}

Loginviewcontroller:

- (IBAction)LogIn:(UIButton *)sender {

    TableViewController *resetButtons = [[TableViewController alloc] init];

    if([userObj userAuthenticated]) {

      [resetButtons showLoginButtons];
      [self.view setNeedsDisplay];
      [self dismissLoginAndShowProfile];

    }

}

I’ve tried to do with: [_addButton setTintColor: nil]; with enable/disable, removeObj, and even put the width: 0.01, but nothing works, just strange behavior for no apparent reason. The login/logout is working properly without any problems, only the buttons do not do what is supposed to (Hide/show) depending on the status of the user. I also know that you are entering the correct functions because you are printing the correct Nslog.

  • your logic wouldn’t be the other way around? If the user is logged in, display the logout buttons and vice versa?

  • This is correct. When I am logged in the add, Edit and logout button appears and when I am not only the login button appears.

2 answers

0

No point in setting tintColor for nil. If you set the tintColor for [UIColor clearColor] and enabled for NO the button will disappear.

  • 1

    I would use [button setHidden:YES] instead of disabling the button and putting it transparent, because in my view it would still be there, occupying space of something that could be "clickable"

  • This method does not exist for a Uibarbuttonitem. There is some equivalent?

-1

Try to set the Alpha to 0, as follows: [UIBarButtonItem setAlpha:0];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.