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.
Few issues:
1
In
MainView.hyou declare-(id) vr;And in
MainView.mit returns int.2
Maybe
pressButtonis 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
myLabelis 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
view1in 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 insideviewDidLoad.7
[view1 dealloc];– never calldeallocdirectly on objects. Callreleaseinstead. The only place, where you can and should usedeallocis the line[super dealloc];insidedeallocmethod.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
displayStringis not initiated. Add the next line in theviewDidLoad:displayString = [NSMutableString new];