Jump to another view controller dynamically - IOS / Objective C

Jump to another view controller without using segues - IOS / Objective C

As a iOS/iPhone developer you have might need to move between different view controllers time to time. I hope everyone knows that there is an easy way using segues. Segues are generating from UI layout. But you may want to redirect to another IOS View controller dynamically. Therefore I have programmed this event below manner.

NSString *storyboardTypeName = @"Main_iPhone";
            if([[UIDevice currentDevice].model  isEqualToString:@"iPhone"]){
                storyboardTypeName = @"Main_iPhone";
            }else if([[UIDevice currentDevice].model isEqualToString:@"iPad"]){
                storyboardTypeName = @"Main_iPad";
            }
            UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:storyboardTypeName bundle: nil];
            dlCouponListViewController *listViewViewController = [mainStoryboard instantiateViewControllerWithIdentifier:@"secondViewControllerID"];
            [(UINavigationController*)self.window.rootViewController pushViewController:listViewViewController animated:NO];

Now I am gonna explain you what I have done using above code.

------------------
NSString *storyboardTypeName = @"Main_iPhone";
            if([[UIDevice currentDevice].model  isEqualToString:@"iPhone"]){
                storyboardTypeName = @"Main_iPhone";
            }else if([[UIDevice currentDevice].model isEqualToString:@"iPad"]){
                storyboardTypeName = @"Main_iPad";
            }

This code snippets identify the user device either current device is a iPhone or iPad. Because my app was a universal one. But if you are using only iPhone or ipad app then you no need this code.
------------------

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:storyboardTypeName bundle: nil];

This line of code does what is catching the storeboard is.
------------------
dlCouponListViewController *listViewViewController = [mainStoryboard instantiateViewControllerWithIdentifier:@"secondViewControllerID"];

This line catches the next view controller app should be redirected.
secondViewControllerID is the id of next particular view controller.
------------------

 [(UINavigationController*)self.window.rootViewController pushViewController:listViewViewController animated:NO];

Finally navigation controller complete the redirection task.

---------------

Thank you and happy coding.
Thanks.
Dasun

Comments

Popular Posts