NSXmlparser /变量问题。 - 变量不会被重置

时间:2011-06-20 12:55:24

标签: iphone objective-c cocoa-touch nsurlconnection nsxmlparser

我的一个变量存在问题,我似乎无法找到问题。问题是当用户登录时,很好,当用户注销时,它很好。然后是用户重新登录的时候.userId保持不变。

-(IBAction)postData:(id)sender
{      
    cmdLoginButton.hidden = YES;
    cmdLoginButton.enabled = NO;

    if(textName.text.length && textNumber.text.length > 0)
    {
        //[self performSelectorInBackground:@selector(sendData) withObject:nil];
        NSMutableData *data = [NSMutableData data]; 

        NSString *name = textName.text;
        NSString *number = textNumber.text;

        NSString *nameString = [[NSString alloc] initWithFormat:@"name=%@", name];
        NSString *numberString = [[NSString alloc] initWithFormat:@"&number=%@", number];
        NSString *genderString = [[NSString alloc] initWithFormat:@"&gender=%@", gender];

        //NSLog(nameString);
        //NSLog(numberString);

        [data appendData:[nameString dataUsingEncoding:NSUTF8StringEncoding]];
        [data appendData:[numberString dataUsingEncoding:NSUTF8StringEncoding]];
        [data appendData:[genderString dataUsingEncoding:NSUTF8StringEncoding]];

        NSURL *url = [NSURL URLWithString:@"http://www.directory.net/test.php"];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

        [request setHTTPMethod:@"POST"];
        [request setHTTPBody:data];

        NSURLResponse *response;
        NSError *err;
        NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
        NSLog(@"responseData: %@", responseData);

        userData = responseData;

        cmdLogoutButton.hidden = NO;
        cmdLogoutButton.enabled = YES;

        [self startParsingUserId];

    }
    else 
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Text Fields Empty" message:@"One Or More Textfields Are Empty" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
        [alert show];
        [alert release];

        cmdLoginButton.enabled = YES;
        cmdLoginButton.hidden = NO;
        cmdLogoutButton.enabled = NO;

    }


}

//*****************************START OF PARSER*************************

-(void)startParsingUserId;
{
    NSXMLParser *idParser = [[NSXMLParser alloc] initWithData:userData];
    idParser.delegate = self;
    [idParser parse];
    [idParser release];
}

-(void)parserDidStartDocument:(NSXMLParser *)parser
{
    currentElementName = nil;
    currentText = nil;
}

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if([elementName isEqualToString:@"usercallback"])
    {
        [currentIDDict release];
        currentIDDict = [[NSMutableDictionary alloc] initWithCapacity:[interestingTags count]];
    }
    else if([interestingTags containsObject:elementName])
    {
        currentElementName = elementName;
        currentText = [[NSMutableString alloc] init];
    }
}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    [currentText appendString:string];
}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if([elementName isEqualToString:currentElementName])
    {
        [currentIDDict setValue: currentText forKey: currentElementName];
    }
    else if([elementName isEqualToString:@"usercallback"])
    {
        [self.userArray addObject:currentIDDict];

    }

    NSLog(@"ending");
    [currentText release];
    currentText = nil;
}

-(void)parserDidEndDocument:(NSXMLParser *)parser
{
    NSLog(@"end");
    NSDictionary *rowData = [self.userArray objectAtIndex:0];
    NSString *userID = [[NSString alloc] initWithFormat:@"%@", [rowData objectForKey:@"id"]];
    self.userId = userID;

    NSLog(@"DONE PARSING DOCUMENT");
    NSLog(@"userid = %@", userId);
}

/*- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
    NSLog(@"Error on XML Parse: %@", [parseError localizedDescription] );
}*/

//*****************************END OF PARSER*************************

-(IBAction)logout:(id)sender
{
    NSString *userID = self.userId;

    NSMutableData *data = [NSMutableData data]; 

    NSMutableString *userString = [[NSMutableString alloc] initWithFormat:@"id=%@", userID];

    //NSLog(userString);
    //NSLog(numberString);

    [data appendData:[userString dataUsingEncoding:NSUTF8StringEncoding]];

    NSURL *url = [NSURL URLWithString:@"http://www.directory.net/offline.php"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:data];

    NSURLResponse *response;
    NSError *err;

    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
    NSLog(@"responseData: %@", responseData);

    cmdLoginButton.hidden = NO;
    cmdLogoutButton.hidden = YES;

    cmdLoginButton.enabled = YES;
    cmdLogoutButton.enabled = NO;

}

1 个答案:

答案 0 :(得分:1)

这种情况发生的可能性与下一行有关。

NSDictionary *rowData = [self.userArray objectAtIndex:0];

确保在注销方法中添加对[self.userArray removeAllObjects];的调用,我建议添加到登录方法但需要。