Search code examples
objective-ctextintegerlabelsynthesizer

@synthesize is not working and basic operations are not working in Objective-C


I am unsure why this code will not work. When I click a button (action: buttonclick) it should change the two text boxes' (MyTextLabel and MyTextLabel2) text increment value "r" by one. Here is the code:

MainView.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface MainView : UIView {
    IBOutlet UIButton *MyButton;
    IBOutlet UILabel *MyTextLabel;
    IBOutlet UILabel *MyTextLabel2;
}

@property (nonatomic, retain) UIButton *MyButton;
@property (nonatomic, retain) UILabel *MyTextLabel;
@property (nonatomic, retain) UILabel *MyTextLabel2;


- (IBAction)buttonclick;
@end

MainView.m:

#import "MainView.h"
#include <stdlib.h>
#include <libc.h>

@implementation MainView

@synthesize MyButton, MyTextLabel, MyTextLabel2;

int r;

- (IBAction)buttonclick {
    r++
    if(r < 50) {
        MyTextLabel.text = @"< 50";
    }
    else {
        MyTextLabel2.text = @"=> 50";
    }
}
@end

Solution

  • I have a feeling something's wrong with the way you're using int r. Try putting static int r; at the top of the @interface line in MainView.h and also add, under the - (IBAction)buttonclick; line:

    +(void) initialize;
    

    Then remove int r; from MainView.m. Then in MainView.m add:

    +(void) initialize {
       count = 0;
    }