gpt4 book ai didi

ios - 创建注销按钮

转载 作者:行者123 更新时间:2023-11-29 01:28:45 34 4
gpt4 key购买 nike

注意:第一个应用程序;

我目前正在尝试为我创建的应用程序创建注销按钮。本质上,加载时,用户会看到 LoginViewController.xib,其中包含 2 个文本字段和一个按钮,假设文本字段满足参数,按下按钮时,将执行以下参数:

if (success) {
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate.window setRootViewController:appDelegate.tabBarController];
}

这工作正常,用户被带到应用程序中,带有一个在 3 个 xib(主页、设置、表格)之间切换的选项卡 Controller 。

在设置选项卡中,我有一个按钮“LogOut”,按下该按钮时,我希望用户返回到“LoginViewController.xib”,但我似乎无法从教程中找到任何方法来执行此操作YouTube 或网络上。

请参阅下面的设置编码;

SettingsViewController.h:

#import <UIKit/UIKit.h>

@interface SettingsViewController : UIViewController

- (IBAction)LogOutClick:(id)sender;

@end

SettingsViewController.m:

#import "SettingsViewController.h"

@interface SettingsViewController ()

@end

@implementation SettingsViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)LogOutClick:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}

@end

最佳答案

调用 dismissViewControllerAnimated: 不会有任何帮助,因为您的标签栏 Controller /设置 View Controller 不由任何 View Controller 呈现。据我从您提供的代码中得知,原始登录 Controller 已不存在于内存中,因此您无法“返回”它。

您有两种选择:一种是在选项卡栏 Controller 上呈现登录 View Controller ,另一种是将窗口的 Root View Controller 更改为登录 Controller 。例如

//Present over tab bar
- (IBAction)LogOutClick:(id)sender {
LoginViewController *loginController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
[self presentViewController:loginController animated:YES completion:nil];
}

//Switch root view controller
- (IBAction)LogOutClick:(id)sender {
LoginViewController *loginController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
[appDelegate.window setRootViewController:loginController];
}

在我看来,第一种方法是更好的做事方式。关闭登录 Controller 后,您可以使用适当的数据填充选项卡栏 Controller 的 View 。但是,由于登录 Controller 已经有一个方法可以切换窗口的 Root View Controller ,因此您可能更容易将 Root View Controller 切换回登录 Controller 。

关于ios - 创建注销按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33716515/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com