2010-10-26 93 views
2

我開始從viewDidLoad獲取數據並使用該數據填充NSMutableArray的過程。但是當我想填充UIPicker時,我不能這樣做,因爲數組中沒有更多的數據。我是怎麼迷失的?請幫助:(NSMutableArray填充但數據丟失

@synthesize activityIndicator; 
@synthesize pckCountries; 

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 

countriesList = [[NSMutableArray alloc] initWithCapacity:20]; 
[self getCountriesList]; 

NSLog(@"%@", [countriesList count]); 


[super viewDidLoad]; 

} 

- (void)dealloc { 
[activityIndicator release]; 
    [xmlParser release]; 
    //[soapResults release]; 

    [super dealloc]; 
} 

- (void) getCountriesList{ 


NSString *soapMsg = 
[NSString stringWithFormat: 
    @"<?xml version=\"1.0\" encoding=\"utf-8\"?>" 
    "<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>" 
    "<getCountries xmlns=\"http://www.smsbug.com/api/\" />" 
    "</soap:Body>" 
    "</soap:Envelope>" 
    ]; 

NSURL *url = [NSURL URLWithString: 
     @"http://www.smsbug.com/api/webservice.asmx"]; 
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; 
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]]; 
[req addValue:@"text/xml; charset=utf-8" 
forHTTPHeaderField:@"Content-Type"]; 
    [req addValue:@"http://www.smsbug.com/api/getCountries" 
forHTTPHeaderField:@"SOAPAction"]; 
    [req addValue:msgLength forHTTPHeaderField:@"Content-Length"]; 
    [req setHTTPMethod:@"POST"]; 
    [req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]]; 
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self]; 
    if (conn) { 
     webData = [[NSMutableData data] retain]; 


    } 
} 

-(void) connection:(NSURLConnection *) connection 
didReceiveResponse:(NSURLResponse *) response { 
[webData setLength: 0]; 
} 

-(void) connection:(NSURLConnection *) connection 
didReceiveData:(NSData *) data { 
[webData appendData:data]; 
NSLog(@"%@", webData); 
} 

-(void) connection:(NSURLConnection *) connection 
    didFailWithError:(NSError *) error { 
[webData release];  
[connection release]; 
} 

-(void) connectionDidFinishLoading:(NSURLConnection *) connection { 
    NSLog(@"DONE READING WEATHER WEB SERVICE. Received Bytes: %d", [webData length]); 
    NSString *theXML = [[NSString alloc] 
         initWithBytes: [webData mutableBytes] 
         length:[webData length] 
         encoding:NSUTF8StringEncoding]; 
    //---shows the XML to test--- 
    NSLog(theXML);  

    [theXML release]; 

// stop activity indicator animation 
    [activityIndicator stopAnimating];  


//----------------------------------------------------------------- 
// start parsing received XML message 
//----------------------------------------------------------------- 
if (xmlParser) 
{ 
    [xmlParser release]; 
} 
xmlParser = [[NSXMLParser alloc] initWithData: webData]; 
[xmlParser setDelegate:self]; 
[xmlParser setShouldResolveExternalEntities:YES]; 
[xmlParser parse]; 

// clear memory 
    [connection release]; 
    [webData release]; 
} 

-(void) parser:(NSXMLParser *) parser 
didStartElement:(NSString *) elementName 
    namespaceURI:(NSString *) namespaceURI 
qualifiedName:(NSString *) qName 
attributes:(NSDictionary *) attributeDict { 

//NSLog(elementName); 
if ([elementName isEqualToString:@"Country_Name"]) 
{ 
    countryFound = YES; 
} 
} 

-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string 
{  

    if([string isEqualToString:@"Data Not Found"]) 
    { 
    errorOccured = YES; 
    } 
    else if(countryFound == YES) 
    { 
    //NSLog(string); 
    [countriesList addObject:string]; 

    } 
    else 
    { 
    [soapResults appendString: string]; 
    }  
} 

-(void)parser:(NSXMLParser *)parser 
didEndElement:(NSString *)elementName 
namespaceURI:(NSString *)namespaceURI 
qualifiedName:(NSString *)qName 
{ 
if(errorOccured == YES) 
{ 
    UIAlertView *alert = [[UIAlertView alloc] 
     initWithTitle:@"No Data!"       
     message:@"Sorry" 
     delegate:self 
     cancelButtonTitle:@"OK" 
     otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
    [soapResults setString:@""]; 
    errorOccured = FALSE; 
} 
else 
{ 
    if ([elementName isEqualToString:@"Country_Name"]) 
    { 
    countryFound = FALSE; 
    } 
} 
} 

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ 
return 1; 
} 

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ 
return countriesList.count; 
} 

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ 
return [countriesList objectAtIndex:row]; 
} 

回答

1
  1. [super viewDidLoad]應該在viewDidLoad方法的第一次調用
  2. 有泄漏 - 你應該在dealloc方法調用[countriesList release]
  3. NSURLRequest是異步除了。 NSXMLParser所以所有的picker委託方法都會在下載/解析完成之前調用,因此,在所有的委託方法中,您應該檢查countriesList是否等於nil
  4. 解析完成時(實施parserDidEndDocument:方法來接收此消息),您應該明確地調用​​。
+0

1)完成 2)完成 3)我已經加入這樣的事情: 如果(countriesList ==無){ \t} \t否則{ \t返回[countriesList objectAtIndex:行]。 \t} in numberOfRowsInComponent,titleForRow。我添加了 - (void)parserDidEndDocument:(NSXMLParser *)parser { \t [pckCountries reloadAllComponents]; } 但仍在控制檯收到錯誤:完成讀取天氣網絡服務。收到的字節數:43784 編程接收信號:「EXC_BAD_ACCESS」。 kill quit – 1110 2010-10-26 09:21:24

+0

好的,我做了一些有用的事情。現在,當我進入parserDidEndDocument方法時,我的countryList中有222個對象。在這裏我檢查NIL,如果一切正常,我稱之爲「[pckCountries reloadAllComponents];」但沒有任何反應。我正在做一些事情:),任何想法? – 1110 2010-10-26 09:57:19

+0

人爲錯誤:)我沒有連接UIPickerView與IBOutlet變量。它的工作非常感謝。 – 1110 2010-10-27 06:56:00