2014-03-31 81 views
0

我有兩個數組中的字符串列表真相&敢於從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 [email protected]"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 [email protected]"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 [email protected]"This could be interesting!"; 


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

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

      self.plistArray = [plistArray4 mutableCopy]; 


      NSLog(@"%@", plistArray); 


     } 
     else { 
      self.text.text [email protected]"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

回答

0

這裏會有加了很多更多的數據這是一個有點很難理解代碼,所以我會用一般的術語來說明這一點。如果你有兩個字符串數組(也許你的真值數組和敢於數組),並且你有一個用戶選擇的字符串(這是你的代碼中不清楚的部分 - 它是self.text.text?)讓我們只是打電話那麼你可以測試:

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 
}