2012-10-20 70 views
0

當URL請求成功完成時,我將如何指導用戶定義的View Controller。我有一個帶有用戶名和密碼字段的登錄頁面。以下是我的代碼。任何援助 將不勝感激。登錄成功後轉到View Controller Xcode

#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 

回答

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命名空間中插入數據總是明智的。

0

如果你試圖做一個登錄頁面,我建議一個不同的方法:豎起登錄成功將帶你到第一,並使其你的根視圖控制器的頁面。然後,立即將登錄控制器作爲模式控制器。這樣,即使登錄失敗,也可以重新登錄屏幕。如果您允許用戶保留其憑據並且無需在啓動後重新登錄(如果適用),這也會清除您的代碼。無論如何,這是一個更清潔的方法。您沒有在rootViewController級別返回登錄屏幕。

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]; 
相關問題