我在XCode 4.6.3
中使用情节提要创建了一些按钮和标签我想在单击FirstViewController中的按钮时激发一些方法,为了使这些方法发挥作用,我定义了一些变量/NSMutableArrays
(currentQuestionIndex、questions等)。我想使用自定义的init方法来初始化这些方法。当我保持initWithNibName
和initWithCoder
的原样,编写一个新的init方法并在其中编写实现时,它不会被调用。
但是,当我按照下面的片段所示操作代码时,它起了作用。
我想知道在使用Storyboard
创建对象时如何使用自定义init方法,因为我在这里所做的可能不是一个好的实践。当我尝试使用initWithCoder
进行初始化时,它不起作用。但据我所知,我们使用initWithCoder
从Storyboard
初始化,对吧?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
我正在获取所有值。。当我登录时。。。试试这个
-(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;
}
您可以在UIViewController
中使用- (void)awakeFromNib
方法