resignfirstresponder crashes my ios app - iphone

I am new to IOS and can't seem to get this to work , I have an input filed on my app,from which I want to hide the keyboard whenever the user either presses return or the associated button (searchGo)
The following is my code :
mainViewController.h
#interface kepnMainViewController : UIViewController <kepnFlipsideViewControllerDelegate, MKMapViewDelegate>
{
MKMapView *_mapView;
IBOutlet UITextField *searchBox;
IBOutlet UIBarButtonItem *searchGo;
IBOutlet UIBarButtonItem *searchNearby;
MKAnnotationView *annotationView;
}
#property (strong, nonatomic) MKMapView *_mapView;
#property (strong, nonatomic) MapAnnotation *annotation;
#property (strong, nonatomic) UIPopoverController *flipsidePopoverController;
#property (strong, nonatomic) MKAnnotationView *annotationView;
#property (strong, nonatomic) UIBarButtonItem *searchGo;
- (IBAction)showInfo:(id)sender;
- (IBAction)searchGo:(id)sender;
- (IBAction)showNearby:(id)sender;
- (IBAction)searchBoxReturn:(id)sender;
- (void) setPlaceMarker: (CLLocationCoordinate2D) coord :(NSString*) title :(NSString*) subtitle;
#end
Appropriate .m snippet
-(IBAction)searchGo:(id)sender
{
NSLog(#"sender object %#",sender);
[sender resignFirstResponder];
NSLog(#"search button pressed and textbox = %#",searchBox.text);
}
-(IBAction)searchBoxReturn:(id)sender
{
NSLog(#"search box return ");
[sender resignFirstResponder];
}
Sorry if this is a dumb question but what am I doing wrong.??

UIBarButtonItem isnt an UIView and therefore definitely not an UIResponder. Instead, it's a subclass of NSObject, which doesn't respond to - (void)resignFirstResponder.
(solution: remove the [sender resignFirstResponder]; lines)

So if your view is manually programmed than you might add the following check to your code:
if ( [sender isKindOf: [UIResponder class]] == YES && [(UIResponder*)sender canResignFirstResponder] == YES )
[sender resignFirstResponder];

If you want to hide keybord on button tap assuming your searchBox is current responder than you should write
[searchBox resignFirstResponder]; // This will close keyboard

Related

Focus on UITextField does not work

In my iPhone application I have a UIScrollView with several UITextFields.
Using BSKeyboardControls I have added Prev/Next/Done buttons to move between the fields. However, the focus on the selected field is not working, meaning that the text field is actually still under the keyboard although selected.
becomeFirstResponder is activated but just don't set the focus.
Any ideas what might be wrong?
Thanks
In the H file
#import "BSKeyboardControls.h"
...
#interface AddClientViewController : BaseViewController<UIAlertViewDelegate, UIScrollViewDelegate, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate, UITextViewDelegate, BSKeyboardControlsDelegate>
...
#property (strong, nonatomic) IBOutlet UITextField *firstName;
#property (strong, nonatomic) IBOutlet UITextField *lastName;
#property (strong, nonatomic) IBOutlet UITextField *email;
#property (strong, nonatomic) IBOutlet UITextField *mobile;
#property (strong, nonatomic) IBOutlet UITextField *birthday;
#property (strong, nonatomic) IBOutlet UITextField *anniversary;
#property (strong, nonatomic) IBOutlet UITextField *street;
#property (strong, nonatomic) IBOutlet UITextField *city;
#property (strong, nonatomic) IBOutlet UITextField *state;
#property (strong, nonatomic) IBOutlet UITextField *zip;
#property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
#property (nonatomic, strong) BSKeyboardControls *keyboardControls;
....
In M file
- (void)viewDidLoad
{
...
NSArray *fields = #[ self.firstName, self.lastName,
self.email, self.mobile,
self.birthday, self.anniversary,
self.street, self.city, self.state, self.zip];
[self setKeyboardControls:[[BSKeyboardControls alloc] initWithFields:fields]];
[self.keyboardControls setDelegate:self];
}
- (void)keyboardControlsDonePressed:(BSKeyboardControls *)keyboardControls
{
[keyboardControls.activeField resignFirstResponder];
}
- (void)keyboardControls:(BSKeyboardControls *)keyboardControls directionPressed:(BSKeyboardControlsDirection)direction
{
UIView *view = keyboardControls.activeField.superview.superview;
[self.scrollView scrollRectToVisible:view.frame animated:YES];
}
- (void)keyboardControls:(BSKeyboardControls *)keyboardControls selectedField:(UIView *)field inDirection:(BSKeyboardControlsDirection)direction
{
UIView *view = keyboardControls.activeField.superview.superview;
[self.scrollView scrollRectToVisible:view.frame animated:YES];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self.keyboardControls setActiveField:textField];
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
[self.keyboardControls setActiveField:textView];
}
The setActiveField in BSKeyboardControls
- (void)setActiveField:(id)activeField
{
if (activeField != _activeField)
{
if ([self.fields containsObject:activeField])
{
_activeField = activeField;
if (![activeField isFirstResponder])
{
[activeField becomeFirstResponder];
}
[self updateSegmentedControlEnabledStates];
}
}
}
Since you're using a UIScrollView with UITextFields, you can use the scrollRectToVisible method for the UIScrollview, in a method that'd be roughly like this:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[_myScrollView scrollRectToVisible:textField.frame animated:YES];
}
To do this, you'll need to make sure that the UIViewController is the UITextFieldDelegate of each of the textfields in the scrollview. You could do this in the viewDidLoad method of your UIViewController:
[textField1 setDelegate:self];
[textField2 setDelegate:self];
...and so on

