Can not set text for Label - objective-c

In controller I have label property:
#interface BallsViewController : UIViewController <UIInteraction>
#property (weak, nonatomic) IBOutlet UILabel *ScoreLabel;
-(void)UpdateScore:(int)score;
#end
#interface BallsViewController ()
#end
#implementation BallsViewController
#synthesize ScoreLabel;
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)UpdateScore:(int)score
{
NSString *scoreStr =[NSString stringWithFormat:#"%d",score];
[self.ScoreLabel setText:scoreStr];
[self.InfoLabel setText:scoreStr];
}
#end
UpdateScore is protocol method. When I want to set text to ScoreLabel it have value : ScoreLabel UILabel * 0x00000000
It means that it is not initialize?
When I set text, on ui it not change.

Try setting your Label as Strong.
#property (strong, nonatomic) IBOutlet UILabel *ScoreLabel;

Related

Property 'text' not found on object of type 'UIView'

I want to send something from viewController to another viewController which I use NSNotification.
I defined button in first vc and I defined label in second vc. When I click button, I want to see number of click on label but it has problem. I receive an 'Property 'text' not found on object of type 'UIView' error.
How can I solve this?
---firstVC.h---
import <UIKit/UIKit.h>
#interface FirstViewController : UIViewController
- (IBAction)btnTap:(id)sender;
#end
---firstVC.m---
import "FirstViewController.h"
#interface FirstViewController ()
#end
#implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction)btnTap:(id)sender {
[[NSNotificationCenter defaultCenter] postNotificationName:#"not" object:nil];
}
#end
---secondVC.h---
import <UIKit/UIKit.h>
#interface SecondViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIView *lbl;
#property int number;
#end
---secondVC.m---
import "SecondViewController.h"
#interface SecondViewController ()
#end
#implementation SecondViewController
#synthesize number,lbl;
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(notificationMethod:) name:#"not" object:nil];
number=0;
}
-(void) notificationMethod :(NSNotificationCenter *) notification{
number++;
lbl.text = [NSString stringWithFormat: #"%d",number];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#end
Error you are getting comes only when you try to set text on UIView.
Because UIView doesn't have text property and you are trying to set text on UIView.
So, may be you have declared lbl as UIView, change it UILabel and check after that.
EDIT:
Replace in secondVC.h
#property (strong, nonatomic) IBOutlet UIView *lbl;
With
#property (strong, nonatomic) IBOutlet UILabel *lbl;

UISlider values

I'm trying to create an app that has three UISlider used for selecting R, G and B values. The slider should change the colour of a label according to the sliders values.
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
NSInteger color;
IBOutlet UISlider *redSlider;
IBOutlet UISlider *greenSlider;
IBOutlet UISlider *blueSlider;
IBOutlet UILabel *colorLabel;
}
#property (nonatomic, retain) UISlider *redSlider;
#property (nonatomic, retain) UISlider *greenSlider;
#property (nonatomic, retain) UISlider *blueSlider;
colorLabel.textColor = [UIColor colorWithRed: redSlider.value green:greenSlider.value
blue:blueSlider.value alpha:1];
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (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.
}
#synthesize redSlider, greenSlider, blueSlider;
#end
This is what I have so far and I'm totally stuck.
You can do this like...
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
// you need to make sure these are actually created
// and give them all a target of self and action sliderValueChanged
#property (nonatomic, retain) UISlider *redSlider;
#property (nonatomic, retain) UISlider *greenSlider;
#property (nonatomic, retain) UISlider *blueSlider;
// not sure where this is set up
#property (nonatomic, strong) UILabel *colorLabel;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// You may have to create your labels and sliders in here...
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)sliderValueChanged
{
// the sliders have changed so set the colour of the label
// based on the new values of the sliders.
self.colorLabel.textColor = [UIColor colorWithRed:self.redSlider.value
green:self.greenSlider.value
blue:self.blueSlider.value
alpha:1.0];
}
#end
Because you don't actually have a question I'm going to take a guess that your code doesn't compile, because it wouldn't.
colorLabel.textColor = [UIColor colorWithRed: redSlider.value green:greenSlider.value blue:blueSlider.value alpha:1];
The above line doesn't belong in you interface. I would recommend you change the code you currently have to something like.
ViewController.h
#interface ViewController : UIViewController
// You don't need the ivars anymore these get generated with the properties below
#property (nonatomic, retain) IBOutlet UISlider *redSlider;
#property (nonatomic, retain) IBOutlet UISlider *greenSlider;
#property (nonatomic, retain) IBOutlet UISlider *blueSlider;
#property (nonatomic, retain) IBOutlet UILabel *colorLabel
#property (assign) NSInteger color;
#end
ViewController.m
#implementation ViewController
// Also no need for the synthesize as these also get generated automatically
- (void)viewDidLoad
{
[super viewDidLoad];
// To change the actual color when changing the value of a slider move this to a method that gets called on slider changed.
colorLabel.textColor = [UIColor colorWithRed:self.redSlider.value green:self.greenSlider.value self.blue:blueSlider.value alpha:1];
}
#end
And once you actually ask a question I might also be able to help you with that.
Your ViewController.h :-
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
IBOutlet UISlider *redSlider;
IBOutlet UISlider *greenSlider;
IBOutlet UISlider *blueSlider;
IBOutlet UILabel *colorLabel;
}
#property (nonatomic, retain) UISlider *redSlider;
#property (nonatomic, retain) UISlider *greenSlider;
#property (nonatomic, retain) UISlider *blueSlider;
#end
and your ViewController.m will be like this:-
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize redSlider, greenSlider, blueSlider;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self changeLabelColor];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(Void) changeLabelColor
{
colorLabel.backgroundcolor = [UIColor colorWithRed:redSlider.value green:redSlider.value blue:redSlider.value alpha:1.0];
}
#end
As i didn’t copied paste and i have written it, there might be some typo..
textcolor wouldn’t change label color so i changed it to backgroundcolor.
you can also add slider method with IBAction.

