Monday 22 June 2015

Dynamically Assign Selected Value Placed in TextField using Tag for iOS

DetailViewController.h
UIView *detail_view;
NSArray *functions;
int tag_selected;
UITextField *text_field;
UITableView *btn_dropdown;
UITableView *tbl_dropdown;


DetailViewController.m
-(void)viewDidLoad
{
detail_view = [[UIView alloc]init];

functions = [[NSArray alloc]initWithObjects:@"Buy Gold",@"Buy Silver",@"Buy Diamond",@"Golden day",@"Good Friday",@"Pongal", nil];
[tbl_dropdown reloadData];

int y_val = 60;
    for (int q = 0; q < functions.count; q++)
    {
        text_field = [[UITextField alloc]init];
        text_field.tag = q;
        text_field.frame = CGRectMake(13, y_val, 230, 40);
        UIView *paddingTextfield = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 42)];
        text_field.leftView = paddingTextfield;
        text_field.leftViewMode = UITextFieldViewModeAlways;
        text_field.placeholder = @"Enter Text";
        text_field.font = [UIFont fontWithName:@"Arial" size:18.0];
        text_field.textColor = [UIColor brownColor];
        text_field.borderStyle = UITextBorderStyleNone;
        text_field.returnKeyType = UIReturnKeyDone;
        text_field.delegate = self;
        text_field.backgroundColor = [UIColor whiteColor];
        text_field.layer.cornerRadius = 3;
        [detail_view addSubview:text_field];

btn_dropdown = [UIButton buttonWithType:UIButtonTypeCustom];
    btn_dropdown.tag = q;
    btn_dropdown.frame = text_field.frame;
    UIImage *btnImage= [UIImage imageNamed:@“cust_btn.png"];
    [btn_dropdown setImage:btnImage forState:UIControlStateNormal];
    [detail_view addSubview:btn_dropdown];
        
        y_val = y_val + 20;
    }
}

-(void)drop_table_clicked:(id)sender
{
    UIButton *btn = (UIButton *)sender;
    tag_selected = btn.tag;
tbl_dropdown.hidden = NO;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == tbl_dropdown)
    {
        return [functions count];
    }
return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cell_Identifier = @"cell_Identifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_Identifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cell_Identifier];
    }
    
    if (tableView == tbl_dropdown)
    {
        cell.textLabel.text = [functions objectAtIndex:indexPath.row];
}
return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == tbl_dropdown)
    {
        tbl_dropdown.hidden = YES;
        NSString *str_text = [functions objectAtIndex:indexPath.row];
        NSLog(@"%@", str_text);
        
        NSLog(@"%d", tag_selected);
        text_field = (UITextField *)[detail_view viewWithTag:tag_selected];
        text_field.text = str_text;
    }
}

No comments: