Why I cannot set text property for a UILabel? - objective-c

I've decleared a UILabel in my view controller
#interface ViewController : UIViewController
{
UILabel *statusText;
}
#property (nonatomic,retain) IBOutlet UILabel *statusText;
-(IBAction)buttonPressed:(id)sender;
#end
Now I want the UILabel's title to change when my button is pressed, but my statusText can not set the properly. for example:
- (IBAction)buttonPressed:(id)sender{
NSString *title = [sender titleForState:UIControlStateNormal];
NSString *newText = [[NSString alloc]initWithFormat:#"%#button pressed",title];
statusText.text = #"helloworld";
NSLog(#"%#",statusText.text);
[newText release];
}
the log returns
2012-01-25 00:46:27.663 buttonfun[34247:f803] (null)
It means that the text property "helloworld" has not been set. Why?

Try logging this instead:
NSLog(#"%#",statusText);
If it's also null, it means you haven't hooked up your statusText IBOutlet to your UILabel in Interface Builder (you need to ctrl-drag from your File's Owner to the label and then select statusText).

Related

UILabel in detail view is not being populated

I have a UITableView populated with an array of data gathered from an SQLite database. When a cell is selected a detail view is loaded relevant to that cell. The title on the navigation controller changes ok but a UILabel on the detail view does not get populated.
Relevant tableViewController code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
ExhibitorDetailViewController *exhibitorDetailViewControllerInstance = [[ExhibitorDetailViewController alloc] init];
exhibitorDetailViewControllerInstance.theTitle = [self.tableView cellForRowAtIndexPath:indexPath].textLabel.text;
exhibitorDetailViewControllerInstance.exhibitorDetailModal = [arrayOfExhibitors objectAtIndex:indexPath.row];
// Have tried setting the text before the detail view is loaded wiht the line below
exhibitorDetailViewControllerInstance.LabelExhibitorDescription.text = arrayOfExhibitors.description;
[self.navigationController pushViewController:exhibitorDetailViewControllerInstance animated:YES];
}
The viewDidLoad event of the detail view controller:
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = _theTitle; //This sets the title ok
NSLog(#"0 - %#", _exhibitorDetailModal.description); //This property is what I want to fill the UILabel with
self.LabelExhibitorDescription.text = _exhibitorDetailModal.description; // This does nothing
self.view.backgroundColor = [UIColor colorWithRed:0.6 green:0.8 blue:0.8 alpha:2.0f]; // If I don't set the background colour here it loads up black (despite being set in the storyboard)
NSLog(#"label text = %#", self.LabelExhibitorDescription.text); // This shows the text box being null despite having its text property set above
}
Detail view header file:
#interface ExhibitorDetailViewController : UIViewController
{
}
#property (nonatomic, retain) NSString *theTitle;
#property (strong, nonatomic) IBOutlet UILabel *nameLabel;
#property (strong, nonatomic) IBOutlet UILabel *categoryLabel;
#property (strong, nonatomic) IBOutlet UILabel *descriptionLabel;
#property (strong ,nonatomic) NSArray *exhibitorDetailModal;
#property (strong, nonatomic) IBOutlet UILabel *LabelExhibitorDescription;
#end
Why isn't the UILabel text being populated?
Thanks.
Edit:
I have logged the 'outgoing' description of the array - [arrayOfExhibitors objectAtIndex:indexPath.row] and this shows the correct data as expected which is the same as the _exhibitorDetailModal.description which is shown in the detail view.
To get this working in the end I rewrote the entire code and tidied it up along the way.
To get the UILabel displaying properly I first passed the description property of the exhibitorDetailModal object to an NSString.
_stringExhibitorDescription = _exhibitorDetailModal.description;
I then initialised the UILabel.
self.labelExhibitorName = [[UILabel alloc]init];
Then set the text of the UILabel to be that of the NSString above
[_labelExhibitorName setText:_stringExhibitorName];
Then added the UILabel as a subView of UIView.
[self.view addSubview:_labelExhibitorDescription];

Xcode: Passing label to second view?

I have two location labels on my first view controller I need them to be displayed on another view controller when opened, what is the best way to implement this?
The labels i want to pass look like this:
latitudeLabel.text = [NSString stringWithFormat:#"latitude=%f", location.coordinate.latitude];
longitudeLabel.text = [NSString stringWithFormat:#"longitude=%f", location.coordinate.longitude];
i had created a sample for sharing data between classes.. All you need to do is explained here
Before you push secondViewController you can add your labels as subviews of second ViewController's view
SecondViewController *cntr = [[SecondViewController alloc] initWithNibName:#"" bundle:nil];
[cntr.view addSubview latitudeLabel];
[cntr.view addSubview longitudeLabel];
[self.navigationController pushViewController:cntr animated:YES]
You have to create identical labels on both view controllers and pass only the string data. You can pass parameters from one class to another with properties (since they are public).
In first view controller .m:
- (void)openSecondViewController {
SecondViewController *controller = [SecondViewController alloc] init];
controller.data = [NSArray arrayWithObjects:#"First String", #"Second String", nil];
[self.navigationController pushViewController:controller];
}
In second view controller .h:
#interface SecondViewController : UIViewController
#property (weak, nonatomic) id data;
#property (weak, nonatomic) IBOutlet UILabel *label1;
#property (weak, nonatomic) IBOutlet UILabel *label2;
#end
In second view controller .m:
- (void)setData:(id)data {
self.label1.text = [data objectAtIndex:0];
self.label2.text = [data objectAtIndex:1];
}

xcode: TextField.text return nil no matter what is the input: [New Referencing Outlet]?

I have a tab-bar application.
In one of the view(.xib), there is a text field and a button. The button supposed to save the text inputted in textField.
However, no matter what i typed, it always read (nil) in textField.text in debug.
When i right click the textField in .xib, i cannot drag the [New Referencing Outlet] to [File's Owner]. i can only drag the [delegate] to [File's Owner]. Does anyone have idea on why i can't do so?
Below the the .h file:
#interface AddDebtorViewController : UIViewController
{
UITextField *tDebtorName;
}
#property (retain, nonatomic) IBOutlet UITextField *tDebtorName;
-(IBAction)returnButPressed:(id)sender;
-(IBAction)addDebtorButPressed:(id)sender;
#end
Below is the .m file:
#import "AddDebtorViewController.h"
#implementation AddDebtorViewController
#synthesize tDebtorName;
- (IBAction)addDebtorButPressed:(id)sender
{
NSString *debtorName = [[NSString alloc] init];
debtorName = tDebtorName.text;
NSLog(debtorName);
}
...
#end
You must select File's Owner, press right button mouse, find your outlet and link it with the ui element.

How to get the current text from a UILabel outlet in another class?

I have a basic calculator app with a label, some numeric buttons, and some simple operatin buttons. In this app, on the bottom of the screen, the top of another view(AdvancedView) is showing and there is a button on that, and when clicked, the subview slides up to the bottom of the UILabel. I'm able to access the current text in the UILabel from the CalcViewController class because there's an outlet:
#property (strong, nonatomic) IBOutlet UILabel *display;
in the .h simply by calling:
self.display.text
But i have a squareRoot button in my AdvancedView (the view that slides up) that also needs access to the UILabel text. How can I both get and set the UILabel text from another class?
You can do this:
NSString *text = nil;
UIViewController *otherViewController;
for (UIView *view in [otherViewController.view subviews])
{
if ([view isKindOfClass:[UILabel class]])
{
UILabel *label = (UILabel *) view;
text = label.text;
break;
}
}
if (text == nil)
{
NSLog(#"Hmm, couldn't find the view...");
}
you have to #synthesize your text as well
#synthesize display
then in the file you want to use it in import the file you made the label in and then give it a pointer to that class. (in the new .h file)
CalcViewController* calcViewController;
then inside your code you would do:
calcViewController.display.text;

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.