Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

initialize instance variables on Objective-C

I'm developing an iPhone 3.1.3 application and I have the following header file:

#import <UIKit/UIKit.h>

@interface VoiceTest01ViewController : UIViewController {
    IBOutlet UITextView *volumeTextView;
    BOOL isListening;
    NSTimer *soundTimer;
}

@property (nonatomic, retain) IBOutlet UITextView *volumeTextView;
@property (nonatomic, retain) NSTimer *soundTimer;

- (IBAction)btnStartClicked:(id)sender;

@end

And .m file is:

#import "VoiceTest01ViewController.h"

@implementation VoiceTest01ViewController

@synthesize volumeTextView;
@synthesize soundTimer;

...

How can I set isListening up to false at start?

like image 565
VansFannel Avatar asked Mar 14 '26 14:03

VansFannel


2 Answers

All instance variables are set to 0/NULL/nil by default, which in the case of a BOOL means NO. So it already is NO (or false) by default.

If you need any other value then you need to override the designated initializer(s), most of the time init, and set the default value there.

like image 117
DarkDust Avatar answered Mar 16 '26 06:03

DarkDust


Set the boolean value in your viewDidLoad

- (void)viewDidLoad {
  isListening = NO;
  //Something
}
like image 40
ipraba Avatar answered Mar 16 '26 05:03

ipraba