我使用情节XCode 4.6.3
提要创建了一些按钮和标签,我想单击FirstViewController中的按钮时触发一些方法,为了使这些方法起作用,我定义了一些变量/ NSMutableArrays
(currentQuestionIndex,questions等)。我想使用自定义的初始化方法来初始化这些。当我离开initWithNibName
并按initWithCoder
原样编写新的init方法并在其中编写实现时,它不会被调用。
但是,当我按下面的代码片段所示操作代码时,它起作用了。
我想知道在使用创建对象时如何使用自定义init方法,Storyboard
因为我在这里所做的可能不是一个好习惯。当我尝试使用初始化时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;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句