Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 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 Reply

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);

}
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...