i am using UINavigationController but when i push a view controller just simply by making object of the class the class load but not the components in it.
this is my AppDelegate.m
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ViewController *vc = [sb instantiateInitialViewController];
UINavigationController *nav = [[UINavigationController alloc] init];
self.window.rootViewController = nav;
nav.viewControllers = #[vc];
and my first UIViewController if action button code is
NextView *next = [[NextView alloc] init];
[self.navigationController pushViewController:next animated:YES ];
it gets to next UIViewController but no internal components are loaded such as - button,textfield,etc.
and if i try this code it give me runtime error
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
NextView *next = [sb instantiateViewControllerWithIdentifier:#"two"];
[self.navigationController pushViewController:next animated:YES ];
make sure that NextView inherit UIViewController and you have given identifier as two to that view controller and you have set class NextView from identity inspector to that viewcontroller.
Hope this will help :)
You could not get outlets (buttons etc.) to be loaded with alloc init in the subclass of UIViewController. You should init it with the xib file, using -initWithNibName:bundle: method. Or you can instantiate it with storyboard.
// You can get current storyboard from the View Controller
UIStoryboard *sb = vc.storyboard;
NextView *next = [sb instantiateViewControllerWithIdentifier:#"two"];
[self.navigationController pushViewController:next animated:YES];
Related
I created a SingleView application then use "Embed In Navigation Controller" to get a navigation control.
when push a controller, the viewcontroller's backgroundColor is black.
i know use this code :
UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];//UIStoryboard(name: "Main", bundle:nil)
UIViewController *roomController = [storyBoard instantiateViewControllerWithIdentifier:#"controlerID"];
but what's the reason ?
and if i don't want to use the storyboard completely ,only use this code
UIViewController *roomController = [[UIViewController alloc]init];
what can i do? is only set the viewcontroller's backgroundColor?
You need to use the instantiateViewControllerWithIdentifier: method of your UIStoryboard instance and then you can push the controller.
UIStoryboard *storyboard =[UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *roomController = [storyboard instantiateViewControllerWithIdentifier:#"controlerID"];
[self.navigationController pushViewController: roomController animated:YES];
// if You make controller with out storyboard Than change You view color
UIViewController *roomController = [[UIViewController alloc]init];
[self.navigationController pushViewController: roomController animated:YES];
- (void)viewDidLoad
{
self.view.backgroundColor =[UIColor whiteColor];
}
I need to present a view controller from app delegate.
When a phone notification comes in, I am able to decide which one of 3 view controllers (named ForumViewController, BlogViewController & NewsViewController) should be presented by analyzing the 'userInfo' in the method 'didReceiveRemoteNotification'.
But when i try to present the appropriate view controller using storyboards or the code below:
self.viewController = [[MembersViewController alloc] initWithNibName:#"MembersViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
Then, the app gives the error 'Warning: Attempt to present whose view is not in the window hierarchy!'. Also it gets stuck on a particular view controller.
Please keep in mind that the view controllers that I am trying to present are not part of the flow when the app starts (the flow is LogoViewController -> SplashViewController -> HomeViewController).
The HomeViewController & MembersViewController are essentially the main menu pages for public & private viewing. Here I have to display something to the viewer.
choice-1
using push
UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
MembersViewController *vc = [navController.storyboard instantiateViewControllerWithIdentifier:#"MembersViewController"];
[navController pushViewController:vc animated:YES];
using present
MembersViewController *root = (MembersViewController *)self.window.rootViewController;
UIViewController *vc = [root.storyboard instantiateViewControllerWithIdentifier:#"MembersViewController"];
[root presentViewController:vc animated:YES completion:NULL];
upadted
UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
MembersViewController* pvc = [mainstoryboard instantiateViewControllerWithIdentifier:#"MembersViewController"];
[self.window.rootViewController presentViewController:pvc animated:YES completion:NULL];
Loading a view controller from the storyboard:
[self performSelector: #selector(ShowModalViewController) withObject: nil afterDelay: 0];
-(void)ShowModalViewController{
NSString * storyboardName = #"MainStoryboard";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"IDENTIFIER_OF_YOUR_VIEWCONTROLLER"];
[self.window.rootViewController presentViewController:vc animated:YES completion:nil];
}
Identifier of your view controller is either equal to the class name of your view controller, or a Storyboard ID that you can assign in the identity inspector of your storyboard.
In my AppDelegates 'didFinishLaunchingWithOptions' function, I have this code in there:
if(loggedIn != nil)
{
MainViewController *mvc = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:#"MainView"];
[self.window setRootViewController:mvc];
}
Second Attempt which didn't work:
if(loggedIn != nil)
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
MainViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"MainView"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
[self.window setRootViewController:nav];
}
The problem is that when the MainViewController loads, the NavigationHeader is missing. I've tried various methods online and instantiations that basically do the same thing to no avail. I have also tried created a whole new navigationController and adding my view to it, however, that fails as well.
Your setting MainViewController as your root, if this is not a navigation controller, there will be no header when it opens.
Instead create a UINavigationController, set MainViewController as its root and then set the navigation controller as the window root.
e.g.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"storyboardName" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"home"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
[self.window setRootViewController:nav];
or if you have the navigation controller inside the storyboard then instantiate that. Most likely the initial view controller.
e.g.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"storyboardName" bundle:nil];
[self.window setRootViewController::[storyboard instantiateInitialViewController]];
not sure if it will be the initial viewController or not, that requires more info of your setup to know.
I have a form design by xib file. In form have a button "Done". I want when click done button, it will forward to my view controller.
I tried code like this:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main.storyboard" bundle:nil];
CommentViewController *myVC = (CommentViewController *)[storyboard instantiateViewControllerWithIdentifier:#"Comment"];
[self presentModalViewController:myVC animated:YES];
But error at "presentModelViewController".
" No visible #interface for 'my form' declares the selector 'presentModalViewController'...
If self is UINavigationController, use this:
[self.navigationController pushViewController:yourViewController animated:YES];
If self is UIViewController, use this:
[self presentModalViewController:yourViewController animated:YES];
Note that: presentModalViewController: animated: is deprecated now.
The problem is that self is not a UIViewController. So you can't send presentModalViewController to self. Send it to a view controller!
UIStoryboard *loStoryboard ;
loStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil]; // Here only "Main" not "Main.storyboard".
// Instantiate the initial view controller object from the storyboard
UIViewController *initialViewController = [loStoryboard instantiateInitialViewController];
// Instantiate a UIWindow object and initialize it with the screen size of the iOS device
self.view = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Set the initial view controller to be the root view controller of the window object
[self.navigationController pushViewController:initialViewController animated:YES];
Below is an example of using initWithNibName with separate xib views:
TerminalViewController *ctrl = [[TerminalViewController alloc]
initWithNibName:#"ControllerView" bundle:[NSBundle mainBundle]];
ctrl.appDelegate = self;
viewCtrl = ctrl;
However i need to implement it with a storyboard UI layout. For 'initWithNibName' how can i point to a View in my storyboard:
i.e. :
TerminalViewController *ctrl = [[TerminalViewController alloc]
initWithNibName:#"STORYBOARD.ControllerView" bundle:[NSBundle mainBundle]];
ctrl.appDelegate = self;
Any help is much appreciated.
Thanks,
Dave
You can give the controller an Identifier in the storyboard and then use this...
[self.storyBoard instantiateViewControllerWithIdentifier:#"TheIdentifier"];
Just to add to this...
When creating a UIViewController subclass from a xib file. If you have a class called MyViewController and a xib called MyViewController.xib then all you need to do is...
MyViewController *controller = [[MyViewController alloc] init];
The system will then look for a xib file called MyViewController.xib and use that to init the object and only fallback to doing it in code if the xib file doesn't exist.
get storyboard instance with name of storyboard and set identifier to all scene and get the instance
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
MyNewViewController *myVC = (MyNewViewController *)[storyboard instantiateViewControllerWithIdentifier:#"myViewCont"];
UIStoryboard *stryBoard=[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
TerminalViewController *ctrl = [stryBoard instantiateViewControllerWithIdentifier:#"ControllerView"];