views:

37

answers:

1

I am new to objective C and I have a c++ background. I want to display a value in the label on the screen. I am calling the label value from the MainView.m. However, the label becomes blank after I click a button instead of printing a value. What is the problem? Here is the code.

MainView.h

@interface MainView : UIView { int a; }

-(int) vr;

@end

MainView.m

-(int) vr { return 100; }

@end

MainViewController.h

@interface MainViewController : UIViewController {

IBOutlet UILabel *myLabel;

NSMutableString *displayString;

MainView *view1; }

@property (nonatomic, retain) UILabel *myLabel;

@property (nonatomic, retain) NSMutableString *displayString;

  • (IBAction)showInfo;

  • (IBAction) pressButton:(id) sender;

@end

MainViewController.m

@synthesize myLabel, displayString;

-(IBAction) pressButton:(id) sender{

[displayString appendFormat:@"%i", view1.vr];
myLabel.text = displayString;}



 - (void)viewDidLoad {
 view1 = [[MainView alloc] init];
 [super viewDidLoad];}

- (void)dealloc {
[view1 dealloc];
[super dealloc];}

I have not mentioned code that had been auto generated. This is enough to get the whole picture. I tried a lot to debug this thing. I believe that IBAction carries out direct command such that

myLabel.text = @"string";

but it does not invoke any method or class. Any subtle ideas? Thanks.

A: 

Few issues:

1
In MainView.h you declare -(id) vr;
And in MainView.m it returns int.

2
Maybe pressButton is not connected to the right event in Interface Builder (it is usually touch up inside).
Try to write to log in this method.

3
Maybe myLabel is not connected to the label in the Interface Builder.
Try to set tome hard-coded string to label's text property.

4
Do you initiate view1 in some place?
Can you post this piece of code too?

5
You can use [displayString appendFormat:@"%i", view1.vr];...

EDIT (due to changes in question):

6
The line [super viewDidLoad]; should be the first line inside viewDidLoad.

7
[view1 dealloc]; - never call dealloc directly on objects. Call release instead. The only place, where you can and should use dealloc is the line [super dealloc]; inside dealloc method.

8
When you format your question/answer in Stack Overflow, remember that each code line should start with at least 4 spaces (or tab). Try reformatting you question by adding 4 spaces in the beginning of each code line.

9
I think that displayString is not initiated. Add the next line in the viewDidLoad: displayString = [NSMutableString new];

Michael Kessler
1) The mistake has been corrected.2) The pressButton is connected to the touch inside.3) The label is connected with the proper field. It works with the myLabel.text = @"string";4) and 5) The updated code is posted. After some analysis, I have found that, pressing the button carries out the command directly coded in the IBAction (See #3). However, IBAction does not invoke any method or class.
meghaljani
See my answer edit...
Michael Kessler
The real problem was the string displayString was not initiated. I added displayString = [NSMutableString new]; and it worked. Thanks for your help.
meghaljani
You welcome! You are more than welcome to mark my answer as accepted.
Michael Kessler