iPhone connect textfield end on exit event to button press event

I have a text field and a button. When the button is pressed, it calls a routine. I want the textfield end on exit to call the same routine without having to duplicate the code. ViewController.h is below
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIWebView *webView;
#property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityind;
#property (weak, nonatomic) IBOutlet UITextField *search;
- (IBAction)calculate:(id)sender;
#end
In ViewController.h implement UITextFieldDelegate like so:
#interface ViewController : UIViewController <UITextFieldDelegate>
and then use the method
-(BOOL)textFieldShouldReturn:(UITextField *)textField
and call your IBAction and resignFirstResponder, I would also auto-enable return key.
[EDIT1]
In your viewController header:
#interface ViewController : UIViewController <UITextFieldDelegate>
#property (weak, nonatomic) IBOutlet UIWebView *webView;
#property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityind;
#property (weak, nonatomic) IBOutlet UITextField *search;
-(void)doSomeWork;
-(IBAction)calculate:(id)sender;
#end
Simply implement the functionality in a 2nd tier routine.
In your viewController.m file:
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
if(search == textField){
[textField resignFirstResponder];
[self doSomeWork];
}
return YES;
}
-(void)doSomeWork{
// Do whatever you want to do here!!!
}
-(IBAction)calculate:(id)sender{
[self doSomeWork];
}
In your xib file you will have to connect up the button to the "calculate" action, the search to the proper UITextField.
You can set the delegate of the UITextField either graphically in the Interface Builder, or in code. If in code, then within your viewController.m file add the line:
search.delegate = self;
to your viewDidLoad method as follows:
-(void)viewDidLoad{
[super viewDidLoad];
search.delegate = self;
}

UISlider - parameter - iphone

