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
Related
I am trying to get my UITextField email in my first view controller to become the UILabel in my next view controller. It says there is an error stating there is no visible #interface for NSString declares the selector initvalue. I thought the interface was being loaded from the vc1.h file when you imported it on the vc2? any help would be nice. Heres what I have so far:
vc1.h
#import <UIKit/UIKit.h>
#interface loginUserViewController : UIViewController
#property (nonatomic, retain, strong) IBOutlet UITextField *email;
#property (nonatomic, retain) IBOutlet UITextField *password;
#property (nonatomic, retain) IBOutlet UIButton *login;
#property (nonatomic,retain) IBOutlet UIButton *registerBtn;
-(IBAction)loginUser:(id)sender;
#end
vc2.h
#import <UIKit/UIKit.h>
#import "loginUserViewController.h"
#interface Home : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *username;
#property (weak, nonatomic) IBOutlet UIBarButtonItem *Nav;
#property (weak, nonatomic) IBOutlet UIBarButtonItem *logout;
-(IBAction)logout:(id)sender;
-(IBAction)bandHome:(id)sender;
#end
vc2.m
import "Home.h"
#import "loginUserViewController.h"
#interface Home ()
#end
#implementation Home
#synthesize username, band1, band2, band3;
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self){
}
return self;
}
-(void)viewDidLoad
{
[super viewDidLoad];
loginUserViewController *object = [[loginUserViewController alloc]init];
NSString *string = [[NSString alloc] initWithFormat:#"%i",[object.email.text initValue]];
username.text = string;
}
vc1.m
#import "loginUserViewController.h"
#import "Home.h"
#import "createBandViewController.h"
#interface loginUserViewController ()
#end
#implementation loginUserViewController
#synthesize email, password;
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
}
- (void)viewDidload
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[self setEmail:nil];
[self setPassword:nil];
[super viewDidUnload];
}
-(IBAction)loginUser:(id)sender {
if ([email.text isEqualToString:#""] || [password.text isEqualToString:#""])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"alert" message:#"Please Fill all the field" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
return;
}
Simply replace this string:
NSString *string = [[NSString alloc] initWithFormat:#"%i",[object.email.text initValue]];
with this string:
NSString *string = [[NSString alloc] initWithFormat:#"%i",[object.email.text intValue]];
and if you want simply the email just as a string then replace it with this string
NSString *string = [[NSString alloc] initWithFormat:#"%#",object.email.text];
your error will be resolved.Hope it helps :)
First of All in VC2.h
#import <UIKit/UIKit.h>
#import "loginUserViewController.h"
#interface Home : UIViewController
#property (strong, nonatomic) IBOutlet UILabel *username; // Keep property strong to get the value
in VC2.m
#synthesize username;
Now in VC1.h
#import <UIKit/UIKit.h>
#import "homeviewcontroller.h"
#interface loginUserViewController : UIViewController
#property (nonatomic, strong) IBOutlet UITextField *email;
#property (nonatomic, strong) IBOutlet UITextField *password;
#property (nonatomic) nsstring *username;
-(void)viewDidLoad
{
[super viewDidLoad];
username= self.email.text;
homeviewcontroller *object = [[homeviewcontroller alloc]init];
object.username.text=username;
}
I just started learning objective-c, i was breaking my head why this below simple code for protocols and delegates is not working, please clarify me why this is delegate method is not getting called. Thanks in advance.
ViewController.h
#import <UIKit/UIKit.h>
#include "secondViewController.h"
#interface ViewController : UIViewController<ConverterDelegate>
#property (strong,nonatomic) secondViewController *secondview;
#property (strong, nonatomic) IBOutlet UILabel *msgtext;
#property (strong, nonatomic) IBOutlet UIButton *converbutton;
#end
ViewController.m
#import "ViewController.h"
#import "secondViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize msgtext,converbutton,secondview;
- (void)viewDidLoad
{
[super viewDidLoad];
self.secondview=[[secondViewController alloc]init];
secondview.delegate=self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)convertToFahrenheit:(int)celsius{
float fah = ((1.8)*celsius)+32;
msgtext.text = [NSString stringWithFormat:#"the %i deg Celsius equal to %.2f Fah",celsius,fah];
}
#end
SecondViewController.h
#import <UIKit/UIKit.h>
#protocol ConverterDelegate <NSObject>
#required
-(void)convertToFahrenheit:(int)celsius;
#end
#interface secondViewController : UIViewController
#property (nonatomic, assign) id <ConverterDelegate> delegate;
#property (strong, nonatomic) IBOutlet UITextField *textfield;
- (IBAction)convert:(id)sender;
#end
secondViewController.m
#import "secondViewController.h"
#interface secondViewController ()
#end
#implementation secondViewController
#synthesize textfield,delegate;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (IBAction)convert:(id)sender {
[delegate convertToFahrenheit:[textfield.text intValue]];
//[self dismissViewControllerAnimated:YES completion:nil];
}
#end
I was totally confused why my delegate method is not getting called. I a newbie to Objective c please help me.
I hope following code will fulfil your requirements.
ViewController.h
#import <UIKit/UIKit.h>
#import "Calculation.h"
#interface ViewController : UIViewController<CalculationDelegate>
#property (weak, nonatomic) IBOutlet UILabel *msgtext;
#property (weak,nonatomic) IBOutlet UITextField * inputField;
#property (weak, nonatomic) IBOutlet UIButton *converbutton;
-(IBAction)convertMetod:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize msgtext,converbutton,inputField;
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(IBAction)convertMetod:(id)sender
{
Calculation * CalculationObj = [[Calculation alloc] init];
CalculationObj.delegate = self;
[CalculationObj convertCelToFaher:[inputField.text intValue]];
}
-(void)showTheAns:(NSString *)ans
{
NSLog(#"delegate method called");
msgtext.text = ans;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Calculation.h
#import <Foundation/Foundation.h>
#protocol CalculationDelegate <NSObject>
#required
-(void) showTheAns:(NSString *) ans;
#end
#interface Calculation : NSObject
#property (strong,nonatomic) id <CalculationDelegate> delegate;
-(void)convertCelToFaher:(int)celsius;
#end
Calculation.m
#import "Calculation.h"
#implementation Calculation
#synthesize delegate;
-(void)convertCelToFaher:(int)celsius
{
float fah = ((1.8)*celsius)+32;
[delegate showTheAns:[NSString stringWithFormat:#"the %i deg Celsius equal to %.2f Fah",celsius,fah]];
}
#end
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];
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
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.