ios - 如何在导航栏右侧显示注销按钮以从应用程序注销?

标签 ios

<分区>

如何以导航栏右侧按钮的形式添加从应用程序中注销用户的功能。因为导航栏左侧有一个导航按钮,可以导航回上一个 View 。可以吗导航栏上有两个按钮吗?

登录 View Controller 的代码如下所示:

- (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];
}

最佳答案

1) 要以编程方式在 NavBar 上添加按钮,您可以使用如下代码

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

2) 如果您使用的是界面构建器,您可以将条形按钮项拖到导航栏的右侧。

enter image description here

关于ios - 如何在导航栏右侧显示注销按钮以从应用程序注销?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21095969/

上一篇:ios - 在关闭 View 之前调用委托(delegate)方法

下一篇:ios - 将iOS应用程序连接到Google App Engine教程

相关文章:

ios - @synchronized [UIView animationWithDuration ...] 锁定结束?

ios - UICollectionView 的动态单元格宽度取决于标签宽度

ios - 遍历数组(Swift)

iOS应用程序在集成revmob后随机崩溃

ios - 我的页面菜单不显示 View Controller

iOS 应用程序更新和用户默认值

ios - 如何从 FBLoginView 获取头像?

ios - 如何取消解析获取数据请求

ios - UnsafePointer 不再适用于 swift 3

ios - 应用程序被 SIGSEGV 终止