2

我已经使用情节提要创建了一些按钮和标签,XCode 4.6.3 我想在单击放置在 FirstViewController 中的按钮时触发一些方法,并且为了使这些方法起作用,我已经定义了一些变量/ NSMutableArrays(currentQuestionIndex、questions 等)。我想使用自定义初始化方法来初始化这些。当我离开initWithNibName并按initWithCoder原样编写一个新的 init 方法并在其中编写我的实现时,它不会被调用。

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

我想知道在创建对象时如何使用自定义初始化方法,Storyboard因为我在这里所做的可能不是一个好习惯。当我尝试使用初始化时initWithCoder,它不起作用。但据我所知,我们使用initWithCoder从 初始化Storyboard,对吗?中的按钮/标签位于storyboardFirstViewController 中。

这是我的 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
4

2 回答 2

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;
}
于 2013-12-14T15:29:00.190 回答
0

你可以- (void)awakeFromNib在你的方法中使用UIViewController

于 2013-12-14T15:35:16.347 回答