On Cancel Pressed of UISearchBar - Fire Custom Action in iPhone - iphone

alt text http://img216.imageshack.us/img216/2288/problem11.png
As given in the picture, I have an search bar in my application.
Now I want to handle my custom action on Cancel Taped.
-> On Cancel Taped -> PopViewController
-> On Cancel Taped -> Clear Search Text & Hide Search Bar Keyboard.
I don't know how to handle Cancel Tap?
Thanks in advance for helping me.

Ok ! I tried Something Different. I added a new button on Cancel Button.
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.backgroundColor=[UIColor clearColor];
[btn setFrame:CGRectMake(260, 5, 53, 32)];
[btn addTarget:self action:#selector(onCancelSearch:) forControlEvents:UIControlEventTouchUpInside];
[searchBar addSubview:btn];
Now. See Following image. but it is with RoundedRect Type, Instead Use TypeCustom - so it will become invisible.
alt text http://img269.imageshack.us/img269/4337/problem12e.png

Related

Is it possible to make the navigationcontroller title a button?

Is it possible to make the navigationcontroller title a button? but without a button look to it. I want it to be just text that if you click on it a view/actionsheet pops up from the bottom
If so, can you show me programmatically how I would make it a button?
Thanks
You can set TitleView of navigationItem to set a button as title.
Here is what i tried and got it working:
UIButton*button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame=CGRectMake(0, 0, 30, 20);
[button setTitle:#"Testing" forState:UIControlStateNormal];
[button addTarget:self action:#selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
[[self navigationItem] setTitleView:button];
But if you don't want it to look like a button, you have set the buttonWithType to custom and set its backGroundImage property.
in btnAction have your code to show the pop ups.

Adding a UIButton

I need a UIButton to act as a Radio button (selection button). When the user
clicks on the button, the color should change (to indicate that the user
clicked this button), and when the user clicks the same button again (the already selected
button) the color of the button should change to the default color indicating
that the button was un-selected.
How can i do this programatically ?
There might be other alternatives to do this, but i require to use the button
to do this.
You can specify different images for selected and normal button states:
[btnName setImage:selectedImage forState:UIControlStateSelected];
[btnName setImage:normalimage forState:UIControlStateNormal];
And then in button's action method simply toggle button's state between normal and selected:
- (void) buttonAction:(UIButton*)sender{
sender.selected = !sender.selected;
}
Put the below code in your button action.
if([btnName isSelected]) {
[btnName setImage:#"SelectedImage" forState:UIControlStateNormal];
[btnName setSelected:NO];
} else {
[btnName setImage:#"deSelectedImage" forState:UIControlStateNormal];
[btnName setSelected:YES];
}
Hope it will be useful.
Follow below link:
Radio Button in iPhone

Where to insert code that creates buttons programmatically?

I receive some JSON from the net and based on that data, I must create 2 or 3 buttons. Part of my gui is static and created in NIB (won't change), only the number of buttons will change. I found this code for making buttons in code:
//create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//set the position of the button
button.frame = CGRectMake(100, 170, 100, 30);
//set the button's title
[button setTitle:#"Click Me!" forState:UIControlStateNormal];
Is this the right way? In which method of my viewcontroller should I put this code?
You can add the button whenever you want, as long as the view has been loaded already. The one thing you'd need to add to the above code is
[[self view] addSubview:button];
Using this code, you have a button on the screen, but it won't be able to trigger any actions. You'll probably also want to add:
[button addTarget:self action:#selector(someMethod:) forControlState:UIControlEventTouchUpInside];
You should add the buttons in the delegate/method that parses the JSON data.
Don't forget to add the created buttons to your view:
[containerView addSubview:button];

Numeric keyboard dismiss?

HI all,
I am having a 2 textFeild and 1 textview.
out of 2 textfeild,one textfield requires number to be filled in there.
so for that i hv provided numeric keyboard.
i m not able to dismiss numeric keyboard,since i cant c any option to hit the numeric keyboard dismiss.
is it possible to dismiss numeric keyboard
??
if yes than can anyone guide me through.
regards
shishir
You have 2 options: add a return keyboard in a numpad keyboard or catch the event of user select another textfield and dismiss the numpad there. Here is the link to add a return (done) button to a numpad keyboard. Done in numpad keyboard
There is no button to dismiss the numeric keypad (although on the iPad you can do this). You will have to call [textfield resignFirstResponder] in code if you want to do this yourself.
There is one other possible way. For those who are looking for something perfect then vodkhang's answer is very good. But if you are looking for some work-around then textField.inputAccessoryView can be useful. The idea is to show done button in inputAccessoryView. Following is sample code
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 25)];
view.backgroundColor = [UIColor grayColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Done" forState:UIControlStateNormal];
button.frame = CGRectMake(320-60, 0, 60, 25);
[view addSubview:button];
textField.inputAccessoryView = view;
There are few things to be noted.
Width of accessoryView is equal to screen width. So you need to write some code for managing look and feel according to your application.
You can give any kind of look to you dome button. And here you don't have to worry about Apple's design changes in keyboard.

UITextField leftView

Is it possible to set up a UITextField with a leftView so that if a user clicks into the UITextField the keyboard shows but if they click on an icon in the leftView another method is called (i.e., one that displays a UIPickerview)?
Have you checked that the leftFieldViewMode is set to something other than never
[textField setLeftViewMode:UITextFieldViewModeAlways];
Instantiate a button as u do normaly
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0,0,30,30)];
[btn setTitle:#"title" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(BtnClick) forControlEvents:UIControlEventTouchUpInside];
Instantiate a textfield as you do normally
UITextField *txtfield1=[[UITextField alloc]initWithFrame:CGRectMake(10, 10,300,30)];
[txtfield1 setBackgroundColor:[UIColor grayColor]];
[txtfield1 setPlaceholder:#" Enter"];
To set a view in left side(there is right view also available)
[txtfield1 setLeftView:btn];
if u want the view to show always then below code does it
[txtfield1 setLeftViewMode:UITextFieldViewModeAlways];
if u want the view to show never then
[txtfield1 setLeftViewMode:UITextFieldViewModeNever];
if u want the view to show if the textfield is not edited then below
[txtfield1 setLeftViewMode:UITextFieldViewModeUnlessEditing];
if u want the view to show if the textfield is edited then
[txtfield1 setLeftViewMode:UITextFieldViewModeWhileEditing];
button click event and pop over
-(void)BtnClick
{
//Usual pop over coding goes here
}
Have you tried using a UIButton for this task? From the documentation for the leftView property:
If your overlay view does not overlap
any other sibling views, it receives
touch events like any other view. If
you specify a control for your view,
the control tracks and sends actions
as usual. If an overlay view overlaps
the clear button, however, the clear
button always takes precedence in
receiving events.
So create a UIButton instance, configure its appearance and actions as needed, then set it as the leftView property.