ios changing class of view controller - ios

I have few buttons in main view controller. Using storyboards, I desire to segue from two of those buttons to the same tab bar controller and depending on which button is pushed to change the class of one of view controllers of given tab bar controller.
Is it possible? Appreciate any advice.

You can make use of prepareForSegue:(UIStoryboardPopoverSegue *)segue sender:(id)sender in the controller where you have define the buttons in order to send information to the destination controller.
See for detail : https://stackoverflow.com/a/11464268/1749367

In Storyboard connect your ViewController with the TabBarController add your respective tabs to TabBarController
Add two buttons in viewController in Storyboard add assign tags to them 1 and 2 respectively.
In viewController.m
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UIButton *mybutton = (UIButton *)sender;
if ([segue.identifier isEqualToString:#"tabbar"])
{
UITabBarController *tabbar = segue.destinationViewController;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
if(myButton.tag == 0)
{
UIViewController *tab1 = [storyboard instantiateViewControllerWithIdentifier:#"tab1"];
}
else
{
UIViewController *tab1 = [storyboard instantiateViewControllerWithIdentifier:#"tab2"];
}
UIViewController *otherTab= [storyboard instantiateViewControllerWithIdentifier:#"Othertab"];
tabbar.viewControllers = [NSArray arrayWithObjects:tab1,othertab, nil];
}
}

Related

Adding child view controller to my parent view controller is not working

I am trying to add the "child_view_controller" to the "parent_view controller". I am using this for my iPad app, where the requirement is to add the another controller(child controller) to the parent controller. Moreover I have to establish the communication from the child view controller to the parent view controller for which I am using notification. But the child view controller in not getting added up. I am posting the code.
-(void)addPresentViewController:(UIViewController *)viewController storyBoardId:(NSString *)stroyBoardId withFrame:(CGRect)frame contact:(NgnContact *)contact
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
viewController = [storyboard instantiateViewControllerWithIdentifier:storyBoardId];
viewController.view.frame = frame;
//viewController.contactToSendChat=contact;
[self addChildViewController:viewController];
[self.view addSubview:viewController.view];
[viewController didMoveToParentViewController:self];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"cpdc_check_embed"]) {
self.checkVC = segue.destinationViewController;
}
}
...where checkVC is a property on the container controller:
#property (weak,nonatomic) <Viewcontroller name> * checkVC;
You just have to set your embed segue's Storyboard ID to whatever you want (in this case, cpdc_check_embed):
..and then check the identifier in -prepareForSegue:sender:.
Still not an outlet, but cleaner than Matt's (IMHO) and more specific than Caleb's, and you still get a nice-looking storyboard:

Changing Tab bar programmatically

I have created tabbar in storyboard .and i am trying to change default tab bar programmatically using following code.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:#"TAB"])
{
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UITabBarController *tabBar = [storyboard instantiateViewControllerWithIdentifier:#"tabBar"];
tabBar.selectedViewController = [tabBar.viewControllers objectAtIndex:1];
}
}
But its not working.How i can do it? its not working at all.
Also i want to go back to home view controller when user select first tab.how we can achieve this functionality.
From the current selected view controller you can change tab selected index using code below.
[self.tabBarController setSelectedIndex:1];
To add Tab bar controller to viewcontrollers hirarchy IF YOU ARE USING NAVIGATION AS BASE then you can use:
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UITabBarController *tabBar = [storyboard instantiateViewControllerWithIdentifier:#"tabBar"];
tabBar.selectedViewController = [tabBar.viewControllers objectAtIndex:1];
[self.navigationController pushViewController:tabBar animated:BOOL];
You can change tab from programmatically:-
[self.tabBarController setSelectedIndex:1];
You can access tab bar's object from storyboard. In storyboard select tabBarController and set storyboard Id. Refer image.
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil]; //please check your main storyboard name.
UITabBarController *tabBar = [storyboard instantiateViewControllerWithIdentifier:#"tabBar"];
tabBar.selectedViewController = [tabBar.viewControllers objectAtIndex:1];

iOS, TabBar and NavigationBar got hidden after adding pushViewController

I want to pass the variable " uid " between the FirstViewController and SecondViewController, so I used the following code to pass it. It successfully pass it, but the problem it hides the NavigationBar and the TabBar !
How can I solve it?
SecondViewController *vc2 = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
vc2.uid = uid;
[self.navigationController pushViewController:vc2 animated:YES];
Why dont you use storyboard segues to solve this? Just embed you view controller on a Navigation View controller and call performSegueWithIdentifier function.
Just add prepareForSegue function to your class and set uid there:
- (void) prepareForSegue: (UIStoryboardSegue *) segue sender: (id) sender
{
if([segue.identifier isEqualToString:#"secondViewSegue"])
{
//**OPTION A**: If you are using Push segue
SecondViewController *secondVC = (SecondViewController
*)segue.destinationViewController;
//**OPTION B**: ***ELSE, If you are using a Modal segue
UINavigationController *navController = (UINavigationController*)segue.destinationViewController;
SecondViewController *secondVC = (SessionViewController *)navController.topViewController;
//END OF OPTION B
secondVc.uid = uidValue;
}
}

Embed a UIViewController in a NavigationController using segues

I have a viewController that is usually (most often) accessed by using a push segue. This viewController needs to be embedded in a UINavigationController. So typically, this is no problem. The push segue manages pushing the viewController, and therefore the viewController has it's UINavigationController.
My issue is that in a few cases, I'd like to present this same exact viewController using a modal segue. When I do this, the viewController is not embedded in a navigationController. Is there a way to do this using segues?
I know this can be done purely in code without segues by creating a UINavigationController, setting the rootView as the viewController and then presenting that modally. That can be done using code like this :
MyViewController *viewController = [[MyViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
[self presentModalViewController:navController animated:YES];
But how do I do this same thing but using Segues?
Update
For further clarity, here is some code to supplement how I used the accepted answer in the prepareForSegue method.
When Using Modal Segue
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue destinationViewController] isEqualToString:#"Modal to MyVC"])
{
UINavigationController *nav = [segue destinationViewController];
MyViewController *vc = [nav topViewController];
//setup vc
}
}
When Using Push Segue
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue destinationViewController] isEqualToString:#"Push to MyVC"])
{
MyViewController *vc = [segue destinationViewController];
//setup vc
}
}
In your Storyboard, you can embed a ViewController in a Navigation Controller by selecting the View Controller and then picking from the menu at the top Editor->Embed In->Navigation Controller. From another view controller, you control drag to this Navigation controller to set up the modal segue. You can also control drag to the original View Controller to set up segues to it without the Navigation Controller.

iOS navigationController pushViewController not working

I have a storyboard with a UINavigationController. The root view controller of the navigation controllers is called rootViewController.
I am trying to programmatically change the view controller (depending on other conditions) to another view controller called loginViewController.
I am trying to do this in the viewDidLoad from the rootViewController like this:
- (void)viewDidLoad
{
[super viewDidLoad];
loginViewController *viewController = [[loginViewController alloc] init];
[self.navigationController pushViewController:viewController animated:YES];
}
I am not getting any errors but it's not working.
It just loads the navigation controller with the top nav back button but the rest of the screen is black. It's like it's not loading any view controller.
I am trying to figure out what I am doing wrong.
Any help with this would be great.
Thanks
First add loginViewController class in storyboard and and connect ViewController class to loginViewController class and give identifier name as "identifier_Name".
- (void)viewDidLoad
{
[super viewDidLoad];
[self performSegueWithIdentifier:#"identifier_Name" sender:nil];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:#"identifier_Name"])
{
loginViewController *viewController = [segue destinationViewController];
}
}
If you're using storyboard, create your login view in the storyboard, link your rootVC to the loginVC with a segue, choose an identifier (for example: "goToLogin"). and then to go in the login view, and use:
[self performSegueWithIdentifier:#"goToLogin" sender:self];

Resources