Navigate to storyboard from a viewController programmatically - objective-c

in my iOS application, I would like to navigate programmatically to a whole storyboard (tabBarController) from a viewcontroller.
I found this solution:
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Home" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"SMShopViewController"];
[self.navigationController pushViewController:controller animated:YES];
but it is not what I need, I want to show directly my tabBarController
thank you for your reply

As i understand, You have UITabBarController on storyboard like this and you want show it from code?
So, Firstly set storyboard identifier for UITabBarController
Then in code
UITabBarController *tabBar = [self.storyboard instantiateViewControllerWithIdentifier:#"KITTabBarController"];
[self.navigationController pushViewController:tabBar animated:NO];
If you have parent navigation controller better way to do it is:
[self.navigationController setViewControllers:#[tabBar] animated:YES];

Related

ObjC - presentViewController breaks

I am trying to programmatically show another View Controller. I wrote some code that I learned from examples, it gives no errors. But it Breaks and shows this in green:
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); thread 1 : signal SIGABRT
I used this code:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"secondViewController"];
[self presentViewController:vc animated:NO completion:nil];
Is there something wrong with my code, or is it something else?
Thanks in advance.
This happens probably because you don't have a UIViewController with Storyboard ID secondViewController in your storyboard.
You can drag a UIViewcontroller to your Storyboard and in Identity inspector set Storyboard ID to secondViewController.
See the image below
I hope this helps :)

Switch Active View Controller Programmatically

I have 2 views as part of my Navigation Controller.
I created a class for my Navigation Controller with a public method that certain actions on the first view will trigger to load the Detail View Controller. I am importing the Detail View Controller, but nothing in my code is working. I know the method is being called correctly because I'm logging it.
What am I doing wrong here?
#import <UIKit/UIKit.h>
#interface NearbyController : UINavigationController
- (void) nextpage;
#end
#import "NearbyController.h"
#import "DetailView.h"
#implementation NearbyController
-(void) nextpage {
NSLog(#"working");
DetailView *nextView = [[DetailView alloc] init];
[self pushViewController:nextView animated:YES];
}
#end
If you initialize a view controller programmatically you should use this init.
If you have set a storyboard ID you can do this
DetailView *nextView = (DetailView *)[self.storyboard instantiateViewControllerWithIdentifier:#"DetailViewControllerID"];
[self pushViewController:nextView animated:YES];
If you want to load from a XIB you can call
DetailView *nextView = [[DetailView alloc] initWithNibName:#"MyDetailViewControllerID" bundle:nil];
[self pushViewController:nextView animated:YES];
Hope it helps!
If you're using storyboards what you wanna do is setup the storyboard id in the identity inspector on your DetailViewViewController
Then you would wanna instantiate your view controller like so:
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
DetailView *nextView =[sb instantiateViewControllerWithIdentifier:#"DetailViewViewController"];
/* your identifier is your storyboard id*/
sb = nextView.storyboard;
Then you should be able to push your view controller.
Im still new to this but i hope this helps!

Make UITabBarController the root view controller

I made an empty application, then later I and added a UITabBarController in the Storyboard. Then I checked it as the initial viewcontroller and connected two NavigationControllers to represent the tabs.
But if I run the app now it says that "Application windows are expected to have a root viewcontroller at the end of application launch." and shows a white screen.
My AppDelegate looks like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
I have to add the TabBarController as the main window, but how to do this?
If TabBarController was made programmatically I would have figured it but how to when it's created in storyboard? Can I make a property for it in AppDelegate? How to hook it in storyboard in that case?
You can hook storyboard inside your target settings Then make sure that your UITabBarController inside of your storyboard is initial UIViewController . You can also use this code:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *initialViewController = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:#"IdentifierOfYourViewController"];
self.window.rootViewController = initialViewController;

Cocoa - Adding a Navigation Controller to a subview

I'm currently working on a view based app for the iPad that has 3 seperate views on the main page. A custom menu up the top, a status list on the side, and a main view. The issue I am having with the main view is trying to add a navigation controller.
In AppPadViewController.h
#interface AppPadViewController.h : UIViewController {
MainViewController *MainView;
}
#property (nonatomic,retain) IBOutlet MainViewController *MainView;
And in AppPadViewController.m
#synthesize MainView;
- (void) viewDidLoad {
[super viewDidLoad];
MainView.navigationItem.title = #"Home";
UINavigationController *mainNavController = [[UINavigationController alloc] initWithNibName:#"MainView" bundle:[NSBundle mainBundle]];
self.MainView = [MainViewController alloc] initWithRootViewController:mainNavController];
}
And in the nib I have added the view where I would like it, and tied it in to the MainView, and then added the MainViewController and tied it to the File Owner and view.
When I run this, I get an 'Unrecognized Selector" error thrown on the initWithRootViewController line.
Can anyone see any problem with the code, or suggest a better way to add a navigation controller to a sub view?
You have your two view controllers reversed. Try something like this:
self.MainView = [[MainViewController alloc] initWithNibName:#"MainView" bundle:[NSBundle mainBundle]];
UINavigationController *mainNavController = [[UINavigationController alloc] initWithRootViewController:MainView];

Push NavigationController from UIScrollView

how can I push a new view onto the stack of a NavigationController from a UIScrollView?
I tried
[self.navigationController pushViewController:myNewViewController animated:YES];
but get "navigationController not in structure or union".
regards
You don't have a navigation controller in your app. You need to create one. Something like:
In your appDelegate create a UINavigationController instance variable and then use your existing viewController as the rootViewController of the navigation controller.
e.g. in pure code using a UITableViewController (you can use xibs as well which your template app probably does).
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Create root view and navigation controller
UITableViewController *rootViewController = [[[UITableViewController alloc] initWithStyle:UITableViewStyleGrouped] autorelease];
self.navigationController = [[[UINavigationController alloc] initWithRootViewController:rootViewController] autorelease];
// Not necessary if you're using xibs
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Add the nav controller's root view to the window
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
return YES;
}
Then you can push/pop new views in the way you're attempting.