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.
Related
I want to pass UITextField *nameText from GreenViewController to UILabel *nameValue in ViewController using delegate.
I made delegate, the onSave method in ViewController is called but the screen is not going back and the name is not passed.
What is the problem?
Here are the header and impelentation files:
GreenViewController.h
#import <UIKit/UIKit.h>
#protocol GreenViewDelegate <NSObject>
-(void)onSave:(NSString*)nameValue;
#end
#interface GreenViewController : UIViewController
#property id<GreenViewDelegate> delegate;
#property (nonatomic) NSString* sliderValuePassed;
#property (weak, nonatomic) IBOutlet UIButton *Next;
#property (weak, nonatomic) IBOutlet UIButton *Save;
#property (weak, nonatomic) IBOutlet UIButton *Back;
#property (weak, nonatomic) IBOutlet UILabel *sliderTextValue;
#property (weak, nonatomic) IBOutlet UITextField *NameText;
- (IBAction)save:(id)sender;
#end
GreenViewController.m
#import "GreenViewController.h"
#import "ViewController.h"
#interface GreenViewController ()
#end
#implementation GreenViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.sliderTextValue.text = self.sliderValuePassed;
}
- (IBAction)back:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)save:(id)sender {
[self.delegate onSave:self.NameText.text];
[self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#import "GreenViewController.h"
#interface ViewController : UIViewController <GreenViewDelegate>
#property (weak, nonatomic) IBOutlet UISlider *slider;
#property (weak, nonatomic) IBOutlet UILabel *sliderTextValue;
#property (weak, nonatomic) IBOutlet UIButton *EnterName;
#property (weak, nonatomic) IBOutlet UILabel *NameValue;
#property (weak, nonatomic) IBOutlet UIButton *LikeIOS;
#property (weak, nonatomic) IBOutlet UISwitch *CheckboxIOS;
#end
ViewController.m
#import "ViewController.h"
#import "RedViewController.h"
#import "GreenViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.sliderTextValue.text = [NSString stringWithFormat:#"Slider value = %d",(int)self.slider.value];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"yellowToGreen"]){
GreenViewController* gVC = segue.destinationViewController;
gVC.sliderValuePassed = [NSString stringWithFormat:#"Slider value = %d",(int)self.slider.value];
gVC.delegate = self;
}else if([segue.identifier isEqualToString:#"yellowToRed"]){
RedViewController* rVC = segue.destinationViewController;
rVC.sliderValuePassed = [NSString stringWithFormat:#"Slider value = %d",(int)self.slider.value];
rVC.CheckboxIOS = self.CheckboxIOS;
}
}
- (IBAction)sliderChangeValue:(id)sender {
int sliderVal = self.slider.value;
self.sliderTextValue.text = [NSString stringWithFormat:#"Slider value = %d",sliderVal];
}
-(void)onSave:(NSString*)nameValue{
NSLog(#"onSave in father controller");
self.NameValue.text = [NSString stringWithFormat:#"Name = %#",nameValue];
}
#end
I assume you didnt set the delegate .
Set it like this:
GreenViewController *myVc = [[GreenViewController alloc] init];
myVc.delegate = self;
Put this in your view controller view did load method!!!
try with GreenViewController.h:
#property (nonatomic, assign) id<GreenViewDelegate> delegate;
and GreenViewController.m:
- (IBAction)save:(id)sender {
[_delegate onSave:self.NameText.text];
[self.navigationController popViewControllerAnimated:YES];
}
ViewController.* seems to be OK
I am trying to get the keyboard to go away, when I press the return key on my application heres my code from the .m:
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
and the code in the .h:
- (BOOL)textFieldShouldReturn:(UITextField* )textField;
But when I run the application and hit the return key nothing happens. What am I doing wrong?
my .h:
#import <UIKit/UIKit.h>
#interface ChemicalInfoViewController: UIViewController{
IBOutlet UIScrollView *ChemicalInforScroller;
}
#property (strong, nonatomic) IBOutlet UITextField *PPMForChlTextField;
#property (strong, nonatomic) IBOutlet UITextField *GallonsForChlTextField;
#property (strong, nonatomic) IBOutlet UITextField *PPMForAlkTextField;
#property (strong, nonatomic) IBOutlet UITextField *GallonsForAlkTextField;
#property (strong, nonatomic) IBOutlet UITextField *PPMForPHTextField;
#property (strong, nonatomic) IBOutlet UITextField *GallonsForPHTextField;
- (IBAction)SumbitCDI:(UIButton *)sender;
#property (strong, nonatomic) IBOutlet UILabel *PPMLabelForChl;
#property (strong, nonatomic) IBOutlet UILabel *GallonsLabelForChl;
#property (strong, nonatomic) IBOutlet UILabel *PPMLabelForAlk;
#property (strong, nonatomic) IBOutlet UILabel *GallonsLabelForAlk;
#property (strong, nonatomic) IBOutlet UILabel *PPMLabelForPH;
#property (strong, nonatomic) IBOutlet UILabel *GallonsLabelForPH;
#property (strong, nonatomic) IBOutlet UITextField *ChlorinePoundsTextField;
#property (strong, nonatomic) IBOutlet UITextField *GallonsForAlkDecreaser;
#property (strong, nonatomic) IBOutlet UITextField *PhPoundsTextField;
#property (strong, nonatomic) IBOutlet UITextField *AlkPoundsTextField;
#property (strong, nonatomic) IBOutlet UITextField *LbsForAlkDecreaser;
#property (strong, nonatomic) IBOutlet UITextField *PPMForAlkDecreaser;
#property (strong, nonatomic) IBOutlet UITextField *LbsForPhDecreaser;
#property (strong, nonatomic) IBOutlet UITextField *PPMForPhDecreaser;
#property (strong, nonatomic) IBOutlet UITextField *GallonsForPhDecreaser;
- (IBAction) clickedBackground;
#interface ChemicalInfoViewController : UIView <UITextField>
#end
and my .m:
#import "ChemicalInfoViewController.h"
#interface ChemicalInfoViewController ()
#end
#implementation ChemicalInfoViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[ChemicalInforScroller setScrollEnabled:YES];
[ChemicalInforScroller setContentSize:CGSizeMake(320, 1000)];
self.textField.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction) clickedBackground {
[self.view endEditing:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
In your .h, remove:
- (BOOL)textFieldShouldReturn:(UITextField* )textField;
And it will work as it should, given that in your .h, you conform to the protocol:
#interface yourViewController : UIView <UITextFieldDelegate>
And set up the delegate like so:
yourTextField.delegate = self;
Edit regarding to the #end:
In your .m, add a #end to the bottom; so , it becomes (scroll to the very bottom):
#implementation ChemicalInfoViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[ChemicalInforScroller setScrollEnabled:YES];
[ChemicalInforScroller setContentSize:CGSizeMake(320, 1000)];
self.textField.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction) clickedBackground {
[self.view endEditing:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
#end //Here :-)
You didn't set the delegates for your textfields.
In "viewDidLoad" add
_ChlorinePoundsTextField.delegate = self;
_GallonsForAlkDecreaser.delegate = self;
_PhPoundsTextField.delegate = self;
_AlkPoundsTextField.delegate = self;
_LbsForAlkDecreaser.delegate = self;
_PPMForAlkDecreaser.delegate = self;
_LbsForPhDecreaser.delegate = self;
_PPMForPhDecreaser.delegate = self;
_GallonsForPhDecreaser.delegate = self;
none of these methods seemed to be working for some reason but what i did end up doing was creating a DidEndOnExit action and resigned the keyboard under that action for each textfield
I'd appreciate if anyone could help.
I've created a slider and a label that changes it's text value when the user changes the slider. It's not working, for some reason.
Thoughts ?
I'm using xcode 6.1
Here's the code:
#import "ViewController.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UITextField *numberField;
#property (weak, nonatomic) IBOutlet UITextField *nameField;
#property (weak, nonatomic) IBOutlet UILabel *sliderLabel;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.sliderLabel.text = #"50";
}
- (IBAction)textFieldDoneEditing:(id)sender {
[sender resignFirstResponder];
}
- (IBAction)backgroundTap:(id)sender {
[self.nameField resignFirstResponder];
[self.numberField resignFirstResponder];
}
- (IBAction)sliderChanged:(UISlider *)sender {
int progress = lroundf(sender.value);
self.sliderLabel.text = [NSString stringWithFormat:#"%d", progress];
}
#end
take UISlider outlet and add selector as value changed in viewDidLoad like below.
IBOutlet UISlider *slider;
and to add selector in viewDidLoad
[slider addTarget:self action:#selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
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.
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];
}