如何在 XCode 成功登录后将重定向到新视图?

发布时间: 2016/9/30 21:32:42
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我试图建立一个应用程序,成功登录后将重定向到主页。我有两个视图控制器︰ LoginViewControllerDashboardViewController 。我有编码登录部分和已创建的仪表板视图,但不知道如何将重定向到 DashboardViewController 后成功登录。我会很感激一些帮助。

以下是我对的代码 LoginViewController.m 文件︰

#import "LoginViewController.h"

@implementation LoginViewController

@synthesize userName,password,loginbutton,indicator;

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


- (IBAction) loginButton: (id) sender
{
    // TODO: spawn a login thread

    indicator.hidden = FALSE;
    [indicator startAnimating];
    NSString *post =[NSString stringWithFormat:@"username=%@&password=%@",userName.text, password.text];

    NSString *hostStr = @"******";
    hostStr = [hostStr stringByAppendingString:post];
    NSData *dataURL =  [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]];
    NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
    //NSLog(@"Result = '%@'", serverOutput); // look for space between quotes
    serverOutput = [serverOutput stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];



    if([serverOutput isEqualToString:@"Yes"]){


        UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Congrats" message:@"You are authorized"
            delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

        [alertsuccess show];
        [alertsuccess release];


        }
    else {
        UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Username or Password Incorrect"
            delegate:self cancelButtonTitle:@"OK"otherButtonTitles:nil, nil];
        [alertsuccess show];
        [alertsuccess release];
        loginbutton.enabled = TRUE;

    }


    loginbutton.enabled = FALSE;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.userName resignFirstResponder];
    [self.password resignFirstResponder];

}

@end

解决方法 1:

首先添加 #import "DashboardViewController.h" 文件在你 loginViewContrller.m 文件

和创建对象的 DashboardViewController

if([serverOutput isEqualToString:@"Yes"])
{
       DashboardViewController *newView = [[DashboardViewController alloc] init];
       [self presentModalViewController:newView animated:YES];

        UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Congrats" message:@"You are authorized"
            delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

        [alertsuccess show];
        [alertsuccess release];

}
官方微信
官方QQ群
31647020