0

is there anyway to check if a specific instance of a class has already been created. I feel like it is hard to check if the instance already exists when there is a chance you may not have created it yet.

Here is my code:

-(IBAction)done:(id)sender
{ //I want to figure out how to check if 'newWindow' already exists before I create another 'newWindow'

SimpleTableView *newWindow = [self.storyboard instantiateViewControllerWithIdentifier:@"SimpleTableView"];

[self.navigationController pushViewController:newWindow animated:YES];
}

Thanks for all the help guys.

Just_chillin
  • 39
  • 1
  • 8

5 Answers5

3

Yes, there is a simple way to do it.

You just need to have some reference to it (for example create a property) and check whether it is nil (not initialized) or not. You can do it like this:

if(!myReference){
    //if you get here it means that it hasn't been initialized yet so you have to do it 
}
hichris123
  • 10,145
  • 15
  • 56
  • 70
Julian
  • 9,299
  • 5
  • 48
  • 65
1

First make newWindow an ivar or a property.

Then:

if (!newWindow){    
    newWindow = [self.storyboard instantiateViewControllerWithIdentifier:@"SimpleTableView"];
}
jscs
  • 63,694
  • 13
  • 151
  • 195
Guilherme Torres Castro
  • 15,135
  • 7
  • 59
  • 96
1

I wrote you a method that checks all viewControllers in UINavigationController:

- (BOOL)classExistsInNavigationController:(Class)class
{
    for (UIViewController *controller in self.navigationController.viewControllers)
    {
        if ([controller isKindOfClass:class])
        {
            return YES;
        }
    }
    return NO;
}

Use it like this:

- (IBAction)done:(id)sender
{ 
    //I want to figure out how to check if 'newWindow' already exists before I create another newWindow
    if (![self classExistsInNavigationController:[SimpleTableView class]])
    {
        SimpleTableView *newWindow = [self.storyboard   instantiateViewControllerWithIdentifier:@"SimpleTableView"];

        [self.navigationController pushViewController:newWindow animated:YES];
    }
}

You can also do something like this:

- (UIViewController *)classExistsInNavigationController:(Class)class
    {
        for (UIViewController *controller in self.navigationController.viewControllers)
        {
            if ([controller isKindOfClass:class])
            {
                return controller;
            }
        }
        return nil;
    }

And use it like this if you want to pop to the viewController that exists already:

- (IBAction)done:(id)sender
    { 
        //I want to figure out how to check if 'newWindow' already exists before I create another newWindow
        UIViewController *controller = [self classExistsInNavigationController:[SimpleTableView class]];
        if (!controller)
        {
            SimpleTableView *newWindow = [self.storyboard   instantiateViewControllerWithIdentifier:@"SimpleTableView"];

            [self.navigationController pushViewController:newWindow animated:YES];
        }
        else
        {
            [self.navigationController popToViewController:controller animated:YES];
        }
    }
Refael.S
  • 1,634
  • 1
  • 12
  • 22
  • I run this code and use NSlog inside of it to check if neWindow exists. For some reason it will say that newWindow doesn't exist, then after the code that it does exist, but it says that no matter how many times in a row I run this code. – Just_chillin Jan 08 '14 at 20:45
  • I didn't understand your comment, can you explain please better? – Refael.S Jan 08 '14 at 20:52
  • Yeah, sorry about that. so basically when I run this newWindow starts off as null, then this method sets the value so newWindow isn't null anymore. But when I run the method again, instead if newWindow having the value it was just assigned, it says that newWindow is null. – Just_chillin Jan 08 '14 at 22:47
  • Would it be more helpful if I rephrase the question, I may have not explained it properly. – Just_chillin Jan 08 '14 at 22:48
  • Look at my profile and you can send me by mail your xcode project, I will fix it and send to you back. – Refael.S Jan 08 '14 at 23:02
0

You can use if/else to check newWindow exists or not.

if (newWindow) {    // newWindow is exist to do something
    // Do something.
} else {            // newWindow is not exist to do something
    // Do something.
}
Sam
  • 1
  • 2
0

You can implement an instance counter (https://stackoverflow.com/a/30509753/4647396) in the class you want to track. Then just check if the counter is greater than 0. If I interpret your question correctly you just want to know wether an instance exists and dont need a reference to it.

Community
  • 1
  • 1
Andy
  • 866
  • 9
  • 14