Как создать Dynami c Textfield, Checkbox и Picker и добавить все элементы управления пользовательского интерфейса в scrollView в Objective- C - PullRequest
0 голосов
/ 07 мая 2020

Как создать Dynami c Textfield, Checkbox и Picker и добавить все UIcontrols в scrollView
Мои требования:
Мне нужно создать свое представление, как показано ниже:
enter image description here
Ниже мой ответ API:

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

Реализация моего кода:
На данный момент я создал два UIViews с использованием nib.
1) CustomTextField
2) CustomCheckBox
Я реализовал тот же код в обоих файлах представления .m, только loadNibNamed изменилось.

- (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;
}  

Ниже моя реализация My ViewController:

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;
                }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...