0

How to add the functionality of logging a user out from the app,in form of a button on navigation bar's right side.as there's a navigation button which navigates back to the previous view on the left side of the bar.can there be two buttons on the navigation bar?

the login viewcontroller's code is shown below:

- (IBAction)btnLogin:(id)sender
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsPath = [paths objectAtIndex:0];
    NSString *path = [docsPath stringByAppendingPathComponent:@"contacts.db"];
    FMDatabase *database = [FMDatabase databaseWithPath:path];

    if ([txtUser.text length] == 0 || [txtPass.text length]== 0){
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Kindly enter details in all fields" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
    }

    [database open];

    BOOL success = NO;

    NSInteger count = [database intForQuery:@"select count(*) from RegMembers where USERNAME = ? and PASSWORD = ?", txtUser.text, txtPass.text];

    if (count > 0)
    {
        success = YES;
        appDelegate.username = txtUser.text;

        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Welcome" message:@"Login successful" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];

        carTypeViewController *car = [[carTypeViewController alloc]initWithNibName:@"carTypeViewController" bundle:nil];
        [self.navigationController pushViewController:car animated:YES];
    }
    else
    {
        UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:@"Kindly enter the correct credentials" message:@"Entered username or password is incorrect" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert1 show];

        txtUser.text=@"";
        txtPass.text=@"";
    }
    [database close];
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Neelang
  • 69
  • 3
  • 11
  • @BrianShamblen dear friend,i dont think that the question you gave the link to is answering the "logging out" part of my question,so i had to put up this one specifying the logout keyword.so i dont think that it's a duplicate,it can be quite similar though. – Neelang Jan 13 '14 at 18:30

2 Answers2

3

1) For adding a button on the NavBar programmatically you can use code as below

UIBarButtonItem* rightNavButton=[[UIBarButtonItem alloc] initWithTitle:@"logout" style:UIBarButtonItemStyleBordered target:self action:@selector(logout:)]; self.navigationItem.rightBarButtonItem =rightNavButton ;

OR

2) If you are using the Interface builder you can drag a bar button item to the right side of the Navbar.

enter image description here

hv88
  • 328
  • 1
  • 7
  • friend your code that i typed under "viewdidload" is throwing an exception and my app is crashing. – Neelang Jan 13 '14 at 17:50
  • could you share what exception is getting thrown and if possible some of the related code? – hv88 Jan 13 '14 at 18:10
  • the exception is: "[carTypeViewController logout:] unrecognized selector sent to instance " – Neelang Jan 13 '14 at 18:22
  • do you have a method -(void) logout:(id) sender in your class? – hv88 Jan 13 '14 at 18:25
  • no ,there's no method like that in my code.if you wish i can put the code above. – Neelang Jan 13 '14 at 18:36
  • do i have to make any changes to the .h files or appdelegate files? – Neelang Jan 13 '14 at 18:56
  • UIBarButtonItem* rightNavButton=[[UIBarButtonItem alloc] initWithTitle:@"logout" style:UIBarButtonItemStyleBordered target:self action:@selector(logout:)]; self.navigationItem.rightBarButtonItem =rightNavButton ; if you are using this code you need to have a method "logout", you can change the selector method to "btnLogin:" as per your code example in the question, the exception is caused as when you click the button it is looking for the log out method – hv88 Jan 13 '14 at 18:57
  • i changed the selector method to "btnLogin:"(which is the action for logging the user in button),i also tried changing the selector to "btnExit"(which is the action for exiting the app without logging in",but none worked and threw the same exception. – Neelang Jan 13 '14 at 19:13
  • Try removing the ":" from @selector(logout:) and define -(void) logout (without the :(id)sender), refer http://stackoverflow.com/a/9084676/2449268 – hv88 Jan 13 '14 at 19:30
  • your comment helped,i created a logout action for the button navigating app to the login page.you rock dude – Neelang Jan 13 '14 at 19:33
1

This is easy. In IB (Interface Builder), bring up the Utilities area on the right side, and drag the bottom tray up to reveal the list of UI objects. Select the little cube symbol (The UI objects) if it's not already. Type in "uibarbuttonitem" into the search box at the bottom and look for the matching item. drag a UIBarButtonItem from the search results onto the right side of the navigation bar in your view controller. When you get to the right spot it should light up. Release it and the system will add a new navigation item to your navigation bar (actually to the navigation item for the current view controller).

Once you've added a bar button item, you can use the attributes inspector to set it's style and title, and the connections inspector to link it to the appropriate IBAction in your view controller. That should do it.

It is also possible to create a bar button item programmatically and add it in at runtime, but it's a little more work and a little harder to figure out all the settings. Most of the time it's better to do it from IB.

Duncan C
  • 128,072
  • 22
  • 173
  • 272