我对Objective-C还是很陌生,需要一些技巧来应对挑战。

我有2个视图控制器,需要显示从FirstViewController检索到TermsViewController的xml数据。

我成功获取用户输入并检索了所需的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在控制器之间交换数据。

从代码中删除以下行:

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

07-28 06:29