2

I have created a few buttons and labels using storyboard in XCode 4.6.3 I want to fire some methods on the click of the buttons placed in the FirstViewController and for the methods to work I have defined some variables/NSMutableArrays (currentQuestionIndex,questions,etc.). I want to use a custom init method for inititalizing these. When I leave initWithNibName and initWithCoder as it is and write a new init method and write my implementation in it, it doesn't get called.

But when I manipulated the code as I have shown in the snippets below, it worked.

I want to know how to use custom init methods when creating objects using Storyboard because what I did here is probably not a good practice. When I tried initializing using initWithCoder , it did not work. But from what I can recall, we use initWithCoder to initialize from Storyboard, right? The buttons/labels in the storyboard are in FirstViewController.

This is my FirstViewController.h file

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController
{
    int currentQuestionIndex;
    // The model objects
    NSMutableArray *questions;
    NSMutableArray *answers;

    //The view objects
    IBOutlet UILabel *questionField;
    IBOutlet UILabel *answerField;
}

@property (strong, nonatomic) UIWindow *window;

- (IBAction)showQuestion:(id)sender;
- (IBAction)showAnswer:(id)sender;

@end

and

This is my FirstViewController.m file

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

- (id)init {
    // Call the init method implemented by the superclass
    self = [super init];
    if(self) {
        currentQuestionIndex = -1;
        // Create two arrays and make the pointers point to them
        questions = [[NSMutableArray alloc] init];
        answers = [[NSMutableArray alloc] init];
        // Add questions and answers to the arrays
        [questions addObject:@"What is 7 + 7?"];
        [answers addObject:@"14"];
        [questions addObject:@"What is the capital of Vermont?"];
        [answers addObject:@"Montpelier"];
        [questions addObject:@"From what is cognac made?"];
        [answers addObject:@"Grapes"];
    }
    // Return the address of the new object
    return self;
}

-(IBAction)showQuestion:(id)sender
{
    currentQuestionIndex++;
    if(currentQuestionIndex == [questions count])
        currentQuestionIndex = 0;
    NSLog(@"%d",[questions count]);
    NSString *question = [questions objectAtIndex:currentQuestionIndex];
    NSLog(@"dislaying question at index %d : %@" ,currentQuestionIndex,question);

    [questionField setText:question];

    [answerField setText:@"???"];
}

-(IBAction)showAnswer:(id)sender
{
    NSString *answer = [answers objectAtIndex:currentQuestionIndex];
    [answerField setText:answer];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
Sport
  • 8,570
  • 6
  • 46
  • 65
ronilp
  • 455
  • 2
  • 9
  • 25
  • 2
    Yes, overriding initWithCoder (and not init) should work. What do you mean by "it did not work"? Is it not called? - You can also override awakeFromNib, which is called when all object are loaded from the storyboard and the outlets are connected. – Martin R Dec 14 '13 at 14:55
  • when i tried to override initWithCoder, the initialization specified in it did not work for me. And what exactly do we do with awakeFromNib? – ronilp Dec 14 '13 at 15:15
  • What do you mean by "it did not work"? – Martin R Dec 14 '13 at 15:16
  • i mean that that the initialization did not take place. For example currentQuestionIndex = -1; this statement never got executed. – ronilp Dec 14 '13 at 15:18
  • Did you set a breakpoint in initWithCoder to check if it is called at all? – Martin R Dec 14 '13 at 15:19
  • No, i checked that through the application itself as the showQuestion method has this statement NSLog(@"dislaying question at index %d : %@" ,currentQuestionIndex,question); and it will give me the currentQuestionIndex value. When i try to run this, i get dislaying question at index 1 : (null) which means that the initialization did not take place. – ronilp Dec 14 '13 at 15:21
  • and can you please tell me why what i did is a bad practice ... – ronilp Dec 14 '13 at 15:23
  • 1
    initWithCoder is a so-called "designated initializer", compare https://developer.apple.com/library/ios/documentation/general/conceptual/CocoaEncyclopedia/Initialization/Initialization.html#//apple_ref/doc/uid/TP40010810-CH6-SW3. – Martin R Dec 14 '13 at 15:33

2 Answers2

2

i ma getting all the values..when I log...try this

-(id)initWithCoder:(NSCoder *)aDecoder{

    if(self = [super initWithCoder:aDecoder]) {

        currentQuestionIndex = 15;
        NSLog(@"%d", currentQuestionIndex);
        // Create two arrays and make the pointers point to them
        questions = [[NSMutableArray alloc] init];
        answers = [[NSMutableArray alloc] init];
        // Add questions and answers to the arrays
        [questions addObject:@"What is 7 + 7?"];
        [answers addObject:@"14"];
        [questions addObject:@"What is the capital of Vermont?"];
        [answers addObject:@"Montpelier"];
        [questions addObject:@"From what is cognac made?"];
        [answers addObject:@"Grapes"];
        NSLog(@"Questions = %@", questions);
        NSLog(@"Answers = %@", answers);
     }
     return self;
}
Suhit Patil
  • 11,748
  • 3
  • 50
  • 60
  • Alright, I got my problem. What I was doing wrong was that I used self = [super init] instead of self = [super initWithCoder]. That solves it. Thanks – ronilp Dec 14 '13 at 15:35
0

you can use - (void)awakeFromNib method in your UIViewController

iiFreeman
  • 5,165
  • 2
  • 29
  • 42