gpt4 book ai didi

ios - Objective c 在 View Controller 之间解析 XML 数据

转载 作者:行者123 更新时间:2023-11-29 12:13:33 26 4
gpt4 key购买 nike

我是 Objective-C 的新手,需要一些技巧来应对我的挑战。

我有 2 个 View Controller ,需要将从 FirstViewController 检索到的 xml 数据显示到 TermsViewController。

我成功获取了用户输入并检索了我需要的 xml 对象。但不知道如何在 TermsViewController.m 中显示用户名

由于数据是异步下载的,无法弄清楚如何为 IOS 6 实现这一点。

提前致谢。

FirstViewController.h

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIButton *accessButton;
@property (weak, nonatomic) IBOutlet UITextField *codeField;
@property (weak, nonatomic) NSString *codeUser;
@property (strong, nonatomic) NSString *nameUser;
@property (strong, nonatomic) NSDictionary *xmlDictionary;

@end

TermsViewController.h

#import <UIKit/UIKit.h>

@interface TermsViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (strong, nonatomic) NSString *nameUserTerms;

@end

FirstViewController.m

#import "FirstViewController.h"
#import "TermsViewController.h"
#import "XMLReader.h"


@interface FirstViewController ()

@property (nonatomic, strong) NSMutableURLRequest *postRequest;
@property NSUInteger responseStatusCode;
@property (nonatomic, strong) NSString *theXML;

@end

@implementation FirstViewController


- (void)viewDidLoad {
[super viewDidLoad];
}

- (IBAction)accessButton:(UIButton *)sender {
self.codeUser = self.codeField.text;

NSString *xmlCode = [NSString stringWithFormat:
@"<?xml version='1.0' encoding='utf-8'?>\n"
"<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>"
"<soap:Body>\n"
"<GetInterview xmlns='http://www.url.com/url.JCV'>\n"
"<Codigo>"
"%@"
"</Codigo>\n"
"</GetInterview>\n"
"</soap:Body>\n"
"</soap:Envelope>", self.codeUser];

NSLog(@"User code is: %@", self.codeUser);
NSLog(@"XML is: %@", xmlCode);

self.postRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.url.com/url.JCV/web.url.asmx"]];

[self.postRequest setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
[self.postRequest setHTTPMethod:@"POST"];
[self.postRequest setHTTPBody:[NSMutableData dataWithBytes:[xmlCode UTF8String] length:strlen([xmlCode UTF8String])]];

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:self.postRequest delegate:self];

if (conn) {
NSLog(@"Connected to: %@", conn);
} else {
NSLog(@"Connection Error");
}

[self.codeField resignFirstResponder];

}

FirstViewController.m connectionDidFinishLoading方法

- (void) connectionDidFinishLoading:(NSURLConnection *)connection {

if (self.responseStatusCode == 200) {

NSLog(@"Succeeded! Received %lu bytes of data",[self.theXML length]);

// Parse the XML into a dictionary
NSError *parseError = nil;

self.xmlDictionary = [XMLReader dictionaryForXMLString:self.theXML options:XMLReaderOptionsProcessNamespaces error:&parseError];
NSLog(@"%@", self.xmlDictionary);

//name of the candidate
self.nameUser = [[[[[[[self.xmlDictionary objectForKey:@"Envelope"] objectForKey:@"Body"] objectForKey:@"GetInterviewResponse"] objectForKey:@"GetInterviewResult"] objectForKey:@"Obj"] objectForKey:@"ProfissionalName"] objectForKey:@"text"];

NSLog(@"User name is: %@", self.nameUser);

TermsViewController *nv = [[TermsViewController alloc] init];
nv.nameUserTerms = self.nameUser;

//check
NSLog(@"User name stored: %@", nv.nameUserTerms);

[self performSegueWithIdentifier:@"goToTerms" sender:self];

}
else {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!"
message:@"bla bla."
delegate:self
cancelButtonTitle:@"Try again"
otherButtonTitles:nil];
[alert show];

}

TermsViewController.m

#import "TermsViewController.h"

@interface TermsViewController ()

@end

@implementation TermsViewController

- (void)viewDidLoad {
[super viewDidLoad];

self.nameLabel.text = self.nameUserTerms;

//this check is returning NULL
NSLog(@"User name: %@", self.nameUserTerms);

}

@end

最佳答案

您应该使用prepareForSegue 在 Controller 之间交换数据。

从您的代码中删除这些行:

TermsViewController *nv = [[TermsViewController alloc] init];
nv.nameUserTerms = self.nameUser;

然后将它们放在这样的方法中:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"goToTerms"]) {
TermsViewController *nv = segue.destinationViewController;
nv.nameUserTerms = self.nameUser;
}
}

关于ios - Objective c 在 View Controller 之间解析 XML 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32591094/

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