Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7017933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:58:42+00:00 2026-05-27T22:58:42+00:00

i m relatively new in Objective C, i have designed a server-client application and

  • 0

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:

  1. How to create an UIButton array and set their title with content of
    JSON dictionary?
  2. 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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:58:42+00:00Added an answer on May 27, 2026 at 10:58 pm

    This is how you create a button:

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    // set a handler to do something when the user presses the button
    [button addTarget:self 
               action:@selector(yourTouchDownHandler:)
     forControlEvents:UIControlEventTouchDown];
    
    // button title will come from the JSON data
    [button setTitle:@"this is the button title" forState:UIControlStateNormal];
    
    // you have to change the following line so that buttons do not overlap
    button.frame = CGRectMake(10.0, 10.0, 100.0, 44.0);
    
    [view addSubview:button]; // this is what makes your button show up on screen
    

    This is how you create an array and add buttons to it:

    NSMutableArray* buttons = [NSMutableArray array];
    [buttons addObject:button];
    

    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:

    self.buttons = [NSMutableArray array];
    
    CGFloat yPosition = 10.0f;
    const CGFloat buttonHeight = 44.0f;
    const CGFloat buttonMargin = 4.0f;
    
    for(NSDictionary* buttonData in result2) {
        NSString* buttonTitle = [buttonData objectForKey:@"name"];
    
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
        // set a handler to do something when the user presses the button
        [button addTarget:self 
                   action:@selector(yourTouchDownHandler:)
         forControlEvents:UIControlEventTouchDown];
    
        NSString* buttonTitle = [buttonData objectForKey:@"name"];
        [button setTitle:buttonTitle forState:UIControlStateNormal];
    
        button.frame = CGRectMake(10.0f, yPosition, 100.0f, buttonHeight);
    
        [view addSubview:button];
        [self.buttons addObject:button];
    
        yPosition+= buttonHeight + buttonMargin;
    }
    

    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.

    // remove old buttons
    for(UIButton* button in self.buttons) {
        [button removeFromSuperview];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm relatively new to objective-c...I'm using the iphone 3.0 SDK I have a UIView,
I am relatively new to Objective C and need some array help. I have
I'm relatively new to Objective-C and I have an enum with a corresponding array
I am relatively new to Objective-C and now I have a problem in my
I'm relatively new to the world of Objective-C and have a class that I've
I'm relatively new to Objective-C and really don't know much about it yet, so
Note: I'm relatively new to Objective-C and am coming from Java and PHP. Could
I'm relatively new to Objective-C + Quartz and am running into what is probably
I'm relatively new to web application programming so I hope this question isn't too
I'm relatively new to Objective C and while I've been trucking right along writing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.