当URL请求成功完成后,如何将用户定向到定义的View Controller。我有一个带有用户名和密码字段的登录页面。以下是我的代码。任何帮助 非常感谢。
#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 :(得分: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
}
另外,更多的旁注,属性应该 NOT 以ObjC中的大写字母开头,这就是类使用的情况。并且明智的做法是不要使用变量名称的UI命名空间。 的
答案 1 :(得分:0)
如果您正在尝试登录页面,我建议采用不同的方法:将成功登录的页面放在首位,然后将其设置为根视图控制器。然后,立即将登录控制器作为模态控制器推送。这样,即使登录失败,您也可以再次按下登录屏幕。如果您允许用户保留其凭据而不必在启动后再次登录(如果适用),这也会清除您的代码。无论如何,这是一种更清洁的方法。您没有rootViewController级别的登录屏幕。
答案 2 :(得分: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];