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];
Related
I am trying to hide two buttons when I press one button on the ViewController. I can only seem to hide the button I click but using [sender setHidden:YES];. The buttons I have:
- (IBAction)Button1:(UIButton*)sender;
- (IBAction)Button2:(UIButton*)sender;
I can't seem to use Button2.hidden = YES in Button1. Is there a way round this?
You should create button property in your viewController, and use it to hide button:
#interface ViewController ()
#property (nonatomic, strong) IBOutlet UIButton* button1;
#property (nonatomic, strong) IBOutlet UIButton* button2;
- (IBAction)action1:(id)sender;
- (IBAction)action2:(id)sender;
#end
In the actions:
- (void)setButtonsHidden:(BOOL)hidden
{
_button1.hidden = hidden;
_button2.hidden = hidden;
}
#pragma mark Actions
- (IBAction)action1:(id)sender
{
[self setButtonsHidden:YES];
}
- (IBAction)action2:(id)sender
{
[self setButtonsHidden:YES];
}
Also you can use NSArray to store all buttons references:
#interface ViewController ()
#property (nonatomic, strong) IBOutletCollection(UIButton) NSArray* allButtons;
<.....>
In setButtonsHidden: method:
- (void)setButtonsHidden:(BOOL)hidden
{
[_allButtons enumerateObjectsUsingBlock:^(UIButton* obj, NSUInteger idx, BOOL *stop) {
obj.hidden = hidden;
}];
}
Viewcontroller.h
#property (weak, nonatomic) IBOutlet UIButton *button1;
#property (weak, nonatomic) IBOutlet UIButton *button2;
- (IBAction)button1:(UIButton *)sender;
- (IBAction)button2:(UIButton *)sender;
Viewcontroller.m
- (IBAction)button1:(UIButton *)sender
{
_button1.hidden = YES;
_button2.hidden = YES;
}
- (IBAction)button2:(UIButton *)sender
{
_button1.hidden = YES;
_button2.hidden = YES;
}
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.
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.
I have some labels on UITableviewcell I want to change the label font in the setter but from some odd reason in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
When I debugging I can see nothing inside the cell get allocated.
When I drop the setters everything work fine but with system font which I don't want to use.
Cell.h file
#import <UIKit/UIKit.h>
#interface CheckoutCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UILabel *labelQuantity;
#property (weak, nonatomic) IBOutlet UILabel *labelMainDescription;
#property (weak, nonatomic) IBOutlet UILabel *labelCountName;
#property (weak, nonatomic) IBOutlet UILabel *labelInchSize;
#property (weak, nonatomic) IBOutlet UILabel *labelMMsize;
#property (weak, nonatomic) IBOutlet UILabel *labelStaticTotal;
#property (weak, nonatomic) IBOutlet UILabel *labelTotalPrice;
#property (weak, nonatomic) IBOutlet UILabel *label_ItemPrice;
#property (weak, nonatomic) IBOutlet UIButton *btnBuyAnotherProduct;
#property (weak, nonatomic) IBOutlet UIButton *btnAddAnotherSet;
#end
cell.m file
#import "CheckoutCell.h"
#implementation CheckoutCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
-(void) setLabelCountName:(UILabel *)labelCountName
{
[self setLabel:labelCountName];
}
-(void) setLabelMMsize:(UILabel *)labelMMsize
{
[self setLabel:labelMMsize];
}
-(void) setLabel_ItemPrice:(UILabel *)label_ItemPrice
{
[self setLabel:label_ItemPrice];
}
-(void) setLabelInchSize:(UILabel *)labelInchSize
{
[self setLabel:labelInchSize];
}
-(void) setLabelMainDescription:(UILabel *)labelMainDescription
{
[labelMainDescription setFont:[UIFont fontWithName:#"MuseoSans-500" size:14]];
}
-(void) setLabelQuantity:(UILabel *)labelQuantity
{
[labelQuantity setFont:[UIFont fontWithName:#"MuseoSans-500" size:18]];
}
-(void) setLabelTotalPrice:(UILabel *)labelTotalPrice
{
[labelTotalPrice setFont:[UIFont fontWithName:#"MuseoSans-500" size:14]];
}
-(void) setLabelStaticTotal:(UILabel *)labelStaticTotal
{
[labelStaticTotal setFont:[UIFont fontWithName:#"MuseoSans-500" size:14]];
}
//BTN setters
-(void) setBtnAddAnotherSet:(UIButton *)btnAddAnotherSet
{
[btnAddAnotherSet.titleLabel setFont:[UIFont fontWithName:#"MuseoSans-500" size:12]];
btnAddAnotherSet.titleLabel.textAlignment = UIBaselineAdjustmentAlignCenters;
}
-(void) setBtnBuyAnotherProduct:(UIButton *)btnBuyAnotherProduct
{
[btnBuyAnotherProduct.titleLabel setFont:[UIFont fontWithName:#"MuseoSans-500" size:12]];
btnBuyAnotherProduct.titleLabel.textAlignment = UIBaselineAdjustmentAlignCenters;
}
//generic setter
-(void) setLabel:(UILabel *)label
{
[label setFont:[UIFont fontWithName:#"MuseoSans-300" size:10]];
}
#end
I believe you should just set the text of the label not create and set the label again. for example
-(void) setLabelCountName:(UILabel *)labelCountName{
[self setLabel:labelCountName];
}
should be
-(void) setLabelCountName:(NSString *)labelCountName{
[self.labelCountName setText:labelCountName];
}
also you should change the Font in StoryBoard designer or in initWith... method.
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];
}