How to dismiss the keyboard when done editing a text field - ios

I have a simple program where you can type text into a text field, hit an ok button, and a label updates with the text entered.
I want the iPhone keyboard to disappear when I hit the OK button, when I press a large button that is in the background covering the whole view, or when I hit the return button on the keyboard. I have been trying to use the
[textField resignFirstResponder]
method, but it is not working. The program compiles fine, but when this method is called from any one of those events, it stops, and I get a message saying:
Thread 1: signal SIGABRT"
What am I doing wrong?
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize txtName;
#synthesize lblMessage;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)doSomething:(UIButton *)sender
{
[txtName resignFirstResponder];
NSString *msg = [[NSString alloc] initWithFormat:#"Hello, %#", txtName.text];
[lblMessage setText:msg];
//[msg release];
}
- (IBAction)makeKeyboardGoAway:(UIButton *)sender
{
[txtName resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
#end
Here is the header file as well:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UITextField *txtName;
#property (weak, nonatomic) IBOutlet UILabel *lblMessage;
- (IBAction)doSomething:(UIButton *)sender;
- (IBAction)makeKeyboardGoAway:(UIButton *)sender;
#end
Well I got it working, but I still don't understand the error message I was getting.
Here is the code that worked for me.
Header:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
IBOutlet UITextField *txtName;
IBOutlet UILabel *lblMessage;
}
#property (nonatomic, retain) IBOutlet UITextField *txtName;
#property (nonatomic, retain) IBOutlet UILabel *lblMessage;
- (IBAction)doSomething;
- (IBAction)makeKeyboardGoAway;
#end
Implementation:
#import "ViewController.h"
#implementation ViewController
#synthesize txtName;
#synthesize lblMessage;
- (IBAction)doSomething
{
[txtName resignFirstResponder];
NSString *msg = [[NSString alloc] initWithFormat:#"Hello, %#",
txtName.text];
[lblMessage setText:msg];
//[msg release];
}
- (IBAction) makeKeyboardGoAway
{
[txtName resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end

Call this method when the OK button is clicked.
[self.view endEditing:YES];

This works....if everything lines up with this it might be something else in your code....
...also be sure to have all objects linked correctly (VC to TextField and Button....Button back to VC)...that could also cause the crash
.h
#interface ViewController : UIViewController {
IBOutlet UIButton *button;
IBOutlet UITextField *textfield;
IBOutlet UILabel *label;
NSString *string;
}
-(IBAction)dismiss:(id)sender;
#property (nonatomic, retain)UIButton *button;
#property (nonatomic, retain)UITextField *textfield;
#property (nonatomic, retain)UILabel *label;
.m
#synthesize textfield, button, label;
-(IBAction)dismiss:(id)sender {
[textfield resignFirstResponder];
string = [NSString stringWithFormat:#"Hello %#", textfield.text];
[label setText:string];
}

Related

Xcode use of undeclared identifier

I am new to coding and had some basic knowledge but i am building out my first app from a tutorial and have a issue i can't figure out and after a few days of looking figured i would just ask. I get an error in my implementation file when initializing the object in the view did load.
It says use of undeclared identifier. any help would be greatly appreciated.
Here is my view controller.m
#import "ViewController.h"
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(#"titleLabel.text = %#", self.titleLabel.text);
self.bandObject = [[BandObject alloc] init];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
self.bandObject.name = self.nameTextField.text;
[self.nameTextField resignFirstResponder];
return YES;
}
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
self.bandObject.name =self.nameTextField.text;
[self saveBandObject];
[self.nameTextField resignFirstResponder];
return YES;
}
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
self.saveNotesButton.enabled = YES;
return YES;
}
-(BOOL)textViewShouldEndEditing:(UITextView *)textView
{
self.bandObject.notes = self.notesTextView.text;
[self.notesTextView resignFirstResponder];
self.saveNotesButton.enabled = NO;
return YES;
}
- (IBAction)saveNotesButtonTouched:(id)sender
{
[self textViewShouldEndEditing:self.notesTextView];
}
- (IBAction)ratingStepperValueChanged:(id)sender
{
self.ratingValueLabel.text = [NSString stringWithFormat:#"%g",self.ratingStepper.value];
self.bandObject.rating = (int)self.ratingStepper.value;
}
- (IBAction)tourStatusSegmentedControlValueChanged:(id)sender
{
self.bandObject.touringStatus = self.touringStatusSegmentedControl.selectedSegmentIndex;
}
- (IBAction)haveSeenLiveSwitchValueChanged:(id)sender
{
self.bandObject.haveSeenLive = self.haveSeenLiveSwitch.on;
}
#end
and here is my .h
#import <UIKit/UIKit.h>
#import "WBABand.h"
#interface ViewController : UIViewController <UITextFieldDelegate, UITextViewDelegate>
#property (nonatomic, strong) WBABand *bandObject;
#property (nonatomic, weak) IBOutlet UILabel *titleLabel;
#property (nonatomic, weak) IBOutlet UITextField *nameTextField;
#property (nonatomic, weak) IBOutlet UITextView *notesTextView;
#property (nonatomic, weak) IBOutlet UIButton *saveNotesButton;
#property (nonatomic, weak) IBOutlet UIStepper *ratingStepper;
#property (nonatomic, weak) IBOutlet UILabel *ratingValueLabel;
#property (nonatomic, weak) IBOutlet UISegmentedControl *touringStatusSegmentedControl;
#property (nonatomic, weak) IBOutlet UISwitch *haveSeenLiveSwitch;
- (IBAction)saveNotesButtonTouched:(id)sender;
- (IBAction)ratingStepperValueChanged:(id)sender;
- (IBAction)tourStatusSegmentedControlValueChanged:(id)sender;
- (IBAction)haveSeenLiveSwitchValueChanged:(id)sender;
#end
It is because you have an object in your .h called WBABand. And in your .m your are initializing a BandObject which does not exists.
Change this [[BandObject alloc] init];
to this [[WBABand alloc] init];
When you run into an issue, please try to narrow it down to the shortest code block possible to reproduce. At the very least, you should be pointing out the line that shows the error so that contributors can help you easier.
You are trying to instantiate an instance of BandObject, but the class you should be trying to instantiate is WBABand.
self.bandObject = [[WBABand alloc] init];

UITextField won't return and text field can't be changed. How do I fix this?

I am having problems trying to get the keyboard to go to the next field or return when the return key is pressed, my code is:
#interface LoginViewController ()
#property (weak, nonatomic) IBOutlet UIButton *loginButton;
#property (weak, nonatomic) IBOutlet UITextField *userField;
#property (weak, nonatomic) IBOutlet UITextField *passwordField;
#property (strong, nonatomic) UITextField *textField;
#end
#implementation LoginViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)viewDidLayoutSubviews
{
[self.userField becomeFirstResponder];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)tapLogin:(id)sender {
NSLog(#"login");
}
#pragma mark - UITextFieldDelegate methods
// for return and next
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.userField) {
[self.passwordField becomeFirstResponder];
}
else if (textField == self.passwordField) {
[self tapLogin:nil];
}
return YES;
}
The cursor beings in the userField but hitting the return key does nothing, if I manually tap on another field i.e the password one this also won't do anything. The cursor is stuck in the username field. I have implemented the protocol in my header file and also set the text fields as delegates in the interface builder. Could someone help please?
Try this,
Remove [self.userField becomeFirstResponder]; method from viewDidLayoutSubviews ie, remove the below method
- (void)viewDidLayoutSubviews
{
[self.userField becomeFirstResponder];
}
and add
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.userField becomeFirstResponder];
}

