ios - Change Button Label -
i new ios app development. working on simple application displays 2 words on screen , user selects 1 , app outputs have selected x.
i have created 2 plists , have loaded them onto array , randomised them in array. on view have 2 buttons want words display on them. however, having 2 problems.. 1 . want label on buttons change when app starts. user doesn't have tap on button or anything. 2. want 1 random word array_1 (loaded plist) appear on random button. either button_1 or button_2 , same list array_2.
here have done far. (with forums (: )
- (ibaction)buttonpressed:(id)sender { // load contents of plist onto array nsstring *path = [[nsbundle mainbundle] pathforresource:@"wordsone" oftype:@"plist"]; nsmutablearray *words = [nsmutablearray arraywithcontentsoffile:path]; //shuffle them using fisher–yates shuffle algorithm (int = words.count-1; i>=0; i--) { int r = arc4random_uniform(words.count); [words exchangeobjectatindex:i withobjectatindex:r]; } // assign word button (int j =0; j<_wordbutton.count; j++) { uibutton *button = [_wordbutton objectatindex:j]; button.titlelabel.text = [words objectatindex:j]; } }
there flaws in above code, example uses 1 plist , doesn't display words on both buttons randomly.
do this:
-(void)viewdidload{ [self setnewtitle]; } -(void)setnewtitle{ nsstring *pathone = [[nsbundle mainbundle] pathforresource:@"wordsone" oftype:@"plist"]; nsmutablearray *wordsone = [nsmutablearray arraywithcontentsoffile:pathone]; nsstring *pathtwo = [[nsbundle mainbundle] pathforresource:@"wordsone" oftype:@"plist"]; nsmutablearray *wordstwo = [nsmutablearray arraywithcontentsoffile:pathtwo]; int words = "the number of objects(words) in plist"; int randombutton = arc4random() % 2; int randomplist = arc4random() % 2; int randomword = arc4random() % words; if (randombutton==0){ if (randomplist==0){ [[_wordbutton objectatindex:randombutton] settitle:[wordsone objectatindex:randomword]]; } else { [[_wordbutton objectatindex:randombutton] settitle:[wordstwo objectatindex:randomword]]; } }else { if (randomplist==0){ [[_wordbutton objectatindex:randombutton] settitle:[wordsone objectatindex:randomword]]; } else { [[_wordbutton objectatindex:randombutton] settitle:[wordstwo objectatindex:randomword]]; } } }
please make sure in array contained buttons initialized , allocated, if have questions please free ask
Comments
Post a Comment