tags:

views:

13

answers:

1

I read a plist data at LightTableViewController.m

and I load a data like this :

    LOG RESULT:   
The Plist Data Is : (
        {
        category = 11;
        id = 1;
        name = "LIVING RM";
        status = 0;
    },
        {
        category = 11;
        id = 2;
        name = "BEDROOM RM";
        status = 0;
    }
)

I need to post the "id" and "status" back to the database

to control which light to turn on or turn off

And this part is the my post method,It's in LightCell0.m

- (void)switchToggled:(id)sender {

    UISwitch *theSwitch = (UISwitch *)sender;
    UITableViewCell *cell = (UITableViewCell *)theSwitch.superview.superview;
    UITableView *tableView = (UITableView *)cell.superview;
    NSIndexPath *indexPath = [tableView indexPathForCell:cell];



    if(theSwitch.on) {
        NSURL * url;
        NSMutableURLRequest * request;  
        NSString *_urlString = @"http://10.85.28.99/req_light.php"; 
        url = [self smartURLForString:_urlString];
        request = [NSMutableURLRequest requestWithURL:url];
        [request setHTTPMethod:@"POST"];


            //This is my first post method,it is static,only get the indexPath,Not a real id and status I want to post back 
        NSString *post = [NSString stringWithFormat:@"lightcb%i=1", indexPath.row+1];
        NSData *postData = [ NSData dataWithBytes: [ post UTF8String ] length: [ post length ] ];
        [request setHTTPBody:postData];
        self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
    } 
    else {
        NSURL * url;
        NSMutableURLRequest * request;
        NSString *_urlString = @"http://10.85.28.99/req_light.php";
        url = [self smartURLForString:_urlString];
        request = [NSMutableURLRequest requestWithURL:url];
        [request setHTTPMethod:@"POST"];


            //I got one error and one warring 
        //Error is "Request for member 'plist' in something not a structure or union"
            //Warning is 'NSString' may not resopnd to '+stringWithFormat:value=ForKey'
        NSString *post = [ NSString stringWithFormat:@"id=%i,status=0", 
                                  [[self.plist objectAtIndex:indexPath.row] valueForKey:@"id"]];
        NSData *postData = [ NSData dataWithBytes: [ post UTF8String ] length: [ post length ] ];
        [request setHTTPBody:postData];
        self.connection = [NSURLConnection connectionWithRequest:request delegate:self];

    }
}

So...my question is

<1>How to post two data back (is it right to use "," to separated two return variables ?

<2>How to eliminate the error "Request for member 'plist' in something not a structure or union"

Great Thanks

A: 

1) POST values are separated by '&' like GET values in an URL.

2) The 'Request for member'... line tells you that your member does not exist, or at least is not declared in this scope, the warning 'NSString may not respond...' tells you you're trying to invoke a message/method on NSString which should be invoked on another class (NSDictionary would be my guess here).

jv42
I add this to lightCell0:NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://10.85.28.99/req_light.php"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];NSData *returnData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil]; NSString *listFile = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding]; self.plist = [listFile propertyList];
WebberLai
but I got [[self.plist objectAtIndex:indexPath.row] valueForKey:@"id" is 97534704 or a random number......is not the real ID I define.
WebberLai
Oh I see ,I forget to add intValue
WebberLai