how to add textfield and button programmatically? - iphone

I have a tableView and I want to apply search facility on tableView. For that I require textField and search Button but I don't know how to create these programmatically. so plz tell me how to create these two tools.
thanx.

Try with the following link which describes for asynchronous scrollbar example
http://blog.patrickcrosby.com/2010/04/27/iphone-ipad-uisearchbar-uisearchdisplaycontroller-asynchronous-example.html
But you can also refer the previous post
UISearchBar Sample Code
For more methods of implementing and getting results as you type, go to apples document and refer UISearchBarDelegate Protocol Methods

Here's the documentation for UITextField and UIButton. You might also find UISearchBar useful, which incorporates both a text field and a button.
Add your views to the table view's header view.

You have a table delegate tableview:cellForRowAtIndexPath. In this delegate add the following code.
//Suppose you want to add textfield and button at the first row
if(indexPath.Row == 0)
{
UITextField *txtSearch = [UITextField alloc] initWithFrame:CGRectMake(0,0,150,35)];
UIButton *btnSearch = [UIButton buttonWithType:UIButtoTypeCustom];
btnSearch.frame = CGRectMake(160,0,50,35);
[[cell contentView] addSubView:txtSearch];
[[cell contentView] addSubView:btnSearch];
}
Also You can add event for button like
[btnSearch addTarget:self action:#selector(eventName) forControlEvents:UIControlEventTouchUpInside];
Thanks

Related

Control more than one button using tags

I have 28 buttons in my application. I need to control them in a single function. All I want to do is make all the buttons visible. I gave tags to the buttons. I tried it with a for loop but I couldn't do it how can I solve this problem?
(IBAction)btnAction:(id)sender{
UIButton *btnPressed = (UIButton *)sender;
NSUInteger i=btnPressed.tag;
for(i=0; i<29; i++)
{
btnPressed.hidden=NO;
}
}
Instead of tags, use an IBOutletCollection. So now you have a single NSArray pointing to all of the buttons. Now just cycle through that NSArray.
You can create a single IBAction method, check tag values and then do what you want to do
- (IBAction)btnAction:(id)sender{
UIButton *btnPressed = (UIBUtton *)sender;
// Check button tags and write code accordingly //
}
use IBOutletCollection. see the following link for your answer.
IBOutletCollection
and
IBOutletCollection (UIbutton)
IBOutletCollection of UIButtons - changing selected state of buttons

retrieve text from textfield

i have
if(indexPath.row==0)
{
cell.lbl.text=#"abc";
cell.txtField.placeholder=#"abc"
cell.txtField.tag=104;
delegate.copyNameString = [NSString stringWithFormat:#"%#", [cell.txtField text]];
} else if(indexPath.row==1)
{
cell.lbl.text=#"home";
cell.txtField.placeholder=#"xyz";
cell.txtField.tag=105;
}
I am trying to retrieve the text which is in textfield, at row 0 and row 1.
i am trying to retrieve text on the textfield on the basis of tag.
How can i retrieve text?
Thanks
You can retrive your textfield like this and when you got your required textfield ,then get text.
UITableViewCell *myCell = (UITableViewCell *)[*yourTableViewName* cellForRowAtIndexPath:*passindexPath*]];
UITextField *mytextFiled = (UITextField *)[myCell viewWithTag:104];
the direct answer to your question is to use viewWithTag. In your example, [cell viewWithTag:105] will give you the text field. That said, your example has other issues.
First, if you can access the cell and the cell has a txtField property than you don't need the tag. Just use cell.txtField when you have the cell. Tags are typically used when you don't have a property holding a reference to the view you need. If you can't access the cell, the tag won't help you.
Second, you shouldn't add subviews directly to the cell. Instead you should add them to the cells contentView. The docs explain how to create a custom UITableViewCell fairly well.
NSString *textValue=((UITextField *)[self.view viewWithTag:104]).text;
You do like this,
UITextField *urtextFiled = (UITextField *)[self.view viewWithTag:104];
NSString *textValue=[urtextFiled text];
In which method you are trying to retrieve the text? I think you have to set delegate for your textfield and retrieve the text from this delegate method -(BOOL) textFieldShouldReturn:(UITextField *)textField

objective-c: How to access textfield in first row of second section of Tableview?

I have a Tableview I want to change the value of textField on the first row of second section?
I assign it tag value which lets say is 2.
Now how to access that TextField with tag.
Give a tag to that text field and by using that tag you can change the value.
UITextField *tempField = (UITextField *) [self.view viewWithTag: tag];
tempField.text = #"Your data";
If you wnat to access your textField inside some table view methods so you can do this:-
UITextField *tView=(UITextField *)[cell.contentView viewWithTag:2];
If you have to access your textField outside some tableVIew method than you have to do :-
lastSelectedPath=indexPath;do where you have added your text field.You will need its indexPath.Make it a global variable.
UITableViewCell *prevoiusCell=(UITableViewCell *)[*yourtabelviewname* cellForRowAtIndexPath:lastSelectedPath];
UITextField *tView=(UITextField *)[prevoiusCell.contentView viewWithTag:2];

If I add a UISwitch control to each of my table view cells, how can I tell which cell it belongs to?

I have a UITableView with cells that contain a UISwitch control. It's similar to the table view in the iPhone's Clock app shown below...
(source: epicself.com)
In my app's cellForRowAtIndexPath method, I create and attach the UISwitch control like so...
CGRect frameSwitch = CGRectMake(215.0, 10.0, 94.0, 27.0);
UISwitch *switchEnabled = [[UISwitch alloc] initWithFrame:frameSwitch];
[switchEnabled addTarget:self action:#selector(switchToggled:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView = switchEnabled;
My question is, when the switch is toggled by the user and the switchToggled method is called, how can I tell which table cell it belongs to? I can't really do much with it without knowing it's context.
Thanks so much in advance for your help!
In your action method, you can cast the superview of the passed-in UISwitch (which is the UITableViewCell itself), then pass that to the tableView's indexPathForCell method.
indexPathForCell will return a NSIndexPath object, which you can then use to index to your datamodel to modify. Then all you gotta do is call reloadData on your tableView.
Also, in cellForRowAtIndexPath, you should set the UISwitch's state based on your model.
First of all, fix the memory leak:
UISwitch *switchEnabled = [[[UISwitch alloc] initWithFrame:frameSwitch] autorelease];
Then pick one of these options:
switchEnabled.tag = someInt; // before adding it to the cell
NSLog(#"switched %d",switch.tag); // to see which switch was toggled
or
UITableViewCell *parentCell = switchEnabled.superview;
// + some magic to see which cell this actually is

Is it possible to accept a button tag to be in some range iphone sdk

In my application I'm doing dynamic resizing of cells and label in it depending upon the text in it.
I'm adding button to cells in uitableview.
I'm taking the label instance and button instance in a new label and button variable respectively and setting their frames to arrange them properly after resizing.
if(cel==nil)
{
//some code
original_label=[[UILabel alloc]init];
original_label.tag=111;
//SOME MORE CODE GOES HERE
original_button=[[UIButton alloc]init];
original_button.tag=222;
//SOME MORE CODE GOES HERE
}
new_label=(UILabel *) [cell viewWithTag:111]; //This' how I'm taking the label instance on cell and below button instance on cell in new variables
new_button = (UIButton * ) [cell viewWithTag:222];
Earlier I kept the tags of all the buttons on cells same, so it was easier to get button instances on cells properly and were being arranged properly. But now I want to recognize these buttons separately as I'm adding some functionality on button_click. I'm giving the buttons that are added to the cells incremental tags[1,2,3...9 and so on]. Now, how can I take these button tags in some range like[suppose 1-9]?
Can anybody help?
Thanks in advance.
You can keep the button tags the same as you had before.
Instead, in the button_click method, figure out which row the button is in like this:
- (void)button_click:(UIButton *)button
{
UITableViewCell *cell = (UITableViewCell *)[[button superview] superview];
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
//code to handle this indexPath.section and indexPath.row...
}
This assumes you have added the button to cell.contentView which is what the first superview gets. The second superview gets the cell.
The addTarget for the button should look like this (note colon after button_click):
[original_button addTarget:self action:#selector(button_click:) forControlEvents:UIControlEventTouchUpInside];