基于数组中的随机字符串显示图像

时间:2014-03-31 22:43:01

标签: ios objective-c xcode

我有两个数组中的字符串列表。敢于从plist加载然后随机化并在标签中向用户显示这些可能是真实的或敢于。我希望显示一个真实和大胆的图像,当显示每个图像时,它取决于从哪个阵列加载。

当我在加载时将plist文件加载到数组中时我不确定如何执行此操作?我是xcode的新手,但我对目前为止的进展感到很兴奋。

这是我的.m文件的完整代码

   @interface ViewController ()


@end

@implementation ViewController




- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@synthesize plistArray;


- (void)viewDidAppear:(BOOL)animated {

    [self becomeFirstResponder];

    NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];






    NSString *path = [[NSBundle mainBundle] pathForResource:
                      @"data" ofType:@"plist"];







    if ([[defaults objectForKey:@"truthonoff"] isEqualToString:@"YES"] && [[defaults objectForKey:@"dareonoff"] isEqualToString:@"YES"]  ) {

            self.text.text =@"Are you ready for this?";



            NSDictionary *plistDict1 = [[NSDictionary alloc] initWithContentsOfFile:path];
            NSArray * plistArray1 = plistDict1[@"truth"];



            NSDictionary *plistDict2 = [[NSDictionary alloc] initWithContentsOfFile:path];
            NSArray *plistArray2 = plistDict2[@"dare"];

            self.plistArray = [[plistArray1 arrayByAddingObjectsFromArray:plistArray2] mutableCopy];

        }

        else if ([[defaults objectForKey:@"truthonoff"] isEqualToString:@"YES"] ) {

            self.text.text =@"I hope you are feeling brave!";



            NSDictionary *plistDict3 = [[NSDictionary alloc] initWithContentsOfFile:path];

            NSArray *plistArray3 = plistDict3[@"truth"] ;

            self.plistArray = [plistArray3 mutableCopy];


            NSLog(@"%@", plistArray);


        }

        else if ([[defaults objectForKey:@"dareonoff"] isEqualToString:@"YES"] ) {

            self.text.text =@"This could be interesting!";


            NSDictionary *plistDict4 = [[NSDictionary alloc] initWithContentsOfFile:path];

            NSMutableArray *plistArray4 = plistDict4[@"dare"];

            self.plistArray = [plistArray4 mutableCopy];


            NSLog(@"%@", plistArray);


        }
        else {
            self.text.text =@"Please turn on Truth or Dare";


        }



}

- (void)viewDidDisappear:(BOOL)animated {

    [self becomeFirstResponder];
}

- (BOOL)canBecomeFirstResponder {
    return YES;
}


- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    // Do your thing after shaking device

    if ([plistArray count] == 0) {
        self.text.text = @"Please Upgrade for more";
    }
    else    {
        ////display random quote from array
        int randV = arc4random() % self.plistArray.count;


        self.text.text = self.plistArray[randV];
        [self.plistArray removeObjectAtIndex:randV];

    }

   }



-(IBAction)modal:(id)sender{

}

- (IBAction)container:(id)sender {
}











- (IBAction)shownext:(id)sender {






    if ([plistArray count] == 0) {
               self.text.text = @"Please Upgrade for more";
            }
        else    {
        ////display random quote from array
        int randV = arc4random() % self.plistArray.count;


            self.text.text = self.plistArray[randV];
            [self.plistArray removeObjectAtIndex:randV];

            }

}




@end

这是我的plist结构,当完成时会有更多的数据添加到这里 enter image description here

1 个答案:

答案 0 :(得分:0)

理解代码有点难,所以我将概括地说明这一点。如果你有两个字符串数组(可能是你的真实数组和dare数组),并且你有一个用户选择的字符串(这是你的代码中不清楚的部分 - 是self.text.text吗? )让我们称之为selectedString,然后你可以测试:

if ([self.truthArray containsObject:selectedString]) {
    // selectedString is from the truth array
} else if ([self.dareArray containsObject:selectedString]) {
    // selectedString is from the dare array
} else {
    // it's in neither array.  maybe this is an error
}