Xcode thinks delegate method is actually a new method being declared

I just created a new utility application from the Xcode templates and I have a problem with the - (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller method not being called. When I use the three finger tap on the trackpad to define it, it says that method is defined in MainViewController.m when it is definitely in the protocol of Flip.
MainVC.h
#import "FlipsideViewController.h"
#interface MainViewController : UIViewController <FlipsideViewControllerDelegate, UIPopoverControllerDelegate>
#property (strong, nonatomic) UIPopoverController *flipsidePopoverController;
#property (weak, nonatomic) IBOutlet UIView *backgroundView;
#end
.m
#import "MainViewController.h"
#interface MainViewController ()
#end
#implementation MainViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Background.jpg"]];
_backgroundView.contentMode = UIViewContentModeCenter;
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Flipside View Controller
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
//width
if (controller.widthSegmentedControl.selectedSegmentIndex == 0) {
//number of icons
} else {
//icon space
}
NSLog(#"b"); // not called
[self.flipsidePopoverController dismissPopoverAnimated:YES];
}
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
self.flipsidePopoverController = nil;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showAlternate"]) {
[[segue destinationViewController] setDelegate:self];
UIPopoverController *popoverController = [(UIStoryboardPopoverSegue *)segue popoverController];
self.flipsidePopoverController = popoverController;
self.flipsidePopoverController.delegate = self;
}
}
- (IBAction)togglePopover:(id)sender
{
if (self.flipsidePopoverController) {
[self.flipsidePopoverController dismissPopoverAnimated:YES];
self.flipsidePopoverController = nil;
} else {
[self performSegueWithIdentifier:#"showAlternate" sender:sender];
}
}
#end
FlipVC.h
#import <UIKit/UIKit.h>
#class FlipsideViewController;
#protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
#end
#interface FlipsideViewController : UIViewController
#property (weak, nonatomic) IBOutlet UISegmentedControl *widthSegmentedControl;
#property (weak, nonatomic) IBOutlet UITextField *widthNumberOfIcons;
#property (weak, nonatomic) IBOutlet UITextField *widthIconSpace;
#property (weak, nonatomic) IBOutlet UISegmentedControl *heightSegmentedControl;
#property (weak, nonatomic) IBOutlet UITextField *heightNumberOfIcons;
#property (weak, nonatomic) IBOutlet UITextField *heightIconSpace;
#property (weak, nonatomic) id <FlipsideViewControllerDelegate> delegate;
- (IBAction)done:(id)sender;
#end
.m
#import "FlipsideViewController.h"
#interface FlipsideViewController ()
#end
#implementation FlipsideViewController
- (void)awakeFromNib
{
self.preferredContentSize = CGSizeMake(320.0, 480.0);
[super awakeFromNib];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Actions
- (IBAction)done:(id)sender
{
NSLog(#"a"); //this is called
[self.delegate flipsideViewControllerDidFinish:self];
}
#end
There are two NSLogs in there, one for the method where the done button gets pressed (a) and one for the one where that didFinish method should be called (b). The problem is that when I press the done button on the Flip controller nothing happens (although "a" gets logged).
I found the problem. I removed the default button bar Info item and replace it with a normal button. The problem is that the segue needs to be anchored to that button and connected to the togglePopover method in the MainViewController.m file.

Keyboard won't dismiss in app

In my app I am making, I have 3 UITextFields and one UITextView. For both of them, my keyboard will appear, but then I cant get it to go away. I looked up some methods on Stack Overflow but I can't seem to implement them the right way. Would anyone care to tell me what I'm doing wrong in my following lines of code?
ViewController.h
#interface ViewController : UIViewController <UITextViewDelegate>
#property (strong, nonatomic) NSString *dna;
#property (weak, nonatomic) IBOutlet UITextField *dnaOut;
#property (weak, nonatomic) IBOutlet UITextField *mrnaOut;
#property (weak, nonatomic) IBOutlet UITextField *trnaOut;
#property (weak, nonatomic) IBOutlet UITextView *aminoOut;
- (IBAction)translateButton:(UIButton *)sender;
- (IBAction)clearButton:(UIButton *)sender;
#property (weak, nonatomic) IBOutlet UILabel *dnaError;
#property (weak, nonatomic) IBOutlet UILabel *mrnaError;
#property (weak, nonatomic) IBOutlet UILabel *trnaError;
#property (weak, nonatomic) IBOutlet UISegmentedControl *inputType;
#end
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_aminoOut.delegate = self;
-(BOOL) _aminoOut textFieldShouldReturn:(UITextField *)textfield {
[textField resignFirstResponder];
return YES;
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
if ([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
// Return FALSE so that the final '\n' character doesn't get added
return NO;
}
// For any other character return TRUE so that the text gets added to the view
return YES;
}
}
Why is all of that code within your viewDidLoad method? It should be:
.h file:
#interface ViewController : UIViewController <UITextViewDelegate, UITextFieldDelegate>
// Other stuff here...
.m file:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.dnaOut.delegate = self;
self.mrnaOut.delegate = self;
self.trnaOut.delegate = self;
self.aminoOut.delegate = self;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textfield {
[textField resignFirstResponder];
return YES;
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
// Return FALSE so that the final '\n' character doesn't get added
return NO;
}
// For any other character return TRUE so that the text gets added to the view
return YES;
}
I think you forgot to connect the delegate of UITextField to files owner.

button or label not working

When I try to change the text of a label on button click, the code compiles but it doesn't work. I click the button and nothing happens.
Currently, im just trying to display "Error" on label x1Label when button solveButton will be pressed. Could you please help me fixing it?
.h
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize errorLabel;
#synthesize aField, bField, cField;
#synthesize x1Label, x2Label;
#synthesize solveButton;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
aField.delegate = self;
bField.delegate = self;
cField.delegate = self;
aField.keyboardType = UIKeyboardTypeNumberPad;
bField.keyboardType = UIKeyboardTypeNumberPad;
cField.keyboardType = UIKeyboardTypeNumberPad;
NSString *str = #"car";
errorLabel.text = str;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[aField resignFirstResponder];
[bField resignFirstResponder];
[cField resignFirstResponder];
return YES;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[aField resignFirstResponder];
[bField resignFirstResponder];
[cField resignFirstResponder];
}
- (IBAction)solveButton:(id)sender
{
errorLabel.text = #"Error";
}
#end
.m
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITextFieldDelegate>
{
IBOutlet UITextField *aField, *bField, *cField;
}
#property (weak, nonatomic) IBOutlet UILabel *errorLabel;
#property (strong, nonatomic) IBOutlet UITextField *aField;
#property (strong, nonatomic) IBOutlet UITextField *bField;
#property (strong, nonatomic) IBOutlet UITextField *cField;
#property (weak, nonatomic) IBOutlet UILabel *x1Label;
#property (weak, nonatomic) IBOutlet UILabel *x2Label;
#property (weak, nonatomic) IBOutlet UIButton *solveButton;
#end
Thank you
You are writing on errorLabel not on x1Label.
Did you connect the SolveButton action to the button in your storyboard?
if not go to storyboard > click on your button > go to Connection Inspector (on the right panel) and link the action to you button.

Resources