views:

223

answers:

2

i created a UITextField and a custom View. after that i added this custom view as "inputView" to my UITextField.

 amountKeyboardViewController *amountKeyboard = [[amountKeyboardViewController alloc] initWithNibName:@"amountKeyboardViewController" bundle:nil];

amountTextField.inputView = amountKeyboard.view;

[amountTextField becomeFirstResponder];

this works quite fine. but can i fill my "amountTextField" with the input clicked in my custom View (UIButtons)?

thanks for some hints !

+1  A: 

Hi,

You mean that if you press 1 then '1' should shown in textfield.

after that if you press 2 then it will show '12'.

Do you want like this?

You have to write IBaction for all buttons eg.

-(IBAction)clickof1
{
    if([amountTextField.text length] <= 0)
       amountTextField.text = @"1";
    else
       amountTextField.text = [amountTextField.text stringByAppendingString:@"1"];
}

-(IBAction)clickof2
{
    if([amountTextField.text length] <= 0)
       amountTextField.text = @"2";
    else
       amountTextField.text = [amountTextField.text stringByAppendingString:@"2"];
}

like that you have to write IBaction for all your buttons

Reena
yes i thought of that, but: my actions are in my amountKeyboardViewController and not in my controller where my textfield is created. also what if i want to use this keyboard somewhere else? do i always have to write all my actions for each textfield?
choise
you need to create property of your textbox and you can access that from amountKeyboardViewController class by using forward declaration and you can set the value
Reena
oh please can you give me a quick sample code about this "forward declaration" in your post above? google stuff looks weird
choise
+1  A: 

in your amountTextField controller you need to write the following code in .h file

create property of your textfield

@property(nonatomic,retain)UiTextField *txt; //whatever your textfield name

Now in .m file write the following

@synthesize txt;

write following statement where you create object of amountKeyboardViewController

 amountKeyboardViewController *amountKeyboard = [[amountKeyboardViewController alloc] initWithNibName:@"amountKeyboardViewController" bundle:nil]
 amountKeyboard.objamountTextField = self;

Now in amountKeyboardViewController controller write the following code

@class amountTextField;       //forward declaration

then create object of this class

amountTextField *objamountTextField;

then create property of

@property(nonatomic,retain)amountTextField *objamountTextField;

Now on click of button

-(IBAction)clickof1
{
    if([amountTextField.text length] <= 0)
        objamountTextField.txt.text = @"1";
    else
        objamountTextField.txt.text=[objamountTextField.txt.text stringByAppendingString:@"1"];
}
Reena
i'll give it a try this evening and accept your answer if its working. thanks for your great support. two things: "amountTextField controller" do you mean my view controller where my amountTextField is setted as an instance variable? `amountKeyboard.objamountTextField = self;` can you give me a quick explanation of that?
choise
Yes,amountTextField controller mean your view controller where amountTextField is set.In amountKeyboardViewController class you can see that i have create a object of amountTextField and also make a property so we need to set that object
Reena
i completely understand now. thanks :)
choise