为什么在一个方法中其值已更改的此属性在另一个方法中重置?

时间:2012-08-10 22:18:10

标签: objective-c xcode

我在头文件中声明了属性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被调用的时间是什么。

我错过了什么?

1 个答案:

答案 0 :(得分:0)

我所缺少的是我是个白痴。我使用的是self.indx -= self.indx而不是self.indx -=1。不要问我为什么。我不能告诉你。