UIAlertView尚未启动

时间:2010-12-13 10:28:19

标签: iphone objective-c cocoa-touch xcode html-parsing

我正在尝试使用HTMLParser解析HTML内容,并借助它我尝试启动UIAlertView,应用程序运行正常,但不会启动UIAlertView。

以下是代码:

- (IBAction) loginButton: (id) sender
{

// Create the username and password string.
// username and password are the username and password to login with
NSString *postString = [[NSString alloc] initWithFormat:@"username=%@&password=%@",userName, password];
// Package the string in an NSData object
NSData *requestData = [postString dataUsingEncoding:NSASCIIStringEncoding];

// Create the URL request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:@"http://localhost/dologin.php"]];  // create the URL request
[request setHTTPMethod: @"POST"];   // you're sending POST data
[request setHTTPBody: requestData];  // apply the post data to be sent

// Call the URL
NSURLResponse *response;  // holds the response from the server
NSError *error;   // holds any errors
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&error];  // call the URL

/* If the response from the server is a web page, dataReturned will hold the string of the HTML returned. */

HTMLParser * parser = [[HTMLParser alloc] initWithData:returnData error:&error];

HTMLNode * bodyNode = [parser body];

NSArray * errorNodes = [bodyNode findChildTags:@"errorbox"];

for (HTMLNode * errorNode in errorNodes) {
    if ([[errorNode getAttributeNamed:@"div class"] isEqualToString:@"errorbox"]){
        alertWithOkButton = [[UIAlertView alloc] initWithTitle:@"Status..." message:[NSString stringWithFormat:@"Invalid Access Info, try again"] delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil];
        [alertWithOkButton show];
        [alertWithOkButton release];
        //Login Failed
    }
}

NSArray * spanNodes = [bodyNode findChildTags:@"clientarea.php?action=masspay"];

for (HTMLNode * spanNode in spanNodes) {
    if ([[spanNode getAttributeNamed:@"action"] isEqualToString:@"clientarea.php?action=masspay"]){
        alertWithOkButton = [[UIAlertView alloc] initWithTitle:@"Status..." message:[NSString stringWithFormat:@"Login Accepted, redirecting to the main app screen. :)"] delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:@"Go",nil];
        [alertWithOkButton show];
        [alertWithOkButton release]; //Login Success
    }
}

[parser release];

}

1 个答案:

答案 0 :(得分:0)

你的AlertViews似乎是正确形成的(虽然你不需要[NSString stringWithFormat:]的东西,因为你实际上并没有格式化任何东西 - 只是@“你的消息”东西很好)。 / p>

因为它们很好,这告诉我们使它们出现的条件永远不会发生。您的isEqualToString比较都不是true或者errorNodes和spanNodes都是空的,或者这些东西的某种组合。

单击第一个 for 语句旁边的并设置断点。构建和调试,让程序运行直到到达断点。现在,您可以检查并查看errorNodes和spanNodes实际包含的内容。