是否可以从ConnectionDidFinishLoading
推送到新的视图控制器?
我有以下内容,它会执行ConnectionDidFinishLoading
,但不会推送到新的视图控制器。
-(void)connectionWithURLString:(NSMutableArray *)urlString
{
NSDate *now = [NSDate date];
int userID = [[clientDataStruct.clientData objectForKey:@"number"] intValue];
NSDate *date = self.datePicker.date;
// NSLog(@"obj: %@", urlString);
NSString *extrString = [NSString stringWithFormat:@", customerdetails:{customer_id = %d, job_date = %@, date_created = %@}",userID,date, now];
NSString *post = [NSString stringWithFormat:@"json=quote:{%@%@}", urlString, extrString ];
NSLog(@"post: %@", post);
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *value = [defaults objectForKey:@"value"];
NSString *jsonUrl = [NSString stringWithFormat:@"xxx?task=add_quote&user_id=%@&customer_id=%@",value, [clientDataStruct.clientData valueForKey:@"number"]];
[request setURL:[NSURL URLWithString:jsonUrl]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
if (!connection)
NSLog(@"Connection failed!");
}
- (void)Success
{
UIStoryboard *subVc = [UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle:nil];
UserTableView *userTable = [subVc instantiateViewControllerWithIdentifier:@"UserTableView"];
userTable.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self.navigationController pushViewController:userTable animated:YES];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"%@", error);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[self performSelectorOnMainThread:@selector(Success) withObject:Nil waitUntilDone:YES];
NSLog(@"connectionDidFinishLoading%@", connection);
}
一切正常,并返回我的数据,数据会被添加到SQL
。我的NSLog
显示ConnectionDidFinishLoading
。但它没有推向新的View Controller。
感谢您的帮助,请抓住这个。
感谢Fahim建议允许我进一步调试,导航视图打破了返回的模型控制器。将其更改为推送并且一切正常
答案 0 :(得分:1)
由于您没有导航控制器,因此无法使用此功能。确保你有导航控制器。
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[self performSelectorOnMainThread:@selector(LoginSuccess) withObject:Nil waitUntilDone:YES];
NSLog(@"connectionDidFinishLoading%@", connection);
}
这是错误的。即使用户输入了错误的用户名或密码,您仍然允许用户登录。
检查您在connectionDidFinishLoading中获得的响应,然后根据此响应执行转换。
e.g。如果你在后端有PHP,你的代码将是
sql query here to add data
if (data added) {
echo "valid";
} else {
echo "invalid";
}
现在在connectionDidFinishLoading中检查此响应,并根据此进行转换
答案 1 :(得分:0)
首先检查如果您的LoginSuccess被调用,如果是,则粘贴此
UserTableView * userTable =[self.storyboard instantiateViewControllerWithIdentifier:@"UserTableView"];
[self.navigationController pushViewController:userTable animated:YES]
喜欢这个但是用UserTableView替换ResultsTableView