resignFirstResponder Dosent work for tagged TextField

Am new to iOS and Xcode. Am working with a simple calculator. I have a table view in which i added 30 textFields through code. I assign them tags.

Then i made the keyboard for textField of the NumPad type. For doing so i needed to dismiss the keyboard using a toolbar.

But when i run the program the textField with the last tag is dismis the keyboard while the others don't do. I guess when i mention textField in my code it assigns textField of last tag.

My code is given.

for (int i = 0; i < 30; i++) {    
    mainTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, (80 + (i * 60)), 280, 30)];        
    mainTextField.borderStyle = UITextBorderStyleRoundedRect;
    mainTextField.textColor = [UIColor blackColor]; 
    mainTextField.font = [UIFont systemFontOfSize:17.0]; 
    mainTextField.backgroundColor = [UIColor whiteColor]; 
    mainTextField.autocorrectionType = UITextAutocorrectionTypeNo;   
    mainTextField.backgroundColor = [UIColor clearColor];
    mainTextField.keyboardType = UIKeyboardTypeNumberPad;
    mainTextField.keyboardType=UIKeyboardTypeDecimalPad;
    mainTextField.returnKeyType = UIReturnKeyDone;  
    mainTextField.tag= i+1;
    mainTextField.delegate=self;

       [self->mainTableView  addSubview:mainTextField];

       self. numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
       self. numberToolbar.barStyle = UIBarStyleBlackOpaque;
       self. numberToolbar.items = [NSArray arrayWithObjects:
                               [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)],
                               [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                               [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)],
                               nil];
       [self.numberToolbar setTag:i+1];
        [self.numberToolbar sizeToFit];

        mainTextField.inputAccessoryView = numberToolbar;


        mainTextField.clearButtonMode = UITextFieldViewModeWhileEditing;

    mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 55+(i*60), 280, 18)];
    mainLabel.backgroundColor = [UIColor clearColor];
    mainLabel.textAlignment = UITextAlignmentLeft;
    mainLabel.tag= i+1;


    {
        switch ([mainLabel tag]) {
            case 1:
                mainLabel.text=[ passedValue objectAtIndex:0];
                break;
            case 2:
                mainLabel.text=[ passedValue objectAtIndex:1];
                break;
            case 3:
                mainLabel.text=[ passedValue objectAtIndex:2];
                break;
            case 4:
                mainLabel.text=[ passedValue objectAtIndex:3]; 
                break;
            case 5:
                 mainLabel.text=[ passedValue objectAtIndex:4];  
                break;
            case 6:
                mainLabel.text=[ passedValue objectAtIndex:5]; 
                break;
            case 7:
                mainLabel.text=[ passedValue objectAtIndex:6]; 
                break;
            case 8:
                mainLabel.text=[ passedValue objectAtIndex:7]; 
                break;
            case 9:
                 mainLabel.text=[ passedValue objectAtIndex:8]; 
                break;
            case 10:
                 mainLabel.text=[ passedValue objectAtIndex:10]; 
                break;
            case 11:
                 mainLabel.text=[ passedValue objectAtIndex:11]; 
                break;
            case 12:
                mainLabel.text=[ passedValue objectAtIndex:12]; 
                break;
            case 13:
                 mainLabel.text=[ passedValue objectAtIndex:13]; 
                break;
            case 14:
                mainLabel.text=[ passedValue objectAtIndex:14]; 
                break;
            case 15:
                 mainLabel.text=[ passedValue objectAtIndex:15]; 
                break;
            case 16:
                 mainLabel.text=[ passedValue objectAtIndex:16]; 
                break;
            case 17:
                 mainLabel.text=[ passedValue objectAtIndex:17]; 
                break;
            case 18:
                 mainLabel.text=[ passedValue objectAtIndex:18];    
                break;
            case 19:
                 mainLabel.text=[ passedValue objectAtIndex:19]; 
                break;
            case 20:
                 mainLabel.text=[ passedValue objectAtIndex:20]; 
                break;
            case 21:
                 mainLabel.text=[ passedValue objectAtIndex:21]; 
                break;
            case 22:
                mainLabel.text=[ passedValue objectAtIndex:22]; 
                break;
            case 23:
               mainLabel.text=[ passedValue objectAtIndex:23]; 
                break;
            case 24:
               mainLabel.text=[ passedValue objectAtIndex:24]; 
                break;
            case 25:
                 mainLabel.text=[ passedValue objectAtIndex:25]; 
                break;
            case 26:
                mainLabel.text=[ passedValue objectAtIndex:26]; 
                break;
            case 27:
                mainLabel.text=[ passedValue objectAtIndex:27]; 
                break;
            case 28:
                 mainLabel.text=[ passedValue objectAtIndex:28]; 
                break;
            case 29:
                 mainLabel.text=[ passedValue objectAtIndex:29]; 
                break;

            default:
                break;
        }



    [self->mainTableView addSubview:mainLabel];
    [self configureView];

Answers


There are a couple of things wrong with this code.

First of all, this is not how you create a table with 30 text fields. Instead, you use a UITableView, set it's delegate and dataSource to your view controller and implement the required methods. Among others tableView:numberOfRowsInSection: where you'll return 30. Then in tableView:cellForRowAtIndexPath: you create a cell, put one text field into it and return the cell. There are tons of tutorials on the internet about how to set up a table view like this and you can also read the documentation by Apple. You almost never add any additional views to the tableView itself.

self->tableView is just wrong. The -> notation is used (among others) in C when you want to dereference a pointer to a struct and access or set one of it's variables. You don't use it with Objective-C objects. Simply write self.tableView. This should work if you declared your property correctly.

That switch block is unnecessary. Why don't you do this instead:

mainLabel.text = [passedValue objectAtIndex[mainLabel tag]-1];

Need Your Help

Cython setup.py to install compiled extension

python cython distutils

How do you write a setup.py to compile .pyx files in an arbitrary location and install the compiled code in another arbitrary location? For example dirA/spam.pyx to build/dirB/spam.so?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.