Question

I want to create a UIViewController from NSString Name (buttonSelected.viewControllerID), considering class name is the same than storyboardIdentifier how to do that???, currently I have following code for two viewControllers, but I only want to have it in one shot

    if ([buttonSelected.typeOfContent isEqualToString:@"AtencionClienteDetalleVC"]) {
        if ([[functions removeBlanksFromString:buttonSelected.viewControllerID] isEqualToString:@""]) {
            [[[UIAlertView alloc] initWithTitle:@"Notification"
                                        message:[NSString stringWithFormat:@"screen destination must not be empty"]
                                       delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil] show];
        }
        else{
            AtencionClienteDetalleVC *viewController = [self.storyboard instantiateViewControllerWithIdentifier:buttonSelected.viewControllerID];

            viewController.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:LOGO_COMPANY]];
            viewController.itemsCatalog = buttonSelected.viewControllerParameters;


            [self.navigationController pushViewController:viewController animated:YES];

        }
        return;
    }


if ([buttonSelected.typeOfContent isEqualToString:@"ListadoVideos"]) {
        if ([[functions removeBlanksFromString:buttonSelected.viewControllerID] isEqualToString:@""]) {
            [[[UIAlertView alloc] initWithTitle:@"Notification"
                                        message:[NSString stringWithFormat:@"screen destination must not be empty"]
                                       delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil] show];
        }
        else{
            ListadoVideos *viewController = [self.storyboard instantiateViewControllerWithIdentifier:buttonSelected.viewControllerID];

            viewController.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:LOGO_COMPANY]];
            viewController.itemsCatalog = buttonSelected.viewControllerParameters;


            [self.navigationController pushViewController:viewController animated:YES];

        }
        return;
    }
Was it helpful?

Solution

just need to set all your buttonSelected.typeOfContent to "ViewController" and then replace your current code for this:

if ([buttonSelected.typeOfContent isEqualToString:@"ViewController"]) {
        if ([[functions removeBlanksFromString:buttonSelected.viewControllerID] isEqualToString:@""]) {
            [[[UIAlertView alloc] initWithTitle:@"Notification"
                                        message:[NSString stringWithFormat:@"screen destination must not be empty"]
                                       delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil] show];
        }
        else{
            NSString *myUIViewControllerClassName   = buttonSelected.viewControllerID;
            Class     myClass                       = NSClassFromString(myUIViewControllerClassName);
            NSObject *myObject                      = [myClass new];
            if( [myObject isKindOfClass:[UIViewController class]] ){
                UIViewController *rightViewController   = (UIViewController *) myObject;
                rightViewController                     = [self.storyboard instantiateViewControllerWithIdentifier:myUIViewControllerClassName];
                rightViewController.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:LOGO_COMPANY]];
                [self.navigationController pushViewController:rightViewController animated:YES];
            }
        }
        return;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top