【发布时间】:2015-12-12 00:09:47
【问题描述】:
我是 Objective-c 的新手,需要一些技巧来应对我的挑战。
我有 2 个视图控制器,需要将从 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
【问题讨论】:
标签: ios objective-c xml nsxmlparser