【发布时间】:2014-03-23 20:24:28
【问题描述】:
我正在尝试开发一个显示随机真理或敢于类型问题的应用程序,但用户可以选择关闭真理或敢于选项。我已经成功地让应用程序从真值或敢于数组的 plist 文件中显示随机引用,我还设法在用户选项视图控制器中编程了两个开关按钮。
我的问题是,如果用户打开了 uiswitchs off,我将如何只显示真话或敢于或两者兼而有之?
- (IBAction)button:(id)sender
{
if (!self.plistArray)
{
NSString *path = [[NSBundle mainBundle] pathForResource:
@"data" ofType:@"plist"];
NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
if ([[defaults objectForKey:@"truthonoff"] isEqualToString:@"YES"])
{
NSDictionary *plistDict = [[NSDictionary alloc] initWithContentsOfFile:path];
NSArray *plistArray1 = plistDict[@"truth"];
}
if ([[defaults objectForKey:@"dareonoff"] isEqualToString:@"YES"])
{
NSDictionary *plistDict2 = [[NSDictionary alloc] initWithContentsOfFile:path];
NSArray *plistArray2 = plistDict2[@"dare"];
}
self.plistArray = [[plistArray1 arrayByAddingObjectsFromArray:plistArray2] mutableCopy];
}
NSLog(@"%@", plistArray);
//check to see if array is empty and display message
if ([plistArray count] == 0)
{
self.text.text = @"array empty";
}
else
{
//display random quote from array
int randV = arc4random() % self.plistArray.count;
self.text.text = self.plistArray[randV];
[self.plistArray removeObjectAtIndex:randV];
}
}
这是我的尝试,但它不会运行,我感觉它不会完成我需要的工作。
基本上,如果用户选择它为真,我需要它只显示真值,或者仅在选择它时才敢显示,或者如果两者都设置为真,则两者都显示。
编辑 抱歉,上面代码的问题是 plist 没有被加载,它直接进入 if array ==0 {
如何确保它加载数组,然后检查从 plist 文件中加载哪些数组?
非常感谢任何帮助
这是我尝试添加 if 语句之前的代码。我很困惑如何最好地做到这一点
- (IBAction)shownext:(id)sender {
//load array and check then cycle through this untill array is empty. Array will add two arrays from plist file.
if (!self.plistArray) {
NSString *path = [[NSBundle mainBundle] pathForResource:
@"data" ofType:@"plist"];
NSDictionary *plistDict = [[NSDictionary alloc] initWithContentsOfFile:path];
NSArray * plistArray1 = plistDict[@"truth"];
NSDictionary *plistDict2 = [[NSDictionary alloc] initWithContentsOfFile:path];
NSArray *plistArray2 = plistDict2[@"dare"];
self.plistArray = [[plistArray1 arrayByAddingObjectsFromArray:plistArray2] mutableCopy];
}
NSLog(@"%@", plistArray);
//check to see if array is empty and display message
if ([plistArray count] == 0) {
self.text.text = @"array empty";
}
else {
//display random quote from array
int randV = arc4random() % self.plistArray.count;
self.text.text = self.plistArray[randV];
[self.plistArray removeObjectAtIndex:randV];
}
}
【问题讨论】:
-
您在问题中谈到了 UISwitch,但我没有看到任何 UISwitch 代码,只有 UIButton。
-
哪个部分不工作?
-
我似乎无法让所有代码相互交谈。我想不出关于如何确定以哪种方式检查状态然后显示结果的逻辑
-
我让 uiswitch 与第二个视图控制器对话,所以没有看到需要发布此代码。我想做的只是根据用户提供的输入仅显示真相或敢于或两者兼而有之。上面是检查ui开关状态的代码。
标签: ios objective-c uiswitch