ShurupuS
ShurupuS

Reputation: 2923

How not to load the view in some cases?

I have a methods

- (void)viewDidAppear:(BOOL)animated
{
    [self updateViews];
}

- (void) updateViews
{
    NSInteger itemIndex = [[DataController sharedInstance] indexFromObjectProperty:itemUUID];
    if (itemIndex == NSNotFound) {
    [self.navigationController popViewControllerAnimated:YES];
    }
    NSDictionary *item = [[[DataController sharedInstance] getItems] objectAtIndex:itemIndex];
}

I need not to load the view in case itemIndex == NSNotFound but in debug mode this string is called and then the next string is accessing and it causes an exception. How to stop updating view and show root view controller?

Upvotes: 0

Views: 37

Answers (1)

Firo
Firo

Reputation: 15566

There are two ways to easily do this:

Add a return:

- (void) updateViews
{
    NSInteger itemIndex = [[DataController sharedInstance] indexFromObjectProperty:itemUUID];
    if (itemIndex == NSNotFound) {
        [self.navigationController popViewControllerAnimated:YES];
        return; // exits the method
    }
    NSDictionary *item = [[[DataController sharedInstance] getItems] objectAtIndex:itemIndex];
}

or you have other things that you want to accomplish in this method (mainly if this is not the view being popped):

- (void) updateViews
{
    NSInteger itemIndex = [[DataController sharedInstance] indexFromObjectProperty:itemUUID];
    // nil dictionary
    NSDictionary *item;
    if (itemIndex == NSNotFound) {
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        // setup the dictionary
        item = [[[DataController sharedInstance] getItems] objectAtIndex:itemIndex];
    }
    // continue updating
}

Upvotes: 1

Related Questions