mercredi 6 mai 2020

How to Create Dynamic Textfield, Checkbox and Picker and add all the UI Controls in scrollView in Objective-C

How to Create Dynamic Textfield,Checkbox and Picker and add all the UIcontrols in scrollView
My Requirement:
I need to design my view like below:
enter image description here
Below is my API Response:

dictCustomFieldGroups: (
        {
        name = Name;
        showtype = mandatory;
        type = "Normal Text";
    },
        {
        name = Age;
        showtype = mandatory;
        type = "Short Text";
    },
        {
        name = Color;
        showtype = optional;
        type = Dropdown;
        values =         (
            Red,
            Green,
            Blue
        );
    },
        {
        name = Symptoms;
        showtype = optional;
        type = Checkbox;
        values =         (
            Cough,
            Cold,
            Fever
        );
    } )  

My code Implementation:
Currently I have created two UIViews using nib.
1) CustomTextField
2) CustomCheckBox
I have implemented the same code in both the views .m files, only loadNibNamed has changed.

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // No need to re-assign self here... owner:self is all you need to get
        // your outlet wired up...
        UIView* xibView = [[[NSBundle mainBundle] loadNibNamed:@"CustomCheckBox" owner:self options:nil] objectAtIndex:0];
        // now add the view to ourselves...
        [xibView setFrame:[self bounds]];
        [self addSubview:xibView]; // we automatically retain this with -addSubview:
    }
    return self;
}  

Below is my My ViewController's Implementation:

self->dictCustomFieldGroups = [[NSDictionary alloc] init];
                self->dictCustomFieldGroups = [resJson valueForKeyPath:@"detail.customfieldgroups"];
                NSLog(@"dictCustomFieldGroups: %@",self->dictCustomFieldGroups);
                if ([self->dictCustomFieldGroups count] > 0) {
                    float height = 0;
                    for (int i = 0; i < self->dictCustomFieldGroups.count; i++) {
                        //NSLog(@"name:%d  %@",i,[[self->dictCustomFieldGroups valueForKey:@"name"] objectAtIndex:i]);
                       //NSLog(@"showtype:%d %@",i,[[self->dictCustomFieldGroups valueForKey:@"showtype"] objectAtIndex:i]);
                        //NSLog(@"type:%d %@",i,[[self->dictCustomFieldGroups valueForKey:@"type"] objectAtIndex:i]);
                        if ([[[self->dictCustomFieldGroups valueForKey:@"type"] objectAtIndex:i] isEqualToString:@"Normal Text"] || [[[self->dictCustomFieldGroups valueForKey:@"type"] objectAtIndex:i] isEqualToString:@"Short Text"]) {
                            CustomTextField *myCustomView = [[CustomTextField alloc] initWithFrame:CGRectMake(0, i*90 + 10, 374, 90)];
                            myCustomView.layer.borderColor = [UIColor lightGrayColor].CGColor;
                            myCustomView.layer.borderWidth = 1.0f;
                            myCustomView.layer.cornerRadius = 5;
                            myCustomView.layer.masksToBounds = true;
                            myCustomView.lblTitle.text = [VizsafeCommonUtil trimWhiteSpaceAndNewLine:[[self->dictCustomFieldGroups valueForKey:@"name"] objectAtIndex:i]];
                            myCustomView.txtValue.placeholder = [NSString stringWithFormat:@"Enter the %@",[VizsafeCommonUtil trimWhiteSpaceAndNewLine:[[self->dictCustomFieldGroups valueForKey:@"name"] objectAtIndex:i]]];
                            myCustomView.txtValue.returnKeyType = UIReturnKeyDone;
                            myCustomView.txtValue.tag = i + 1;
                            myCustomView.txtValue.delegate = self;
                            [self.scView insertSubview:myCustomView atIndex:i];
                            height += 90;
                        } else if ([[[self->dictCustomFieldGroups valueForKey:@"type"] objectAtIndex:i] isEqualToString:@"Dropdown"]) {

                        } else if ([[[self->dictCustomFieldGroups valueForKey:@"type"] objectAtIndex:i] isEqualToString:@"Checkbox"]) {
                            CustomCheckBox *myCustomView = [[CustomCheckBox alloc] initWithFrame:CGRectMake(0, height + 10, 374, 90)];
                            myCustomView.layer.borderColor = [UIColor lightGrayColor].CGColor;
                            myCustomView.layer.borderWidth = 1.0f;
                            myCustomView.layer.cornerRadius = 5;
                            myCustomView.layer.masksToBounds = true;
                            myCustomView.lblTitle.text = [VizsafeCommonUtil trimWhiteSpaceAndNewLine:[[self->dictCustomFieldGroups valueForKey:@"name"] objectAtIndex:i]];
                            NSArray *arrChecks = [[self->dictCustomFieldGroups valueForKey:@"values"] objectAtIndex:i];

                            /*for (NSString *strValue in arrChecks) {
                                 NSLog(@"strValue: %@",strValue);
                                myCustomView.lblValue.text = strValue;
                            }
                            [myCustomView.btnCheckbox setBackgroundImage:[UIImage imageNamed:@"checkbox-checked-white"]forState:UIControlStateSelected];

                            //myCustomView.txtValue.tag = i + 1;

                            [self.scView insertSubview:myCustomView atIndex:i];
                            height += 90;*/


                            CGFloat staticX         = 5;    // Static X for all buttons.
                            CGFloat staticWidth     = 60;   // Static Width for all Buttons.
                            CGFloat staticHeight    = 56;   // Static Height for all buttons.
                            CGFloat staticPadding   = 5;

                            for (int j = 0; j < [arrChecks count]; j++)
                            {
                                self->settButton = [UIButton buttonWithType:UIButtonTypeCustom];
                                [self->settButton setTag:j];
                                [self->settButton setFrame:CGRectMake((staticX + (j * (staticHeight + staticPadding))),5,staticWidth,staticHeight)];
                                [self->settButton setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%i.png",j]] forState:UIControlStateNormal];
                                [self->settButton addTarget:self action:@selector(ChkUnChk:) forControlEvents:UIControlEventTouchDown];
                                [myCustomView insertSubview:self->settButton atIndex:j];
                                //[myCustomView addSubview: self->settButton];
                            }
                            //[self.scView addSubview: myCustomView];
                            [self.scView insertSubview:myCustomView atIndex:i];
                                                       height += 90;
                        }
                    }
                    self->_scView.contentSize = CGSizeMake(self.scParentView.frame.size.width, height);
                    self.scView = [[UIScrollView alloc] initWithFrame:self.scParentView.frame];
                    // now add our scroll view to the main view
                    [self->_scParentView addSubview:self.scView];
                } else {
                    self.scParentView.hidden = TRUE;
                }



Aucun commentaire:

Enregistrer un commentaire