如何注销[iOS]

时间:2015-10-30 19:46:26

标签: ios objective-c

我有2个ViewControllers。登录后,它将移动到下一个屏幕。然后会出现一个注销按钮。在用户按下注销按钮后,它应返回到具有空文本字段的登录屏幕。

我该怎么做?我尝试了所有方法,但似乎没有用。

我的第一个ViewController:

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.sampleDictionary = @{@"username":@"alex", @"password":@"1234"};
}

- (IBAction)loginTapped
{
    if ([self.sampleDictionary[@"password"] isEqualToString:passwordField.text]) {
        [self performSegueWithIdentifier:@"ss" sender:self];
    } else {
        // Alert message
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"wrong" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:nil];
        [alertController addAction:actionOk];
        [self presentViewController:alertController animated:YES completion:nil];
    }
}

@end

这是我的第二个ViewController:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.hidesBackButton = YES;
}

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

2 个答案:

答案 0 :(得分:3)

ViewController中删除viewWillAppear中用户名和密码字段的文字。

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    usernameField.text = @"";
    passwordField.text = @"";
}

答案 1 :(得分:0)

在你的第二个ViewController中:

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIButton *logoutButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, 320, 50)];
    [logoutButton setTitle:@"Log Out" forState:UIControlStateNormal];
    [logoutButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [logoutButton setBackgroundColor:[UIColor blackColor]];
    [logoutButton addTarget:self action:@selector(logout) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:logoutButton];
}

- (void)logout
{
    [self dismissViewControllerAnimated:YES completion:nil];
}