gpt4 book ai didi

ios - 延迟查看下一个 View Controller ,查看帖子中的详细信息?

转载 作者:行者123 更新时间:2023-11-29 12:10:36 25 4
gpt4 key购买 nike

之后我有一个登录屏幕,它将移动到下一个 View Controller ,我已经使用了一些网络,如 http、json 从服务器获取数据。当我输入登录用户名/密码时,如果我单击登录按钮,它会延迟到 8 秒,之后只会移动到下一个 View Controller 。仍然我的登录屏幕单独显示 8 秒,然后只移动到下一个 View Controller 。

这是我的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] 中,我正在从服务器 url 获取数据。谢谢

我的获取数据:

-(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;

});

});

}`

最佳答案

您正在使用主线程获取数据,您需要在后台执行此操作,然后调用您需要的代码(我看到的是重新加载 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;

}

关于ios - 延迟查看下一个 View Controller ,查看帖子中的详细信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33544517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com