Change the green border for an embedded scanner (ZBar)

So I have a ZBarView within a UIViewController (for an embedded scanner) and I was wondering how I could change the Green Border to have it something like this?
#import "ZBarSDK.h"
#interface ScanViewController : UIViewController <ZBarReaderViewDelegate>
#property (nonatomic, strong) IBOutlet ZBarReaderView *readerView;
#property (strong, nonatomic) IBOutlet UILabel *scannedMachineLabel;
#end
- (void)viewDidLoad
{
[super viewDidLoad];
_readerView.readerDelegate = self;
// ensure initial camera orientation is correctly set
UIApplication *app = [UIApplication sharedApplication];
[_readerView willRotateToInterfaceOrientation: app.statusBarOrientation
duration: 0];
}

UITextField: set text from a label

I'm trying to write my first iPad app using Xcode 4: it's based on "Tabbed Application" template with two views. On the first view, user selects a City (label displays selection) and on the second wiew there is a IBOutletCollection(UITextField) NSArray *playersData. I want to set the city selected on first view as a default city on second view. I have checked storyboard connections and they seem to be ok.
I get nothing. Any idea?
First view :
#import
#interface pruebaF1FirstViewController : UIViewController
- (IBAction)selectCity:(UIButton *)sender;
#property (weak, nonatomic) IBOutlet UILabel *citySelected;
#end
First view implementation :
#import "pruebaF1FirstViewController.h"
#import "pruebaF1SecondViewController.h"
#interface pruebaF1FirstViewController ()
#property pruebaF1SecondViewController *secondView;
#end
#implementation pruebaF1FirstViewController
#synthesize citySelected;
#synthesize secondView;
- (void)viewDidLoad
...
- (IBAction)selectCity:(UIButton *)sender {
NSString *currentCity =[sender currentTitle];
citySelected.text=#"";
citySelected.text =[citySelected.text stringByAppendingString:currentCity];
/*writes null*/
secondView.defaultCity.text=[NSString stringWithFormat:#"%#" ,currentCity];
NSLog(#"%#",secondView.defaultCity.text);
}
#end
Second view header
#import <UIKit/UIKit.h>
#interface pruebaF1SecondViewController : UIViewController <UITextFieldDelegate>
#property (strong, nonatomic) IBOutletCollection(UITextField) NSArray *playersData;
#property (retain, nonatomic) IBOutlet UITextField *defaultCity;
- (IBAction)eraseData:(UIButton *)sender;
- (IBAction)savePlayersData:(UIButton *)sender;
- (IBAction)termsPopUp:(UIButton *)sender;
/*- (void)writeDefaultCity:(NSString *)currentCity;*/
#end
Second view implementation
#import "pruebaF1SecondViewController.h"
#import "pruebaF1FirstViewController.h"
#interface pruebaF1SecondViewController ()
#property (nonatomic, strong)pruebaF1FirstViewController *prueba;
#end
#implementation pruebaF1SecondViewController
#synthesize playersData;
#synthesize defaultCity;
#synthesize prueba;
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[self setPlayersData:nil];
[self setDefaultCity:nil];
[super viewDidUnload];
}
/* Return or Done button dismiss keyboard*/
-(BOOL)textFieldShouldReturn:(UITextField *)boxes
{
for (UITextField *boxes in playersData) {
[boxes resignFirstResponder];
}
return YES;
}
....
/*Trying to get city's name from first view in two ways*/
/*-(void)setDefaultCity
{
NSString *defecto=prueba.citySelected.text;
self.defaultCity.text = defecto;
}*/
- (IBAction)eraseData:(UIButton *)sender {
for (UITextField *boxes in playersData) {
boxes.text = #" ";
}
}
/*
-(void)writeDefaultCity:(NSString *)currentCity
{
defaultCity.text =[NSString stringWithFormat:#"%#" ,currentCity];
NSLog(#"Ciudad elegida: %#",currentCity);
}*/
....
#end
Views are generally not loaded until they are displayed on the device, and may be unloaded at any time when not visible to save memory. This means that when you're setting the 'text' property of the 'defaultCity', that label has not yet been created.
Instead you should add a NSString * property to pruebaF1SecondViewController and set the value of the defaultCity label in viewDidLoad:
In the header:
#property (nonatomic, weak) UILabel *defaultCityLabel;
#property (nonatomic, strong) NSString *defaultCity;
In the implementation
- (void)viewDidLoad
{
[super viewDidLoad];
defaultCityLabel.text = defaultCity;
}
And in the first view controller, assign the string value instead of the label value.

Delegation and Modal View Controllers

According to the View Controller Programming Guide, delegation is the preferred method to dismiss a modal view.
Following Apple's own Recipe example, i have implemented the following, but keep getting warnings that the addNameController:didAddName method is not found...
NameDelegate.h
#protocol NameDelegate
- (void)addNameController:(AddName *)addNameController didAddName:(NSString *)name;
#end
AddName.h
#interface AddName : UIViewController {
UITextField *nameField;
id delegate;
}
- (IBAction)doneAction;
- (id)delegate;
- (void)setDelegate:(id)newDelegate;
#property (nonatomic, retain) IBOutlet UITextField *nameField;
#end
AddName.m
- (IBAction)doneAction {
[delegate addNameController:self didAddName:[nameField text]];
}
- (id)delegate {
return delegate;
}
- (void)setDelegate:(id)newDelegate {
delegate = newDelegate;
}
ItemViewController.h
#import "NameDelegate.h"
#interface ItemViewController : UITableViewController <NameDelegate>{
}
#end
ItemViewController.m
- (void)addItem:(id)sender {
AddName *addName = [[AddName alloc] init];
addName.delegate = self;
[self presentModalViewController:addName animated:YES];
}
- (void)addNameController:(AddName *)addNameController didAddName:(NSString *)name {
//Do other checks before dismiss...
[self dismissModalViewControllerAnimated:YES];
}
I think all the required elements are there and in the right place?
Thanks
You haven't specified that the delegate property of AddName has to conform to the NameDelegate protocol.
Use this code in AddName.h:
#import "NameDelegate.h"
#interface AddName : UIViewController {
UITextField *nameField;
id <NameDelegate> delegate;
}
#property(nonatomic, retain) IBOutlet UITextField *nameField;
#property(nonatomic, assign) id <NameDelegate> delegate;
- (IBAction)doneAction;
#end