I have a class that uses sudzc. All works fine, with NSLog I can watch the data of my web services, but when I want to use an array and then use that array in other place, the property is null. I want to fill a table view with the data of the web services, but I can't, How could do it?
This is my class
//
// RootViewController.m
// StratargetMovil
//
// Created by Giovanni Cortés on 30/03/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "RootViewController.h"
#import "Page2.h"
@interface RootViewController ()
@end
@implementation RootViewController
@synthesize myData = _myData;
@synthesize empresaID = _empresaID;
@synthesize datos = _datos;
@synthesize idUnidadNegocio = _idUnidadNegocio;
@synthesize idArray = _idArray;
-(NSString *)empresaID
{
_empresaID = @"fce";
return _empresaID;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
// Do any additional setup after loading the view from its nib.
}
return self;
}
- (void)viewDidLoad
{
// I want to fill the table view but dont work
EWSEmpresaWebServiceSvc *service = [[EWSEmpresaWebServiceSvc alloc] init];
[service ConsultarUnidadesOrganizacionalesPorEmpresa:self EmpresaId:self.empresaID];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.datos = nil;
self.myData = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
// Lo de la tabla
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.myData count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [self.myData objectAtIndex:row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Muestra la vista 2 cuando se selecciona una fila
[[tableView cellForRowAtIndexPath:indexPath] setSelected:NO animated:YES];
self.idUnidadNegocio = [self.idArray objectAtIndex:indexPath.row];
Page2 *nextNavigator = [[Page2 alloc] init];
[[self navigationController] pushViewController:nextNavigator animated:YES];
[nextNavigator release];
}
// -------------------------------------------------
// Consultar unidades organizaciones por empresa
- (void) ConsultarUnidadesOrganizacionalesPorEmpresaHandler: (id) value {
// Handle errors
if([value isKindOfClass:[NSError class]]) {
NSLog(@"%@", value);
return;
}
// Handle faults
if([value isKindOfClass:[SoapFault class]]) {
NSLog(@"%@", value);
return;
}
// Do something with the NSMutableArray* result
NSMutableArray *result = (NSMutableArray*)value;
NSMutableArray *unidadOrganizacional = [[NSMutableArray alloc] init];
self.myData = [[[NSMutableArray array] init] autorelease];
for (int i = 0; i < [result count]; i++)
{
EWSUnidadNegocio *empresa = [[EWSUnidadNegocio alloc] init];
empresa = [result objectAtIndex:i];
[unidadOrganizacional addObject:[empresa Descripcion]];
}
self.myData = unidadOrganizacional;
}
-(void)onload:(id)value
{
// Handle errors
if([value isKindOfClass:[NSError class]]) {
NSLog(@"%@", value);
return;
}
// Handle faults
if([value isKindOfClass:[SoapFault class]]) {
NSLog(@"%@", value);
return;
}
// Do something with the NSMutableArray* result
NSMutableArray *result = (NSMutableArray*)value;
NSMutableArray *unidadOrganizacional = [[NSMutableArray alloc] init];
self.myData = [[[NSMutableArray array] init] autorelease];
for (int i = 0; i < [result count]; i++)
{
EWSUnidadNegocio *empresa = [[EWSUnidadNegocio alloc] init];
empresa = [result objectAtIndex:i];
NSLog(@"%ld -- %@", [empresa Id], [empresa Descripcion]); // With this I can watch the data in the console
[unidadOrganizacional addObject:[empresa Descripcion]];
}
self.myData = unidadOrganizacional; // self.myData in another place is null
}
}
@end
My data is
@property (nonatomic, retain) NSMutableArray *myData;
Thanks
Firstly, this is doing nothing for you:
self.myData = [[[NSMutableArray array] init] autorelease];
...
self.myData = unidadOrganizacional;
You are assigning myData then immediately reassigning it.
Skip the first initialization and just do this:
self.myData = [unidadOrganizacional mutableCopy];
I suspect you are losing the pointer to the unidadOrganizacional array at some point and that is why you are getting null for myData.
Personally though I would just do this initialize myData in the viewDidLoad call and then add the objects to myData directly:
- (void)viewDidLoad
{
self.myData = [[NSMutableArray alloc] init];
// I want to fill the table view but dont work
EWSEmpresaWebServiceSvc *service = [[EWSEmpresaWebServiceSvc alloc] init];
[service ConsultarUnidadesOrganizacionalesPorEmpresa:self EmpresaId:self.empresaID];
}
and then:
- (void) ConsultarUnidadesOrganizacionalesPorEmpresaHandler: (id) value {
// Handle errors
if([value isKindOfClass:[NSError class]]) {
NSLog(@"%@", value);
return;
}
// Handle faults
if([value isKindOfClass:[SoapFault class]]) {
NSLog(@"%@", value);
return;
}
// Do something with the NSMutableArray* result
NSMutableArray *result = (NSMutableArray*)value;
for (int i = 0; i < [result count]; i++)
{
EWSUnidadNegocio *empresa = [[EWSUnidadNegocio alloc] init];
empresa = [result objectAtIndex:i];
[self.myData addObject:[empresa Descripcion]];
}
}
-(void)onload:(id)value
{
// Handle errors
if([value isKindOfClass:[NSError class]]) {
NSLog(@"%@", value);
return;
}
// Handle faults
if([value isKindOfClass:[SoapFault class]]) {
NSLog(@"%@", value);
return;
}
// Do something with the NSMutableArray* result
NSMutableArray *result = (NSMutableArray*)value;
for (int i = 0; i < [result count]; i++)
{
EWSUnidadNegocio *empresa = [[EWSUnidadNegocio alloc] init];
empresa = [result objectAtIndex:i];
NSLog(@"%ld -- %@", [empresa Id], [empresa Descripcion]); // With this I can watch the data in the console
[self.myData addObject:[empresa Descripcion]];
}
}
You may want to clear out myData before the ConsultarUnidadesOrganizacionalesPorEmpresa
call, I'm not really sure what the different between that and onload
is really. It looks like they are duplicates of each other.
Good luck!