specifying textField using UITextField delegate - objective-c

Hi i'm new to software for iphone/ipad apps.
I am using the textField delegate method:
"(BOOL)textFieldShouldReturn:(UITextField *)textField".
I have gotten to the point where when the user enter a name and presses 'enter' and a label displays what the user just wrote. This is just two lines of code like so:
labelChange.text = textField.text; //labelChange is my label in IB
return YES;
Since i have several textfields in IB, this code works for all of them. I'm not sure how to be specific with the code and have this label change for only ONE of my textFields.
My .h files looks like this.
#interface FirstViewController : UIViewController <UITextFieldDelegate> {
IBOutlet UILabel *labelChange;
IBOutlet UITextField *userName;
IBOutlet UITextField *homeValue;
IBOutlet UITextField *downPayment;
IBOutlet UITextField *textField;
}
#property (nonatomic,retain) UILabel *labelChange;
#property (nonatomic,retain) UITextField *userName;
#property (nonatomic,retain) UITextField *homeValue;
#property (nonatomic,retain) UITextField *downPayment;
#property (nonatomic,retain) UITextField *textField;
#end
I want my label to change only when user types in the text field labeled "userName".I'm not sure how to do this, am I missing something?In IB, I connected all my textfield delegates to 'Files Owner'. Any advice would be really helpful. thanks!

Assign each of your text fields a tag number i.e.
textField1.tag = 1
textField2.tag = 2
etc...
Then in your -(BOOL)textFieldShouldReturn:(UITextField *)textField you can do a:
switch (textField.tag) {
case 1:
labelChange1.text = textField.text;
break;
case 2:
labelChange2.text = textField.text;
break;
etc... etc...
}

Related

Changing label's data using textField inout

I want to change the label's text (which is embedded in a tableViewCell) using a textField data (which is embedded outside the tableViewCell).
Can anyone share the code please using objective-C.
Here is my TableViewCell.h file.
#import <UIKit/UIKit.h>
#interface TableViewCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UILabel *label1;
#property (strong, nonatomic) IBOutlet UILabel *label2;
#end
Here is my storyboard controller file
#import <UIKit/UIKit.h>
#interface ViewController:UIViewController<UITableViewDelegate,UITableViewDataSource>
{
NSMutableArray *contacts;
}
- (IBAction)leftBtn:(id)sender;
- (IBAction)rightBtn:(id)sender;
- (IBAction)nextBtn:(id)sender;
- (IBAction)SendBtn:(id)sender;
#property (strong, nonatomic) IBOutlet UITableView *tabV1;
#property (strong, nonatomic) IBOutlet UITextField *txtField;
#end
Here I have the button action connection where I should I write the actual code to change the label's data using a text field.
- (IBAction)leftBtn:(id)sender {
}

Multiplication of Integers in Textfield - Xcode 4.4

I am trying to multiply the integer values in three different text fields.
#property (nonatomic,strong) IBOutlet UITextField *textField;
#property (nonatomic,strong) IBOutlet UITextField *textField2;
#property (nonatomic,strong) IBOutlet UITextField *textField3;
#property (nonatomic,strong) IBOutlet UILabel *total;
I am trying to display the product in a UILabel. Can somebody please help with the implementation? Thanks, so much!
to display the product in the UILabel, set the label text this way (assuming the textFields all contain ints):
total.text = [NSString stringWithFormat: #"%i", (textField.text.intValue * textField2.text.intValue * textField3.text.intValue)];
you might want to put this in a separate method so you can call it from anywhere, like whenever the user updates the textFields with new numbers

UIButton removeFromSuperview

I went through guide from Apple "Your first iOS app"
and now I have a button, which is not declared in ViewController:
#interface HelloWorldViewController : UIViewController <UITextFieldDelegate>
- (IBAction)changeGreeting:(id)sender;
#property (weak, nonatomic) IBOutlet UITextField *textField;
#property (weak, nonatomic) IBOutlet UILabel *label;
#property (copy, nonatomic) NSString *userName;
#end
Now i can remove label (and textField), using [label removeFromSuperview]; but i dont understand how to do it with button. Can someone help?
You should add an IBOutlet to the button as you did for the textfield and the label:
#property (weak, nonatomic) IBOutlet UITextField *textField;
#property (weak, nonatomic) IBOutlet UILabel *label;
#property (weak, nonatomic) IBOutlet UIButton *button; // Don't forget to link to this from Interface Builder
// ...
Then you can remove the button using:
[button removeFromSuperview];
Also note that the tutorial you linked to says:
The sender parameter in the action method refers to the object that is sending the action message (in this tutorial, the sender is the button).
So if you want to remove the button when it is tapped (inside changeGreeting:), then you don't need the IBOutlet because you already have a reference to the button in the sender parameter:
- (IBAction)changeGreeting:(id)sender
{
UIButton *button = (UIButton *)sender;
// ...
[button removeFromSuperview];
// ...
}
You need to declare the button in the controller like you did as an IBAction and this time declare this as an Outlet(IBoutlet).. this way you will get its reference in code..
Alternatively .. you can set a tag to the button in Interface Builder
..
and then retrieve in code using viewWithTag: method

UISlider's value is not shown by UILabel

In my app, I have a Settings view, where the user can use UISlider to set value from 50 to 1000. The selected value is then shown in a UILabel.
The problem is that the label does not display at all. The Action and the IBOutlets are connected in IB. The NSLog displays the value correctly on the console.
I can't figure out what is going on....help is needed :)
I have the following:
IBOutlet UILabel *sliderLabel;
IBOutlet UISlider *slider;
...
#property (nonatomic, retain) UILabel *sliderLabel;
#property (nonatomic, retain) UISlider *slider;
-(IBAction) sliderValueChanged:(id) sender;
and
#synthesize sliderlabel, slider;
- (IBAction) sliderValueChanged:(UISlider *) sender {
NSString *sliderValue = [NSString stringWithFormat:#"%d feet",(int)[sender value]];
NSLog(#"%#", sliderValue);
[[self sliderLabel] setText: sliderValue];
}
Thanks, and regards.

How do I hide a button when an IBAction is performed?

I am attempting to hide a NSButton that performs miniaturize when a different NSButton on the interface is clicked. My attempts thus far have been unsuccessful, this is what I have tried:
.h file:
#interface AppDelegate : NSObject <NSApplicationDelegate> {
IBOutlet NSWindow *window;
IBOutlet WebView *webView;
IBOutlet NSButton *doMinimize;
}
#property (assign) IBOutlet NSWindow *window;
#property (assign) IBOutlet NSButton *button;
#property (nonatomic, retain) IBOutlet WebView *webView;
.m file:
#implementation AppDelegate
#synthesize window;
#synthesize webView;
#synthesize doMinimize;
- (IBAction)toggleFullscreen:(id)sender
{
...
[doMinimize setEnabled:NO];
[doMinimize setTransparent:YES];
...
}
It appears that no matter in what action I try to disable and make the button transparent, it doesn't seem to respond to anything. Do I have to give the button it's own class to make this work? If so, how would I then be able to modify that button from an IBAction inside of a different class?
I apologize in advance if my question is silly, I'm relatively new to the world of Objective-C and am just now starting to get my feet wet.
Thanks in advance.
Have you tried -setHidden:?
[doMinimize setHidden:YES];