1

当 URL 请求成功完成后,我如何将用户定向到定义的视图控制器。我有一个带有用户名和密码字段的登录页面。下面是我的代码。任何帮助将不胜感激。

#import "LoginTableViewController.h"

@interface LoginTableViewController ()
@property (weak, nonatomic) IBOutlet UITextField *UIEmailTextField;
@property (weak, nonatomic) IBOutlet UITextField *UIPasswordTextField;

@end

@implementation LoginTableViewController
@synthesize UIEmailTextField;
@synthesize UIPasswordTextField;

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
    // Custom initialization
}
return self;
}

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

- (void)viewDidUnload
{
[self setUIEmailTextField:nil];
[self setUIPasswordTextField:nil];
[super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - barButton Outlet

- (IBAction)loginButtonPressed:(UIBarButtonItem *)sender {
NSString *data = [NSString stringWithFormat:@"username=%@&password=%@",UIEmailTextField.text, UIPasswordTextField.text];
NSData *postData = [data dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

// preaparing URL request to send data.

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
NSString *url = [NSString stringWithFormat:@"http://www.twitter.com"];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
[request setTimeoutInterval:7.0];

NSURLResponse *response;
NSError *error;

NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];

NSLog(@"Login response:%@",str);

NSLog(@"Log In button was pressed!");
}

@end
4

3 回答 3

0

你现在设置它的方式,你的 NSURLConnection 将阻塞主线程,直到它完成。如果您使用异步 NSURLConnection,您可以成为它的委托人,并对失败和成功做出适当的响应,如下所示:

NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];  //Conform to NSURLConnectionDelegate before you use this.

-(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response
{
    //Got a response.  Not necessarily an error, so login might have been successful,
    //or it might not have.
}
-(void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
    //The connection returned data.  Really only useful for requesting files.
}
-(void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
    //Handle the error properly
}
-(void)connectionDidFinishLoading:(NSURLConnection*)connection
{
    //Connection's finished completely.  Probably a good time to check for an error,
    //Or change the view controller 
}

此外,更多的旁注,属性不应以ObjC中的大写字母开头,这是类使用的情况。而且最好不要进入 UI 命名空间来获取变量名。

于 2012-10-20T20:02:55.223 回答
0

如果您正在尝试创建登录页面,我建议您采用不同的方法:首先放置成功登录将带您进入的页面,并将其设为您的根视图控制器。然后,立即将登录控制器推送为模态控制器。这样,即使登录失败,您也可以再次将登录屏幕向上推。如果您允许用户保留其凭据并且不必在启动后再次登录(如果适用),这也会清除您的代码。无论如何,这是一种更清洁的方法。您没有在 rootViewController 级别返回登录屏幕。

于 2012-10-20T20:03:25.160 回答
0

设置您的视图控制器,以便LoginTableViewController在成功登录后关闭将显示适当的视图控制器。

例如:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIViewController *rootViewController = [[UIViewController alloc] init];
    self.window.rootViewController = rootViewController;

    LoginTableViewController *loginViewController = [[LoginTableViewController alloc] init];
    [rootViewController presentViewController:loginViewController animated:NO completion:nil];

    return YES;
}

登录成功后,关闭LoginTableViewController

[self dismissViewControllerAnimated:YES completion:nil];
于 2012-10-20T20:13:54.783 回答