I am working with UITabBarcontroller, where i want to switch between view s with an UISegmentController, but the UITabBarItem should not be hidden when I click on UISegmentController:
if([sender selectedSegmentIndex] == 0)
{
// UIStoryboard *mystoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
// UITabBarController *wc = [mystoryboard instantiateViewControllerWithIdentifier:#""];
// [self.navigationController pushViewController:wc animated:YES];
}
else if([sender selectedSegmentIndex] == 1)
{
UIStoryboard *mystoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UITabBarController *wc = [mystoryboard instantiateViewControllerWithIdentifier:#"ProfileViewController"];
[self.navigationController pushViewController:wc animated:YES];
}
I will assume that what you are trying to do is have a secondary tab bar (the segment control) to switch between views inside a primary tab. In this case, I would suggest to use a view container, and make the switch inside it. Something like this:
EDIT
You can try this.
But once it changes in The TabBarController,your current VC changes and you cannot access the SegmentedControl as you are in a new VC now, thus you have to again navigate to the older VC via the Tabbar. But you can try this.
if(self.segmentedControl.selectedSegmentIndex == 0){
[self.navigationController.tabBarController setSelectedIndex:1];
}
else {
[self.navigationController.tabBarController setSelectedIndex:0];
}
This code would work when your storyboard is like this
Related
I have a button action in which i'm trying to open view controller on condition bases, but the view controller are not opening. I have use story board identifier method to open the new view controller, i'm confused why it isn't opening. My condition is not working fine, i got null but when i got login and comes back to home screen again it again shows null.
My code is,
- (IBAction)History:(id)sender {
NSString *savedValue = [[NSUserDefaults standardUserDefaults]
stringForKey:#"pin"];
if ([savedValue length]==0) {
NSLog(#"INNNNN");
UIStoryboard *story=[UIStoryboard storyboardWithName:#"Main" bundle:nil];
RegisterViewController *vc = [story instantiateViewControllerWithIdentifier: #"Register"];
[self.navigationController pushViewController:vc animated:YES];
}else{
UIStoryboard *story=[UIStoryboard storyboardWithName:#"Main" bundle:nil];
DetailsViewController *vc = [story instantiateViewControllerWithIdentifier: #"detail"];
[self.navigationController pushViewController:vc animated:YES];
}
}
The story board looks like this,
if your All viewcontrollers does not embed in UINavigationcontroller (Root) then use presentViewController for navigation.
- (IBAction)History:(id)sender {
NSString *savedValue = [[NSUserDefaults standardUserDefaults]
stringForKey:#"pin"];
UIStoryboard *story=[UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *topViewVC;
if ([savedValue length]==0) {
RegisterViewController *vc = [story instantiateViewControllerWithIdentifier: #"Register"];
topViewVC = vc;
}else{
DetailsViewController *vc = [story instantiateViewControllerWithIdentifier: #"detail"];
topViewVC = vc;
}
[self presentViewController:topViewVC animated:true completion:nil];
}
option 2
if you want the pushViewController may be future purpose then dont do any changes in your code , just add UINavigationcontroller as root. Select your initial VC in storyboard then xcode menu --> Editor - -> Embed In --> Navigation Controller
It seems like you missed to embed your base view controller to navigation controller. Select your initial VC in storyboard. Go to Editor -> Embed In -> Navigation Controller.
I opened my project after a week and it seems, for all the new UIViewController I create in the StoryBoard, instantiateViewControllerWithIdentifier is returning is nil. All the ViewControllers which are already in the project are working fine.
GCHConnectViewController* gchCVC = [self.storyboard instantiateViewControllerWithIdentifier:#"GchConnect"];
navigationController.viewControllers = #[gchCVC];
First line is returning nil, my initial thought was that self.storyboard is returning nil, so I tried this and put breakpoint.
if (self.storyboard != nil) {
GCHConnectViewController* gchCVC = [self.storyboard instantiateViewControllerWithIdentifier:#"GchConnect"];
navigationController.viewControllers = #[gchCVC];
}
and its going to inside the if and crash at the second line..
I've also added the class and storyboard id correctly.
If I try to load another ViewController which was already there, it works, but not new ones.
Can't seem to figure out what is the problem. Any help is appreciated.
I've a navigation drawer. switching to other ViewController except with the one with identifier 'GchConnect' (ie. indexPath.section == 1 and indexpath.row == 0 ) works. They are all in the same storyboard
If you have multiple storyboards, self.storyboard returns the one that the current UIViewController is in. You need you check the name of storybaord for "GchConnect". And instantiate that storyboard with storyboardWithName:bundle:
UIStoryboard storyboard = [UIStoryboard storyboardWithName:bundle:nil];
GCHConnectViewController* gchCVC = [storyboard instantiateViewControllerWithIdentifier:#"GchConnect"];
use this code
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
HomepageViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:#"GchConnect"];
[self.navigationController pushViewController:vc animated:YES];
instead of
if (self.storyboard != nil) {
GCHConnectViewController* gchCVC = [self.storyboard instantiateViewControllerWithIdentifier:#"GchConnect"];
navigationController.viewControllers = #[gchCVC];
}
try to use:
if (self.storyboard != nil) {
GCHConnectViewController* gchCVC = [self.storyboard instantiateViewControllerWithIdentifier:#"GchConnect"];
navigationController.pushViewController(gchCVC, animated: true)
}
What you should do is either push or present view controller. At the moment you're trying to replace all viewcontrollers that are in your navigationcontroller,
Try this code,
if you present the viewcontroller use below code,
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
#"YourStoryBoardName" bundle:[NSBundle mainBundle]];
GCHConnectViewController* gchCVC = [storyboard instantiateViewControllerWithIdentifier:#"GchConnect"];
[self presentViewController:gchCVC animated:YES completion:nil];
or if you Push the viewcontroller use below code,
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
#"YourStoryBoardName" bundle:[NSBundle mainBundle]];
GCHConnectViewController* gchCVC = [storyboard instantiateViewControllerWithIdentifier:#"GchConnect"];
[self.navigationController pushViewController:gchCVC animated:YES];
hope its helpful
My problem was that I add localization and changes I add to the storyboard were getting updated to the simulator/device.
It fixed it by:
selecting the StoryBoard, then
Editor > Localization Locking > Reset Locking Controls
Be sure that the code module for the ViewController has been added to the target you are building for. This fixed it for me.
I have three View controller A,B,C.I have navigation controller attached to A view controller.In A i have some buttons,I have attached the button segue to B view controller.Onclick of the button i go to the B view controller.On B View controller i have UITableView on click of table view item i am launching the C view controller.below is the code for that
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row==0)
{
NSLog(#"first cell");
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"BusinessCard"];
[self presentViewController:vc animated:YES completion:nil];
}
else if(indexPath.row==1)
{
NSLog(#"second cell");
}
else if(indexPath.row==2)
{
NSLog(#"third cell");
}
}
But on C view controller the navigation Bar is not appearing.I think C view controller is not linked to the Navigation Controller.
You use navigationController push method to display navigation bar in C viewController
try this code:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"BusinessCard"];
[self.navigationController pushViewController:vc animated:YES];
Use the code below
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"BusinessCard"];
UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:vc];
[self presentViewController:nav animated:YES completion:nil];
You need to present it using UINavigationController as modal.
Hope it helps.
use this:
[self.navigationController pushViewController:vc animated:YES];
You can use "Push" segue and embed your ViewController in Navigation Controller, and then use its Navigation Controller's functions like pushViewController and popViewControllerAnimated
Answer are all correct, but i just want to explain things..
//This line gets the storyboard with the name "Main" which contains all
//the setup you made for UI (User interface)
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
//While this like gets the view inside your storyboard with
//storyboard ID/indentifier `BusinessCard `
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"BusinessCard"];
//Lastly, this line is correct presenting the viewcontroller BUT this doesn't add your
//viewcontroller to the array of viewControllers inside navigationController
//
//Also, this line makes you present the viewController above the
//rootViewController of window which is in your case the navigationController
//
This is you Error
[self presentViewController:vc animated:YES completion:nil];
//This is what you are looking for, and the correct one for your implementation
//
//This will let you add the `vc`(viewController) to the array of viewController
//in navigationController, to confirm that you can check the `self.navigationController.viewControllers`
//which will return the array of viewController inside your navigationController
This is the Answer
[self.navigationController pushViewController:vc animated:YES];
Hope this helps you, and my explanation is apprehendable.. Cheers
i needed to change the tab bar position to the top on my view controller and i found out how to do it. The problem is that now in one of the tabs i need to push a new view controller but when i do , the navigation bar doesn't show up. I tried embedding the new view controller in another navigation controller and it worked (showed the navigation bar) but it didn't showed the back button item. is there a way to do this?
Here some screen shots:
this is how i am navigating between tabs
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
switch (item.tag) {
case 1:
if (Home == nil) {
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"Main"
bundle:nil];
Home = [sb instantiateViewControllerWithIdentifier:#"Home"];
}
[self.view insertSubview:Home.view belowSubview:mainTabBar];
break;
case 2:
if (Activity == nil) {
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"Main"
bundle:nil];
Activity = [sb instantiateViewControllerWithIdentifier:#"Activity"];
}
[self.view insertSubview:Activity.view belowSubview:mainTabBar];
break;
case 3:
if (Discover == nil) {
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"Main"
bundle:nil];
Discover = [sb instantiateViewControllerWithIdentifier:#"Discover"];
}
[self.view insertSubview:Discover.view belowSubview:mainTabBar];
break;
case 4:
if (Profile == nil) {
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"Main"
bundle:nil];
Profile = [sb instantiateViewControllerWithIdentifier:#"Profile"];
}
[self.view insertSubview:Profile.view belowSubview:mainTabBar];
break;
default:
break;
}
this is the view controller i need to push.
Rather that instantiate your controllers when selecting them, you should instantiate and add them to an array then do this in the AppDelegate when you set up the window:
self.tabBarController.viewControllers = localViewControllersArray;
Just in case anyone needs this, the way i solved it was making all the tab view controllers childs of the main view controller so it managed the navigation by itself.
[self addChildViewController:self.Home];
I am having difficulty using IIViewDeck in my app. I am using storyboard. I have managed to bring leftviewcontroller which is a tableviewcontroller with cells that are supposed to segue to particular viewcontroller in the storyboard. How do I do this? I have put this in appdelegate.
UIStoryboard* mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UIViewController *menuController = [mainStoryboard instantiateViewControllerWithIdentifier:#"mainSideNavMenu"];
UINavigationController* navigationController = (UINavigationController *) self.window.rootViewController;
self->viewDeckController = [[IIViewDeckController alloc] initWithCenterViewController:navigationController leftViewController:menuController rightViewController:nil];
self.window.rootViewController = self->viewDeckController;
You can manage it with code, with the didSelectRowAtIndexPath method of your table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
if (indexPath.row == 0) {
MyTripsViewController* newController = [sb instantiateViewControllerWithIdentifier:#"ViewController1"];
[self.viewDeckController closeLeftViewBouncing:^(IIViewDeckController *controller) {
self.viewDeckController.centerController = newController;
}];
}
if (indexPath.row == 1) {
MyTripsViewController* newController = [sb instantiateViewControllerWithIdentifier:#"ViewController2"];
[self.viewDeckController closeLeftViewBouncing:^(IIViewDeckController *controller) {
self.viewDeckController.centerController = newController;
}];
}
else {
MyTripsViewController* newController = [sb instantiateViewControllerWithIdentifier:#"ViewController3"];
[self.viewDeckController closeLeftViewBouncing:^(IIViewDeckController *controller) {
self.viewDeckController.centerController = newController;
}];
}
}
So you have to defined you row / viewController couple.
I know it's a bit late, but someone might find the answer useful at some point.
Max's answer is generally a good answer. However, the original poster's question is a bit vague. He/She didn't specify if the transition is required in the center view controller or the left one.
Assuming the transition is in the left view controller:
If you want push transition, you can use a code like:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
UINavigationController * nc = (UINavigationController *)self.viewDeckController.leftController;
UIViewController * vc = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"yourView"]; // use the name of the view (not the navigation view controller)
[nc pushViewController:vc animated:YES];
}
}
If you want Modal transition, just replace the code within the previous if statement clause with the following code:
UIViewController * vc = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"yourViewName"]; // don't use the name of a navigation controller
UINavigationController * nc = [[UINavigationController alloc] initWithRootViewController:vc];
[self presentViewController:nc animated:YES completion:nil];
The navigation controller in this code is used to get the navigation bar shown. If you don't need the navigation bar, just omit the navigation controller definition and pass the view controller as a parameter instead in the presentViewController: animated: completion: method.
Assuming the transition is in the center view controller:
You can use the code Max had provided. Keep in mind that the way he showed the new view controller is by replacing the existing center view controller. The new view controller is neither pushed nor modally showed. If you want to push new view controller to the existing center view controller, you should use the same way I introduced the transition in the previous two pieces of code above. So, it should look like this:
Push Transition: (the following code should replace the code within the if statement clause in the first piece of code)
[self.viewDeckController closeLeftView];
UINavigationController * nc = (UINavigationController *)self.viewDeckController.centerController;
[nc pushViewController:[[[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"yourViewName"] parentViewController] animated:YES];
Modal Presentation: (the following code should replace the code within the if statement clause in the first piece of code)
[self.viewDeckController closeLeftView];
UINavigationController * center = (UINavigationController *)self.viewDeckController.centerController;
UIViewController * vc = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"yourViewName"];
UINavigationController * nc = [[UINavigationController alloc] initWithRootViewController:vc];
[center presentViewController:nc animated:YES completion:nil];