ios - 如何通过从服务器获取数据来设置文本字段值

标签 ios objective-c iphone web-services nsxmlparser

如何通过从服务器获取数据来设置文本文件的值

我正在从服务器获取 xml 数据,但无法将这些值设置为文本字段。如果下面的代码有任何问题,你能纠正我吗

Customerdetails.h(模型)

@interface Customerdetails : NSObject
@property(nonatomic,strong)NSString*fst;
@property(nonatomic,strong)NSString*lst;
@property(nonatomic,strong)NSString*street;
@property(nonatomic,strong)NSString*city;

@end

detailviewcontroller.m

#import "DetailViewController.h"
#import "Customerdetails.h"
@interface DetailViewController ()<NSXMLParserDelegate>
@property(nonatomic,strong)NSXMLParser*xmlparse;
@property(nonatomic,strong)NSMutableString*tempstr;
@property(nonatomic,strong)NSMutableString*foundvalue;
@property Customerdetails*csd;
@end

@implementation DetailViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _csd=[[Customerdetails alloc]init];
    NSMutableURLRequest*req=[[NSMutableURLRequest alloc]init];
    _tempstr=[[NSMutableString alloc]init];
    NSURL*url=[NSURL URLWithString:@"http://www.thomas-bayer.com/sqlrest/CUSTOMER/4"];
    [req  setURL:url];
    [[[NSURLSession sharedSession]dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        // SMXMLDocument *document = [SMXMLDocument documentWithData:data error:&error];

        NSString*str=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        _xmlparse=[[NSXMLParser alloc]initWithData:data];
        _xmlparse.delegate=self;
        [_xmlparse parse];
    }] resume];
    dispatch_async(dispatch_get_main_queue(), ^{
        self.first.text=[self.csd fst];
        self.last.text=[self.csd lst];
        self.street.text=[self.csd street];
        self.city.text=[self.csd city];
    });

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(NSDictionary<NSString *, NSString *> *)attributeDict;{

    _tempstr=[[NSMutableString alloc]initWithString:elementName];

}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName;{
    // sent when an end tag is encountered. The various parameters are supplied as above.
    if([self.tempstr isEqualToString:@"FIRSTNAME"]){
        [self.csd setFst:_foundvalue];
        self.foundvalue=nil;
        self.tempstr=nil;
    }
    if([self.tempstr isEqualToString:@"LASTNAME"]){
        [self.csd setLst:_foundvalue];
        self.foundvalue=nil;
        self.tempstr=nil;
    }
    if([self.tempstr isEqualToString:@"STREET"]){
        [self.csd setStreet:_foundvalue];
        self.foundvalue=nil;
        self.tempstr=nil;
    }

    if([self.tempstr isEqualToString:@"CITY"]){
        [self.csd setCity:_foundvalue];
        self.foundvalue=nil;
        self.tempstr=nil;
    }


    if([elementName isEqualToString:@"CUSTOMER"]){
        NSLog(@"%@",self.csd);
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{

    if([self.tempstr isEqualToString:@"FIRSTNAME"]){
        self.foundvalue=[[NSMutableString alloc]initWithString:string];
    }
    if([self.tempstr isEqualToString:@"LASTNAME"]){
        self.foundvalue=[[NSMutableString alloc]initWithString:string];
    }
    if([self.tempstr isEqualToString:@"STREET"]){
        self.foundvalue=[[NSMutableString alloc]initWithString:string];
    }

    if([self.tempstr isEqualToString:@"CITY"]){
        self.foundvalue=[[NSMutableString alloc]initWithString:string];
    }
}

@结束

最佳答案

您甚至在设置对象 self.csd 之前就设置了标签的文本。只有在解析完成后才会设置该对象。

[[[NSURLSession sharedSession]dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    // SMXMLDocument *document = [SMXMLDocument documentWithData:data error:&error];

    NSString*str=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    _xmlparse=[[NSXMLParser alloc]initWithData:data];
    _xmlparse.delegate=self;
    [_xmlparse parse];
    }] resume];
//This part will get executed even before the parsing is done.
// So self.csd is nil and so the label are getting empty string.
dispatch_async(dispatch_get_main_queue(), ^{
    self.first.text=[self.csd fst];
    self.last.text=[self.csd lst];
    self.street.text=[self.csd street];
    self.city.text=[self.csd city];
    });

现在我们将在解析 xml 后立即设置标签和文本,但要小心,因为这需要时间,因为它在后台线程中,并且 UI 可能在一段时间内没有变化。

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName;{
    // sent when an end tag is encountered. The various parameters are supplied as above.
    if([self.tempstr isEqualToString:@"FIRSTNAME"]){
        [self.csd setFst:_foundvalue];
        //We are doing the UI stuff in main thread.
        dispatch_async(dispatch_get_main_queue(), ^{
            self.first.text = [self.csd fst];

        });
        self.foundvalue=nil;
        self.tempstr=nil;
    }
    ...

现在执行此操作,您就可以为其他处理准备好 self.csd,并且 UI 也已更新。

关于ios - 如何通过从服务器获取数据来设置文本字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40866679/

相关文章:

ios - 带分页的水平 ScrollView 中的全宽标签

iphone - 理解 Objective C 中的委托(delegate)模式和回调函数的问题

ios - 根据字典键过滤 NSDictionary 的 NSArray

iPhone更改滑动删除按钮标题

html - 在 <li> 中选择菜单适用于 ios chrome 但不适用于 ios safari

ios - 当 Web Inspector 和日志没有显示任何内容时,如何调试崩溃的 iOS Safari?

ios - 选择 iOS 测试框架

javascript - 使用 Swift 检测 WebView URL 的变化

iphone - UIViewController 初始化和加载的顺序

ios - 从远程推送通知打开 View 时后退按钮消失