i m relatively new in Objective C, i have designed a server-client application and coded both server side and client side. I receive JSON objects after sending GETs to server and what i need is, the titles that come via JSON array(dictionary) be set as the titles of the buttons in my app’s view. I also want to create buttons dynamically, i mean if i reset some buttons the changes must come to my app again. Please can anyone help me about:
- How to create an UIButton array and set their title with content of
JSON dictionary? -
How to place the buttons in the view dynamically. I can share code
too if you want..button = 1;
key = 181abc88e57c37a42769;
message = (
{
ID = 1;
date = “2011-12-10 16:00:00”;
message = asdf;
status = 1;
“user_id” = 2;
}
);
“wrong_user” = 0;
This is result for the first get, and after defining its fariables i receive the JSON data below:
Response of Result2: (
{
ID = 1;
name = Ambulance;
},
{
ID = 2;
name = Police;
},
{
ID = 3;
name = "Somethings";
},
{
ID = 4;
name = Something Else;
}
)
what i need is to create some buttons in my view and set their titles with the names in this JSON data. The entire of my GET code is below, my variable names may not be so clear, sorry for that.. By the way, i count how many IDs come for button array, and i will use it when creating buttons, in order to set the buttons enabled-disabled ect.. Thanks again
//## GET code to here**
NSString *str1=[@"?username=" stringByAppendingString:userNameField.text];
NSString *str2=[@"&password=" stringByAppendingString:passwordField.text];
NSString *str3=[str1 stringByAppendingString:str2];
NSString *str4 =[@"http://" stringByAppendingString:serverField.text];
NSURL *url=[NSURL URLWithString:[str4 stringByAppendingString:[@"/ipad/login.php" stringByAppendingString:str3]]];
NSLog(@"%@\n",url);
//get the url to jsondata
NSData *jSonData=[NSData dataWithContentsOfURL:url];
if (jSonData!=nil) {
NSError *error=nil;
id result=[NSJSONSerialization JSONObjectWithData:jSonData options:
NSJSONReadingMutableContainers error:&error];
if (error==nil) {
NSDictionary *mess=[result objectForKey:@"message"];
NSDictionary *messContent=[mess valueForKeyPath:@"message"];
NSDictionary *messDate=[mess valueForKeyPath:@"date"];
NSDictionary *messID=[mess valueForKeyPath:@"ID"];
NSLog(@"%@ *** Message %@ \n Message Content: %@ \n Mesage ID: %@ \n Message Date: %@", result, mess, messContent, messID,messDate);
NSString*key1=[ result objectForKey:@"key" ];
NSString *s1=[@"http://" stringByAppendingString:serverField.text];
NSString *s2=[s1 stringByAppendingString:@"/ipad/button.php"];
NSURL *url2=[NSURL URLWithString:[s2 stringByAppendingString:[@"?key=" stringByAppendingString:key1]]];
NSLog(@"\n%@\n",url2 );
NSData *data2=[NSData dataWithContentsOfURL:url2];
id result2=[NSJSONSerialization JSONObjectWithData:data2 options:NSJSONReadingMutableContainers error:nil];
NSLog(@"\n\n\n******* ************ \n\n *********** ***********\n\nResponse of Result2: %@",result2);
NSArray *idd=[result2 valueForKeyPath:@"ID"];
NSString *iddName=[result2 valueForKeyPath:@"name"];
NSLog(@"-- %@ -- %@ -- \n The Count Of Buttons: %d", idd, iddName, [idd count]);
id sender;
[self performSegueWithIdentifier:@"sg1" sender:sender];
}
}
I still couldn’t get a result. It has a problem with [view addSubview:button], i declared it as UIViewController *view; and prepared its property but this time doesn’t accept having properties with UIViewContrpller. Please can someone give any idea, i googled the results too but couldn’t figure out anything. Both of the views belong to the same class, one view for user account and the other for the panel of the application(i mean the are where i want to place the buttons on)
This is how you create a button:
This is how you create an array and add buttons to it:
Can’t help you more unless you specify your JSON data format, but this should get you started.
edit
You can create a method with this content:
If you intend to call the method more than once in the same view controller lifecycle,
please add the following code in the beginning of the method.