2

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?

0

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

Comments

3

Set the boolean value in your viewDidLoad

- (void)viewDidLoad {
  isListening = NO;
  //Something
}

2 Comments

Yea - @VansFannel, be sure to check out the UIViewController documentation for this as to why this is the case for this type of class. :)
This is so lame -- not your answer, @iPrabu, which is correct, but the fact that the objective-c language doesn't let you specify initializers for instance variables. The fact that you have to do it in some OS- and app-specific way is just sad (or override the designated initializer as @DarkDust says, but that's even more of a pain just to add what should be a simple = 1).
2

The default value for a BOOL field is False, but it's a good place set it in the "viewDidLoad" just as @BuildSucceeded sugest

Greetings

Comments

0

1) init is a good place, like below, but if you are using story board this init method won't be called.

- (id) init {
    self = [super init];
    if (self) {
        isListening = NO;
    }
    return self;
}

2) initWithCoder is a good place for your code if you are using storyboard of course your sdk is 3.0, i believe it has not storyboard at that time, but just in case anyone need it:

- (id) initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        isListening = NO;
    }
    return self;
}

3) If your viewcontroller will be init from nib file:

- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
         isListening = NO;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.