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

时间:2013-04-18 03:46:06

标签: objective-c xcode redirect uiviewcontroller

我正在尝试构建一个成功登录后重定向到主页的应用。我有两个视图控制器: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

2 个答案:

答案 0 :(得分:3)

首先在#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];

}

答案 1 :(得分:1)

1)将tag设为alert为alertsuccess.tag = 1;

2)使用以下代码显示下一个ViewController

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 1 && buttonIndex == 0)
    {
         DashboardViewController *vcDashboard = [[DashboardViewController alloc] init];
         if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0)
         {
             [self presentViewController:vcDashboard animated:YES completion:nil];
         }
         else
         {
             [self presentModalViewController:vcDashboard animated:YES];
         }
         [vcDashboard release];
    }
}