Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

button - how to add values from nsstrings in iOS?

I have 5 buttons. Each button has some value like 1,2,3,4,5 in a string named totalSeats_selected.
I want to save them into a nsstringthat shows result like this:
nsstring * result = [1,2,3,4,5];

These values are to used as request for a web service, so it must be in this format to send total number of seats?

So, how to save all these values in nsstring on button event?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use like this.Suppose your string is in integer means use Method 1 .otherwise use Method 2(If your value assigned in NSString).

 Method 1:



- (IBAction)Savebtnpressed:(id)sender {

NSMutableDictionary *datadict=[NSMutableDictionary dictionary];


[datadict setValue:[NSNumber numberWithInt:[first intValue]] forKey:@"1"];

[datadict setValue:[NSNumber numberWithInt:[second intValue]] forKey:@"2"];

[datadict setValue:[NSNumber numberWithInt:[third intValue]] forKey:@"3"];

[datadict setValue:[NSNumber numberWithInt:[fourth intValue]] forKey:@"4"];

[datadict setValue:[NSNumber numberWithInt:[fifth intValue]] forKey:@"5"];

NSData* jsonData = [NSJSONSerialization dataWithJSONObject:datadict options:kNilOptions error:nil];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSURL *someURLSetBefore =[NSURL URLWithString:@" url "];
[request setURL:someURLSetBefore];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
//    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
  //    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:jsonData];
NSError *error;
NSURLResponse *response;
NSData *responseData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString * string1=[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"%@",string1);

 }
   }

   Method 2:


    - (IBAction)Savebtnpressed:(id)sender {

NSMutableDictionary *datadict=[NSMutableDictionary dictionary];

 [datadict setObject:[NSString stringWithFormat:@"%@",FIRST] forKey:@"1"];

[datadict setObject:[NSString stringWithFormat:@"%@",SECOND] forKey:@"2"];

   [datadict setObject:[NSString stringWithFormat:@"%@",THIRD] forKey:@"3"];

[datadict setObject:[NSString stringWithFormat:@"%@",FOURTH] forKey:@"4"];

 [datadict setObject:[NSString stringWithFormat:@"%@",FIFTH] forKey:@"5"];


NSData* jsonData = [NSJSONSerialization dataWithJSONObject:datadict options:kNilOptions error:nil];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSURL *someURLSetBefore =[NSURL URLWithString:@" url "];
[request setURL:someURLSetBefore];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  //    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
  //    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:jsonData];
NSError *error;
NSURLResponse *response;
NSData *responseData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString * string1=[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"%@",string1);

}
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...