ios - elements of array are null -
i'm working along bignerdranch ios app book. in first chapter makes little quiz app questions hardcoded code file. if application runs successfully, it's supposed displaying question: "what blah blah?"
in console, when run app says
displaying question: (null)
in other words, (null) appearing instead of question array.
no errors showing when compile. wonder if has fact xcode using main.storyboard
file rather xib , nib files, combined fact view controller uses method seems expect nib file, namely
- (id)initwithnibname:(nsstring *)nibnameornil bundle:
any appreciated. code.
iosquizviewcontroller.h
#import <uikit/uikit.h> @interface iosquizviewcontroller : uiviewcontroller
iosquizviewcontroller.h
{ int currentquestionindex; nsmutablearray *questions; nsmutablearray *answers; iboutlet uilabel *questionfield; iboutlet uilabel *answerfield; } - (ibaction)showanswer:(id)sender; - (ibaction)showquestion:(id)sender; @end
iosquizviewcontroller.m
#import "iosquizviewcontroller.h" @interface iosquizviewcontroller () @end @implementation iosquizviewcontroller - (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil { self = [super initwithnibname:nibnameornil bundle:nibbundleornil]; if (self) { questions = [[nsmutablearray alloc] init]; answers = [[nsmutablearray alloc] init]; [ questions addobject:@"what 7 +7"]; [ answers addobject:@"14"]; [questions addobject:@"what capital of vermont?"]; [answers addobject:@"montpelier"]; [questions addobject:@"from cognac made?"]; [answers addobject:@"grapes"]; } return self; } - (ibaction)showquestion:(id)sender { currentquestionindex++; if (currentquestionindex == [ questions count] ){ currentquestionindex = 0; } nsstring *question = [ questions objectatindex:currentquestionindex]; nslog(@"displaying question: %@", question); [questionfield settext: question]; [answerfield settext:@"???"]; } - (ibaction)showanswer:(id)sender { nsstring *answer = [ answers objectatindex:currentquestionindex]; [answerfield settext:answer]; } @end
i deleted initwithnibname:bundle method , put code inside viewdidload
- (void)viewdidload { if (self) { // create 2 arrays , make pointers point them questions = [[nsmutablearray alloc] init]; answers = [[nsmutablearray alloc] init]; // add questions , answers array [questions addobject:@"what 7 + 7?"]; [answers addobject:@"14"]; [questions addobject:@"what capital of vermont?"]; [answers addobject:@"montpelier"]; [questions addobject:@"what cognac made from?"]; [answers addobject:@"grapes"]; } }
Comments
Post a Comment