In my application I have a UISlider where file.h is:
#interface Mapa : UIViewController {
UILabel *label;
UISlider *slider;
}
#property(nonatomic, retain) IBOutlet UILabel *label;
#property(nonatomic, retain) IBOutlet UISlider *slider;
-(IBAction)sliderValueChanged:(UISlider *)sender;
-(IBAction) OpenList:(id)sender;
-(IBAction) OpenMap:(id)sender;
And file.m:
#synthesize label;
#synthesize slider;
-(IBAction)sliderValueChanged:(UISlider *)sender {
label.text = [NSString stringWithFormat:#"%f", sender.value];
}
- (void)dealloc {
[super dealloc];
[label release];
[slider release];
}
done this to me uislider goal of the label, I get the number that's out on the label bound to the UISlider, another screen to call a map, how this value label step to the other screen?
thanks
Assuming your 'other screen' is another UIViewController, then after instantiating the new UIViewController, you would set the parameters on the new UIViewController before calling either pushViewController or presentModalViewController.
If you're using a UIStoryboard then you can set the parameters of the new view controller in 'prepareForSegue'. See this question How to pass prepareForSegue: an object for details.

switching views in iphone [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
switching views in iphone
I am making a client server program in which i want to switch from one view to another but i am getting an error in "clientserverprogram view.m" plz help.i also asked same question five hours ago but the answer was not satisfactory again same error is pertaining.
"clientserverprogram view.h"
#import <UIKit/UIKit.h>
#class secondview;
#interface clientserverprogramViewController : UIViewController {
IBOutlet UITextField *name;
IBOutlet UITextView *filepath;
IBOutlet UIButton *print;
IBOutlet UIButton *settings;
IBOutlet UIButton *cancel;
IBOutlet UILabel *display;
IBOutlet secondview *secondview;
}
-(IBAction) print;
-(IBAction) settings;
-(IBAction) cancel;
#property (nonatomic , retain) IBOutlet UITextField *name;
#property (nonatomic , retain) IBOutlet UITextView *filepath;
#property (nonatomic , retain) IBOutlet UILabel *display;
#end
"clientserverprogram view.m"
#import "clientserverprogramViewController.h"
#import "secondview.h"
#implementation clientserverprogramViewController
#synthesize name ,filepath,display ;
-(IBAction) print {
NSString *str = name.text;
[display setText : str];
}
-(IBAction) settings {
[self presentModalViewController: secondview animated: YES ];
"" error: expected expression before 'secondview'""
}
-(IBAction) cancel {
exit(0);
}
- (void)dealloc {
[super dealloc];
}
#end
"secondview.h"
#import <UIKit/UIKit.h>
#interface secondview : UIViewController {
IBOutlet UIView *view;
IBOutlet UIButton *back;
}
-(IBAction) back;
#end
""secondview.m""
#import "secondview.h"
#implementation secondview
-(IBAction) back {
[self.parentViewController dismissModalViewControllerAnimated: YES];
}
- (void)dealloc {
[super dealloc];
}
#end
I used your code and the problem here is your class name "secondview" and the instance you are making from it IBOutlet secondview *secondview are same. Please use different name for class names and the instances you create.
Always start with uppercase for class names and start with lowercase for class instances you create.
Hence, your class name should be SecondView and you should write IBOutlet SecondView *secondView.
Or you should just use different names. Its very confusing.

Not able to push next view after clicking a uibutton which is on the top right of the view

i am having a view consisting of a tableview and a uibutton like this
when i click on button "add" a new class/xib named "Locsetting" should be open.which i am not able to do.
my code: .h file
#interface LocationViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
UITableView *table;
NSMutableArray *menuList;
LocSetting *locSetting;
IBOutlet UIButton *addbutton;
}
#property (nonatomic,retain) NSMutableArray *menuList;
#property (nonatomic,retain) IBOutlet UITableView *table;
#property (nonatomic,retain) LocSetting *locSetting;
#property (nonatomic, retain) IBOutlet UIButton *addbutton;
-(IBAction)gotoLocSetting:(id) sender;
#end
My .m :
#synthesize addbutton;
-(IBAction)gotoLocSetting:(id) sender {
NSLog(#"gotoLocSetting Entered");
locSetting = [[LocSetting alloc]
initWithNibName:#"LocSetting"
bundle:nil];
//locationViewController.menuList = [menuList objectAtIndex:indexPath.row];
[self.navigationController pushViewController:locSetting
animated:YES];
[locSetting release];
}
what wrong i am doing?or please guide me!
Thanks
On a quick look, there might be one of the following 3 problems:
Is your IBAction linked to the button's outlet?
If not, try removing LocSetting *locSetting; and its property/synthesize. Next, change your line down there to: Locsetting *locSetting = [[LocSetting alloc] initWithNibName:#"LocSetting" bundle:nil];
If that doesn't work, try changing that to: Locsetting *locSetting = [[LocSetting alloc] initWithNibName:nil bundle:nil];
Make sure the IBAction is connected properly to the button. And I think its better to have IBOutlet UIButton *addbutton; alone and #property (nonatomic, retain) UIButton *addbutton;