UITextfield is not showing the assigned NSString - objective-c

UITextfield is showing null eventhough nsstring has value...
I've taken uitextfield in IB. Connection is correct in IB.
This is .h file
{
IBOutlet UITextField *dobTextField;
}
#property (nonatomic, retain) UITextField *dobTextField;
In .m file
#synthesize dobTextField;
-(void)displayDOB:(NSString*)str
{
NSLog(#"%#", str);
dobTextField.text = str;
}
In the log it is showing some value... but dobTextField is showing null...i tried
dobTextField.text = [NSString StringWithValue:#"%#", str];
but no use....
Please help me out guys.

Try doing...
NSLog(#"%#", self.dobTextField);
is it nil? (it shouldn't be)
Sounds like you possibly haven't connected up the outlet in IB.
EDIT
Hmm... second thoughts I think you're confusing things.
Remove the ivar from the .h file. Just use the #property.
#property (nonatomic, retain) IBOutlet UITextField *dobTextField;
Remove the #synthesize from the .m file.
In displayDOB function use this...
self.dobTextField.text = str;
This will ensure that everything is referring to the same thing and also uses best practise methods when it comes to properties.

It seems that you have not set your outlet of UITextField in xib
Check that in your interface builder
Also,
change this line
#property (nonatomic, retain) UITextField *dobTextField;
to
#property (nonatomic, retain) IBOutlet UITextField *dobTextField;
Also, change this line
dobTextField.text = str;
to
self.dobTextField.text = str;
Hope this helps you..

Related

Proper declaration of outlets in Objective-C

I am a little bit confuse about the this:
Why do some programmers use this at their .h file
{
IBOutlet UIScrollView* scrollView;
IBOutlet UIPageControl* pageControl;
}
#property (nonatomic, retain) UIView *scrollView;
#property (nonatomic, retain) UIPageControl* pageControl;
Instead just this...
#property (nonatomic, retain) UIView *scrollView;
#property (nonatomic, retain) UIPageControl* pageControl;
I am new to XCode and Objective C , I will be glad if there is anyone who can explain me this. Thanks.
In recents versions of the toolchain, it's enough just doing this:
#property (nonatomic, retain) IBOutlet UIView *scrollView;
(If you dont put the IBOutlet you wont be able to connect it to the interface builder)
Some people still do it in the old way, some of them just tradition, some dont know it can be skipped, or it's because they are just old projects.
It was only relatively recently that you could declare properties without a corresponding instance variable (the compiler will create the backing instance variable for you now if necessary, but that wasn't always the case).
Outlets are properties
properties need an ivar (unless some very specific case)
Since Xcode 4.3 or 4.4, ivars are automatically added and synthesized by the compiler if absent.

Objective C, array operations

I am trying to do actions on array's elements but i really don't understand why my code doesn't work:
here is my .h:
#interface ViewController : UIViewController
{
NSArray *tableauScore;
UILabel * modificationScore;
}
#property (weak, nonatomic) IBOutlet UILabel *nom1;
#property (weak, nonatomic) IBOutlet UILabel *nom2;
#property (weak, nonatomic) IBOutlet UILabel *nom3;
#property (weak, nonatomic) IBOutlet UILabel *nom4;
#property (weak, nonatomic) IBOutlet UILabel *bsc1;
#property (weak, nonatomic) IBOutlet UILabel *bsc2;
#property (weak, nonatomic) IBOutlet UILabel *bsc3;
#property (weak, nonatomic) IBOutlet UILabel *bsc4;
#end
my .m:
tableauScore = [NSArray arrayWithObjects:nom4, nom3, nom2, nom1, bsc1, bsc2, bsc3, bsc4, nil];
for(int i = 0; i < 8; i++)
{
modificationScore = [tableauScore objectAtIndex:i];
modificationScore.hidden = NO;
modificationScore.center = CGPointMake(modificationScore.center.x, modificationScore.center.y -40);
}
The issue is that i have a thread point at "modificationScore = [tableauScore objectAtIndex:i];" line and I don't know why. I saw a lot of topics but no one can help me. Is anybody of you have an idea?
thank you!
I assume by "thread point" you mean a crash? If so, one of the properties you add to the array is probably nil.
Check the number of elements in the array ([tableauScore count]) before you loop over them; don't assume there are eight. Or, even better, use the syntax to loop over all elements:
for (a in tableauScore) {
I would not globally define modificationScore if you don't really need it.
I would do:
for(UILabel *tempLabel in tableauScore)
{
tempLabel.hidden = NO;
tempLabel.center = CGPointMake(modificationScore.center.x, modificationScore.center.y -40);
}
I don't know what you want to achieve with chaning the center, that needs to be changed
[NSArray arrayWithObjects:] will only create an NSArray up to the first nil given, so if one of the UILabels is nil you will not get an array of size 8 as you assume, but only an array containing the UILabels up to that point.
Since you have a hard coded loop size, you're probably ending up indexing outside of the array.
If you are changing objects in tableauScore you cannot use an NSArray because NSArray is immutable.
Use a NSMutableArray, reconfigure your labels and use replaceObjectAtIndex:withObject: to change them in the array.

create control with properties within it

I have control something like this
#import <Foundation/Foundation.h>
#import "AQLevelMeter.h"
#import "AQPlayer.h"
#import "AQRecorder.h"
#interface SpeakHereController : NSObject {
IBOutlet UIBarButtonItem* btn_record;
IBOutlet UIBarButtonItem* btn_play;
IBOutlet UILabel* fileDescription;
IBOutlet AQLevelMeter* lvlMeter_in;
AQPlayer* player;
AQRecorder* recorder;
BOOL playbackWasInterrupted;
BOOL playbackWasPaused;
}
#property (nonatomic, retain) UIBarButtonItem *btn_record;
#property (nonatomic, retain) UIBarButtonItem *btn_play;
#property (nonatomic, retain) UILabel *fileDescription;
#property (nonatomic, retain) AQLevelMeter *lvlMeter_in;
#property (readonly) AQPlayer *player;
#property (readonly) AQRecorder *recorder;
#property BOOL playbackWasInterrupted;
#property BOOL isReport;
#property CFStringRef recordFilePath;
- (IBAction)record: (id) sender;
- (IBAction)play: (id) sender;
-(void) InitializeThePlayer;
#end
as we can see I added many properties like
#property BOOL isReport;
#property CFStringRef recordFilePath;
then I created uiview contains this control
#import <UIKit/UIKit.h>
#class SpeakHereController;
#interface SpeakHereViewController : UIViewController {
IBOutlet SpeakHereController *controller;
}
Now I want to access the properties of the control object so I say
- (void)viewWillAppear:(BOOL)animated {
[self ReportDirectory];
[controller setIsReport:self.iSReport ];
//controller.isReport = self.iSReport ;
[controller setRecordFilePath:(CFStringRef) self.DICOMpath];
//controller.recordFilePath = ;
}
the problem is that at the lines
[controller setIsReport:self.iSReport ];
[controller setRecordFilePath:(CFStringRef) self.DICOMpath];
there is warning say that
warning: no '-setIsReport:' method found
I made
#synthesize isReport;
#synthesize recordFilePath;
also if I replaced #class SpeakHereController; by #import "SpeakHereController.h" it raise a lot of errors , U can download the sample code from apple
and if I said controller.isReport = self.iSReport ; it raise error request for member 'isReport' in something not a structure or union
My question is how to call the properties in this control , am I missing something
Best regards
I tried
At the top of SpeakHereViewController.m you will need
import "SpeakHereController.h"
Otherwise when SpeakHereViewController.m is compiled, it is completely unaware of what methods and properties your SpeakHereController class has
It should run fine even with the warning because the property does exist. However, I agree with you that the warning needs to be dealt with.
You should synthesize those properties in .m file.
The syntax is like:
#synthesize isReport, recordFilePath;

Is declaration of variable necessary in objective-c

In the following code:
#interface UnitConverterViewController : UIViewController {
UITextField *tempText;
}
#property (strong, nonatomic) IBOutlet UILabel *resultLabel;
#end
I've seen the same result achieved with out having
{
UITextField *tempText;
}
So, is this really necessary?
No it's not necessary as of Objective-c 2.0.
see: Do declared properties require a corresponding instance variable?

Objective C - Get a class to return a value

I rarely ask questions, but this one is frustrating me as I can not find an answer anywhere!
I just need to call the function in GameChallenges.m and return the value to the view controller. GameChallenges.m will be called by different view controllers, thats why its separate. Please help!
I have a separate class file called GameChallenges.
This has a function/method in it:
in the .h
#class StatsViewController;
#interface GameChallenges : NSObject {
StatsViewController* statsController;
NSString* challengeTitle;
}
#property (nonatomic, retain) IBOutlet StatsViewController* statsController;
#property (assign) NSString* challengeTitle;
-(NSString*)checkChallenge:(int)challegeID;
#end
in the .m
#import "GameChallenges.h"
#import "StatsViewController.h"
#implementation GameChallenges
#synthesize challengeTitle,statsController;
-(NSString*)checkChallenge:(int)challegeID{
if(challegeID==1){
self.challengeTitle = #"Some Text.";
return challengeTitle;
}else if(challegeID==2){
self.challengeTitle = #"Some Other Text.";
return challengeTitle;
}
}
From a view controller called StatsViewController I am calling this method
in the .h
#class GameChallenges;
#interface StatsViewController : UIViewController {
UILabel* challengeIDDescText;
}
#property (nonatomic, retain) IBOutlet UILabel* challengeIDDescText;
#property (nonatomic, retain) IBOutlet GameChallenges* challenges;
#end
in the .m
[challenges checkChallenge:tempString];
challengeIDDescText.text = challenges.challengeTitle;
Your code is quite weird, I'd say this is what's happening:
-[GameChallenges checkChallenge] seems to expect an int and you call it with a variable called tempString that I guess is an NSString *. More likely than not, your method is ending without assigning challengeTitle and without a valid return value. Fix it with return nil as last statement and passing an int.
This kind of problems are very easy to solve using the debugger.
Also, have a look to Apple samples.
I'm beginner in objective-C, however, code in the GameChallenges.m looks weird to me... Wouldn't this be better?
challengeIDDescText.text = [challenges checkChallenge:challengeId];