我有一个登录屏幕后,它将移动到下一个视图控制器,我已经使用了一些网络,如http,json从服务器获取数据。当我输入登录用户名/密码然后,如果我点击登录按钮,它延迟到8秒后,只有它移动到下一个视图控制器。仍然我的登录屏幕单独显示8秒,然后只有它移动到下一个视图控制器。 / p>
这是我的login controller.m:
@implementation mainViewController
- (void)viewDidLoad {
[super viewDidLoad];
_username.delegate = self;
_password.delegate = self;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![defaults boolForKey:@"reg"]) {
NSLog(@"no user reg");
_logBtn.hidden = NO;
}
}
- (void)viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:YES animated:animated];
[super viewWillAppear:animated];
_username.text = nil;
_password.text = nil;
}
- (IBAction)LoginUser:(id)sender {
if ([_username.text isEqualToString:@"sat"] && [_password.text isEqualToString:@"123"]) {
NSLog(@"Login success");
[self performSegueWithIdentifier:@"nextscreen" sender:self];
}
else {
NSLog(@"login was unsucess");
// Alert message
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"wrong"
message:@"Message"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"Ok"
style:UIAlertActionStyleDefault
handler:nil];
[alertController addAction:actionOk];
[self presentViewController:alertController animated:YES completion:nil];
}
}
这是我的nextcontroller.m
- (void)viewDidLoad {
[super viewDidLoad];
//for search label data
self.dataSourceForSearchResult = [NSArray new];
//collection of array to store value
titleArray = [NSMutableArray array];
// here only i am getting data from server
[self getdata];
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
[self.collectionView reloadData];
}
帮帮我。如果我的问题不明白。我可以更多地了解我的帖子。我的nextcontroller.m
[self getdata]
是我从服务器网址获取数据。谢谢
我的get data:
-(void)getdata {
NSString *userName = @“users”;
NSString *password = @“images”;
NSData *plainData = [password dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [plainData base64EncodedStringWithOptions:0];
base64String=[self sha256HashFor: base64String];
NSString *urlString = @"https://porterblog/image/file”;
NSMutableURLRequest *request= [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"GET"];
NSString *authStr = [NSString stringWithFormat:@"%@:%@", userName, base64String];
NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];
NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedStringWithOptions:0]];
[request setValue:authValue forHTTPHeaderField:@"Authorization"];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *str = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSError * error;
self->arrayPDFName = [[NSMutableArray alloc]init];
NSDictionary *jsonResults = [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableContainers error:nil];
NSDictionary *dictOriginal = jsonResults[@“birds”];
[titleArray addObject:[NSString stringWithFormat:@" birds(%@)”, dictOriginal[@"count"]]];
NSDictionary *dictOriginal2 = jsonResults[@"owl”];
[titleArray addObject:[NSString stringWithFormat:@" Owl(%@)”, dictOriginal2[@"count"]]];
NSDictionary *dictOriginal3 = jsonResults[@"pensq”];
[titleArray addObject:[NSString stringWithFormat:@" Pensq(%@)”, dictOriginal3[@"count"]]];
NSDictionary *dictOriginal4 = jsonResults[@“lion”];
[titleArray addObject:[NSString stringWithFormat:@" lion(%@)”, dictOriginal4[@"count"]]];
NSArray *arrayFiles = [NSArray arrayWithObjects: dictOriginal, dictOriginal2, dictOriginal3, dictOriginal4, nil];
NSLog(@"str: %@", titleArray);
for (NSDictionary *dict in arrayFiles) {
NSMutableArray *arr = [NSMutableArray array];
NSArray *a = dict[@"files"];
for(int i=0; i < a.count; i ++) {
NSString *strName = [NSString stringWithFormat:@"%@",[[dict[@"files"] objectAtIndex:i] valueForKey:@"name"]];
[arr addObject:strName];
}
[arrayPDFName addObject:arr];
}
NSString *errorDesc;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory1 = [paths objectAtIndex:0];
NSString *plistPath = [documentsDirectory1 stringByAppendingPathComponent:@"SampleData.plist"];
NSString *error1;
returnData = [ NSPropertyListSerialization dataWithPropertyList:jsonResults format:NSPropertyListXMLFormat_v1_0 options:0 error:&error];
if(returnData ) {
if ([returnData writeToFile:plistPath atomically:YES]) {
NSLog(@"Data successfully saved.");
}else {
NSLog(@"Did not managed to save NSData.");
}
}
else {
NSLog(@"%@",errorDesc);
}
NSDictionary *stringsDictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];
}
编辑:
`- (void)viewDidLoad {
[super viewDidLoad];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
self.dataSourceForSearchResult = [NSArray new];
titleArray = [NSMutableArray array];
//Background Tasks
[self getdata];
dispatch_async(dispatch_get_main_queue(), ^(void){
//Run UI Updates
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
[self.collectionView reloadData];
self.navigationItem.hidesBackButton = YES;
});
});
}`
答案 0 :(得分:1)
你正在使用你需要的主线程获取你的数据,然后调用你需要的代码(我看到的是reload collectionView) 我假设因为你没有显示getdata方法代码 如果是这种情况,您可以使用此代码:
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
//Background Tasks
[self getdata];
dispatch_async(dispatch_get_main_queue(), ^(void){
//Run UI Updates
[self.collectionView reloadData];
});
});
这意味着您的VC会立即显示,但是在您完成加载数据后会收集collectionView,您可以在加载Facebook应用程序时添加一些旧数据(您可以看到最新检索的帖子直到完成加载)。
编辑: 在您的代码中,使用下一个代码替换nextController中的viewdidload方法:
- (void)viewDidLoad {
[super viewDidLoad];
//for search label data
self.dataSourceForSearchResult = [NSArray new];
//collection of array to store value
titleArray = [NSMutableArray array];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
//Background Tasks
[self getdata];
dispatch_async(dispatch_get_main_queue(), ^(void){
//Run UI Updates
[self.collectionView reloadData];
});
});
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
}