views:

552

answers:

1

how do i create more than 40-50 textfields and labels in a single view nad when the textfield selected the keyboard should not hide the textfield.?

+1  A: 

You would probably want to create them programmatically - using the Interface Builder to make 40-50 textfields would be pretty time-consuming.

As for the keyboard, you can make your main UIView scrollable, then whenever the keyboard is displayed, check what textfield is selected and scroll it to the upper half of the screen. (If your application is rotatable, make sure "the upper half of the screen" changes definitions depending on your orientation.)

Some sample code for this idea:

// Determine some basic info
int numberOfTextfields = 50;
int textfieldHeight = 40;
int textfieldWidth = 200;

// Create the UIScrollView
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:
                                   CGRectMake(0, 0, 
                                              numberOfTextfields*textfieldHeight,
                                              textfieldWidth)];

// Create all the textfields
NSMutableArray *textfields = [NSMutableArray arrayWithCapacity:
                                   (NSUInteger)numberOfTextfields];
for(int i = 0; i < numberOfTextfields; i++) {
    UITextField *field = [[UITextField alloc] initWithFrame:
                                CGRectMake(0,
                                           i*textFieldHeight,
                                           textFieldHeight,
                                           textFieldWidth)];
    [scrollView addSubview:field];
    [textfields addObject:field];
}

In this code, we first set some variables that determine the behavior of the textfields (their position, appearance, and number), then create the master UIScrollView. Once that's done, we create a bunch of UITextFields with the dimensions specified earlier, simultaneously adding them as subviews of the scrollview and holding them in an array for later reference (if need be).

Later, you'll want to override the becomeFirstResponder: method for your UITextFields (maybe subclass UITextField here) so that whenever a textfield becomes the first responder and shows the keyboard, it calls setContentOffset:animated: in the scrollview to display itself.

Tim
could you please post a sample code for creating something like this..hmmm suppose i have a view and the labels and textfields are situated likke in tableview cell text and accessaryview is situated.
Rahul Vyas
how do i show somthing like this http://www.bandago.com/iphone/settings.html
Rahul Vyas
You probably want to split that up into multiple screens - one for primary loan, one for secondary, and so on. You can easily create a single interface for each of those screens and then combine them with a UINavigationController. Once the user reaches the "end" of the list of options your last screen can present him/her with the results.
Tim
i want to have all in one view there is a way for this.but another problem is there.. i read on net in some articles to use UIScrollView and put labels and textfields as much as u want but in my interface builder i don't see any UIScrollView while in the tutorial there is.how do i get UIscrollView in my Interface BUilder.i have interface BUilder version 3.1...
Rahul Vyas