当我们使用storyboard时,我们如何在objective-c中实现自定义init方法?

时间:2013-12-14 14:45:58

标签: ios objective-c storyboard init initwithcoder

我在XCode 4.6.3中使用故事板创建了几个按钮和标签 我想在点击放置在FirstViewController中的按钮时触发一些方法,并且为了工作方法,我已经定义了一些变量/ NSMutableArrays(currentQuestionIndex,questions等)。我想使用自定义init方法来启动这些。 当我按原样离开initWithNibNameinitWithCoder并编写一个新的init方法并在其中编写我的实现时,它不会被调用。

但是当我按照下面的片段中显示的那样操作代码时,它起作用了。

我想知道在使用Storyboard创建对象时如何使用自定义init方法,因为我在这里做的可能不是一个好习惯。当我尝试使用initWithCoder进行初始化时,它无效。但是从我记忆中,我们使用initWithCoderStoryboard初始化,对吧? storyboard中的按钮/标签位于FirstViewController中。

这是我的FirstViewController.h文件

#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

这是我的FirstViewController.m文件

#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

2 个答案:

答案 0 :(得分:2)

我得到了所有的价值......当我记录下来......试试这个

-(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;
}

答案 1 :(得分:0)

您可以在- (void)awakeFromNib

中使用UIViewController方法