-2
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    self.title = category.categoryName;

    if (self.title == @"Haugesund Motiver" && !motivesLoaded) {
        MotivesDataModel *motiv = [[MotivesDataModel alloc] init];
        motiv.motiveName = @"ksaldkaldsk";
        [category.motives addObject:motiv];
        motivesLoaded = YES;
    }    
}

Why is this not working?

I have to viewcontroller, one with a TableView each.

The 1.st view controller shows categories object, which contains a name plus another array called motives.

The 2.nd view controller is supposed to show the categories.motives array.

category object is passed along with the segue, and works fine.

Here i am trying to populate the nsmutablearray if it hasent been populated before.

But the if statement doesnt work. Everytime the view loads it add the object to the motives array, even if the motivesLoaded bool is changed to YES

1

2 Answers 2

1

you shouldnt check equality of strings with ==, this just checks that they are the same object

you should use

[self.title isEqualToString:@"Haugesund Motiver"]
9
  • Thanks. But the main problem still is there. it creates e new object and adds it to the motives array everytime, even if the If statement only should "work" if motivesLoaded is false.
    – Espen Birk
    Sep 20, 2012 at 14:58
  • 1
    then the if statement is ok, try logging motivesLoaded just before the if statement
    – wattson12
    Sep 20, 2012 at 15:03
  • @EspenBirk Keep in mind !BOOL means the BOOL is NO, or nil. Sep 20, 2012 at 15:13
  • Yes, i know. And if i remember right BOOL is no as default. So if motiveLoaded = NO then it should make an object and put it in the array. After it has created the object it should set motiveLoaded = YES. Stupid question maybe, but how do i NSLog a boolean?
    – Espen Birk
    Sep 20, 2012 at 15:18
  • Correct, but just putting this out there, I'm partial to :NSLog(myBOOL ? @"Yes" : @"No"); Sep 20, 2012 at 15:21
0
self.title == @"Haugesund Motiver"

checks for equality of the Pointers, so if self.title has the same name, but another instance of the String @"Haugesund Motiver", the check will fail, so you should use the [NSString isEqualToString:] method

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.