0
votes

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.

1
what happens if u add in other text in the stringWithFormat..does that get shown? if it does, its a problem with initializing view1, if not it is a problem with initializing the displayStringJesse Naugher

1 Answers

0
votes

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];