我在头文件中声明了属性int indx;
。然后在实现文件中我有这两个方法:
-(IBAction)nextHaiku
{
{
if (!self.indx)
{
self.indx=0;
}
NSString *cat = self.selectedCategory;
//For now (adjust later so that, according to UISegmentedControl, it will also show only the user's haiku or all haiku):
cat = @"Derfner";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"category == %@", cat];
NSArray *filteredArray = [self.gayHaiku filteredArrayUsingPredicate:predicate];
int array_tot = [filteredArray count];
int sortingHat;
if (array_tot > 0)
if (self.indx == self.theseAreDone.count)
{
while (true)
{
sortingHat = (arc4random() % array_tot);
if (![theseAreDone containsObject:[filteredArray objectAtIndex:sortingHat]]) break;
}
self.haiku_text.text = [[filteredArray objectAtIndex:sortingHat] valueForKey:@"quote"];
if (!self.theseAreDone || self.theseAreDone.count==array_tot)
{
self.theseAreDone = [[NSMutableArray alloc] init];
}
[theseAreDone addObject:[filteredArray objectAtIndex:sortingHat]];
self.indx = self.theseAreDone.count;
}
else
{
self.haiku_text.text = [[self.theseAreDone objectAtIndex:indx] valueForKey:@"quote"];
self.indx += self.indx;
}
}
}
和
-(IBAction)previousHaiku
{
self.haiku_text.text = [[self.theseAreDone objectAtIndex:self.indx-1] valueForKey:@"quote"];
self.indx -= self.indx;
}
NSLog
表示self.indx
每次调用newHaiku
时增量为1。但NSLog
也表明self.indx
在{0}处开始previousHaiku
,无论上次newHaiku
被调用的时间是什么。
我错过了什么?
答案 0 :(得分:0)
我所缺少的是我是个白痴。我使用的是self.indx -= self.indx
而不是self.indx -=1
。不要问我为什么。我不能告诉你。