我在视图控制器中显示视图时遇到了一些问题。我在视图控制器中创建了一个自定义视图,当响应为0或获得正确响应时显示。制作两个自定义视图。当响应变为0时,它显示view1,其中用户单击注册按钮和用户进入注册页面,当响应获得值时,它应显示其获取引脚的view2。现在我正在尝试显示视图,但我没有得到它。我试过的代码就是这个,
NSString *string1 = _Mobile.text;
NSLog(@"ID is %@",string1);
NSString *string2=@"&action=pin";
NSString *urll=@"MyURL";
NSString *string3 = [urll stringByAppendingString:string1];
NSString *final=[string3 stringByAppendingString:string2];
NSLog(@"URL %@",final);
_indicator.hidden=NO;
[_indicator startAnimating];
NSString *targetUrl = [NSString stringWithFormat:@"%@",final];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"GET"];
[request setURL:[NSURL URLWithString:targetUrl]];
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:
^(NSData * _Nullable data,
NSURLResponse * _Nullable response,
NSError * _Nullable error) {
[_indicator stopAnimating];
_indicator.hidden=YES;
NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Data received: %@", myString);
if([myString intValue] == 0){
[self RegAlert];
}else if ([myString intValue]==response){
[self PinAlert];
}
}] resume];
我写的这个函数就是这个,
-(void)RegAlert{
_regView.hidden=NO;
[self.regView bounceIntoView:self.view direction:DCAnimationDirectionLeft
}
-(void)PinAlert{
_pinView.hidden=NO;
[self.pinView bounceIntoView:self.view direction:DCAnimationDirectionLeft];
}
我的服务响应来自于此0
{ “数据”: “0”}
当它正确时,它会显示此引脚
{ “数据”:[{ “销”: “5583”}]}
答案 0 :(得分:0)
在dispatch_async block
之前为json添加此代码 NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:nil];
NSString *value = json[@"data"];
像这样修改您的ui更新:
dispatch_async(dispatch_get_main_queue(), ^{
if([value isEqualToString: @"0"]){
[self RegAlert];
}else {
[self PinAlert];
}
});
注意:UI更新应该在主队列上运行,希望它可以帮助你
-(void)RegAlert{
pinView.hidden=YES;
_regView.hidden=NO;
[self.regView bounceIntoView:self.view direction:DCAnimationDirectionLeft];
}
-(void)PinAlert{
_regView.hidden=YES;
_pinView.hidden=NO;
[self.pinView bounceIntoView:self.view direction:DCAnimationDirectionLeft];
}
答案 1 :(得分:0)
在对其执行任何处理之前,您需要隐藏两个视图。因此,当您尝试显示时,它应该只显示一个视图。
以下是代码中的一些小修改:
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:
^(NSData * _Nullable data,
NSURLResponse * _Nullable response,
NSError * _Nullable error) {
[_indicator stopAnimating];
_indicator.hidden=YES;
NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Data received: %@", myString);
// Here we need to hide both view initial
_regView.hidden=NO;
_pinView.hidden=NO;
if([myString intValue] == 0){
[self RegAlert];
}else if ([myString intValue]==response){
[self PinAlert];
}
}] resume];
在视图上执行任何动画或处理之前,您需要添加以下两行。
// Here we need to hide both view initial
_regView.hidden=NO;
_pinView.hidden=NO;