• Objective c Parsing XML data between view controllers

    From =?UTF-8?B?4KSR4KS44KWN4KSV4KSwIOCks@21:1/5 to All on Fri Sep 25 07:39:04 2015
    This is the issue:

    In FirstViewController, when you did:

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

    Notice the flow, the viewDidLoad is getting called before the nameUserTerms is being assigned.

    There are two solutions:
    1. Use NSUserDefaults -or-
    2. pass the name as a constructor argument
    e.g. in TermsViewController
    TermsViewController.h
    - (instancetype) initWithUserName:(NSString *const)name;

    TermsViewController.m
    - (instancetype) initWithUserName:(NSString *const)name
    {
    if( self = [super init]) {
    self -> nameUserTerms = [name copy];
    }
    return self;
    }

    so in the FirstViewController your code now changes to
    TermsViewController *nv = [[TermsViewController alloc] initWithUserName:self.nameUser];

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ronaldo Bahia@21:1/5 to All on Tue Sep 15 09:30:13 2015
    I'm pretty new to objective-c and need some tips for my challenge.

    I have 2 view controllers and need to show xml data retrieved from FirstViewController to the TermsViewController.

    I'm successful getting user input and retrieve xml objects I need. But don't know how to show the user name in the TermsViewController.m

    Since data is downloaded async, can't figure out how to implement this for IOS 6.

    Thanks in advance.

    The code is in http://stackoverflow.com/questions/32591094/objective-c-parsing-xml-data-between-view-controllers and here as well:

    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 method

    - (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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)