I have created UISwipeGestureRecognizer sample app. When I slide left a new view is appear as the picture shows.
I want to edit this red view from my nib file. How can I add another UIView from the nib & connect to this menuDrawer??
This is my Code
MainViewController.h file
#interface MainViewController : UIViewController{
UIView *menuDrawer;
}
#property (readonly,nonatomic)UISwipeGestureRecognizer *recognizer_open , *recognizer_close;
#property (readonly,nonatomic) int menuDrawerX,menuDrawerWidth;
-(void)handleSwipe :(UISwipeGestureRecognizer *)sender;
-(void)drawerAnimation;
#end
MainViewController.m file
#implementation MainViewController
#synthesize menuDrawerWidth,menuDrawerX,recognizer_open,recognizer_close;
-(void)handleSwipe :(UISwipeGestureRecognizer *)sender{
[self drawerAnimation];
}
-(void)drawerAnimation{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:-5];
CGFloat new_x = 0;
if (menuDrawer.frame.origin.x < self.view.frame.origin.x) {
new_x = menuDrawer.frame.origin.x + menuDrawerWidth;
}else{
new_x = menuDrawer.frame.origin.x - menuDrawerWidth;
}
menuDrawer.frame = CGRectMake(new_x, menuDrawer.frame.origin.y, menuDrawer.frame.size.width, menuDrawer.frame.size.height);
[UIView commitAnimations];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
int statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
menuDrawerWidth = self.view.frame.size.width *0.75;
menuDrawerX = self.view.frame.origin.x - menuDrawerWidth;
menuDrawer = [[UIView alloc]initWithFrame:CGRectMake(menuDrawerX, self.view.frame.origin.y+statusBarHeight, menuDrawerWidth, self.view.frame.size.height-statusBarHeight)];
menuDrawer.backgroundColor = [UIColor redColor];
recognizer_close = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(handleSwipe:)];
recognizer_open = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(handleSwipe:)];
recognizer_open.direction = UISwipeGestureRecognizerDirectionLeft;
recognizer_close.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:recognizer_open];
[self.view addGestureRecognizer:recognizer_close];
[self.view addSubview:menuDrawer];
}
Please check it out: https://github.com/mutualmobile/MMDrawerController
This is example of making menu drawer in the application.
Hope, it will may help you,
:)
You can directly add your menu drawer view in your MainViewController's nib file. Set correct frame(Which is origin.x is -(width of menu drawer)). Create a outlet for that view in your controller.
What you can do:
Rather than loading the view in code, you should have a nib (storyboard of xib) file for the entire controller. In that xib file you should create all the views that you want to and link them to the controller using IBOutlets.
If you do this you then would be able to put all the views that you want as children of the red one and all of them will be loaded from the nib.
What you should do:
Even tho the option I said before works, I am guessing you are trying to somehow show a menu within the drawer, or any kind of contect not fully related with the rest of the window. Usually it is better to separate logic in different controllers, so I would create a separated controller to handle the drawer. Just create a new VC in the same storyboard where you create the other one. When the method drawerAnimation is triggered:
1) Load the VC from the storyboard programmatically
2) Add the VC as a child view controller using (content would be the drawer):
[self addChildViewController: drawerViewController]; // self is your main controller, and content is the one that handles the drawer view.
drawerViewController.view.frame = CGRectMake(blabla..); //
[self.view addSubview:drawerViewController.view];
[drawerViewController didMoveToParentViewController:self]; // 3
3) Animate it
If you want to completely remove the view at some point you can just remove the entire VC using:
[drawerViewController willMoveToParentViewController:nil]; // 1
[drawerViewController.view removeFromSuperview]; // 2
[drawerViewController removeFromParentViewController]; // 3
Related
I have three buttons and a UIView (I call it containerView), tap each of the buttons, the containerView will show a View in a UIViewController through a custom segue, the buttons use one IBAction method(switchView), I put the three buttons in a IBOutletCollection called navButtons;
and in viewDidLoad I call switchView to make the containerView show first view controller.
And the code run well, no error,the only question is when I tap first button,the first UIViewController will shown in containerView, the UIImageView (buttons,labels,etc) should be in the centre of the screen, but it never behave like this when it's first loaded, when I tap another button then tap first button again,it behaved as expected,
I have no idea what happened and what's the difference between first load and tapping it again.
IS THERE ANYTHING I MISSED IN THE CODE ?
I am not good at English,Sorry for anything unclear .
- (void)viewDidLoad {
[super viewDidLoad];
self.availableIdentifier = [[NSMutableArray alloc] initWithObjects:#"Seg1",#"Seg2",#"Seg3", nil];
[self switchView:self.navButtons[0]];
}
- (IBAction)switchView:(UIButton *)sender
{
[self setSelectedIndexs:(int)sender.tag];
}
- (void)setSelectedIndexs:(int)index
{
[self performSegueWithIdentifier:self.availableIdentifier[index] sender:self.navButtons[index]];
}
//Code of Custom Segue:
-(void)perform
{
ViewController *controller = (ViewController *)self.sourceViewController;
UIViewController *destController = (UIViewController *)self.destinationViewController;
for (UIView *view in controller.containerView.subviews)
{
[view removeFromSuperview];
}
controller.currentController = destController;
[controller.containerView addSubview:destController.view];
[controller.containerView setTranslatesAutoresizingMaskIntoConstraints:NO];
[destController didMoveToParentViewController:controller];
}
I think you should add:
destController.view.frame = controller.containerView.frame;
before
controller.currentController = destController;
is your clipSubViews set in UIViewController ?
Solved. Thanks for the answer of Bojan Bozovic ;
CGRect frame = controller.containerView.frame;
frame.origin.y = 0;
destController.view.frame = frame;
Add above before controller.currentController = destController;
This problem is driving me crazy. I'm trying to change the viewController when the user changes the selected "tab" of the segmented control. I've spent a couple hours researching and haven't been able to find an answer that works or is done through storyboard.
It really bother me since setting a tab application is so easy, but trying to use the segmented control like the tab application is just not working. I already know how to detect which index is selected in the segmented control. How can I achieve this?
Thank you very much.
NOTE: Answer updated with view controller containment code for iOS 5+ including #interface section
In an app of mine, I have a view controller with a Segment Control in the Navigation Bar and clicking on the "tabs" switches view controllers. The basic idea is to have an array of view controllers and switch between them using the Segment Index (and the indexDidChangeForSegmentedControl IBAction.
Example code (iOS 5 or later) from my app (this is for 2 view controllers but it's trivially extended to multiple view controllers); the code is slightly longer than for iOS 4 but will keep the object graph intact. Also, it uses ARC:
#interface MyViewController ()
// Segmented control to switch view controllers
#property (weak, nonatomic) IBOutlet UISegmentedControl *switchViewControllers;
// Array of view controllers to switch between
#property (nonatomic, copy) NSArray *allViewControllers;
// Currently selected view controller
#property (nonatomic, strong) UIViewController *currentViewController;
#end
#implementation UpdateScoreViewController
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
// Create the score view controller
ViewControllerA *vcA = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewControllerA"];
// Create the penalty view controller
ViewControllerB *vcB = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewControllerB"];
// Add A and B view controllers to the array
self.allViewControllers = [[NSArray alloc] initWithObjects:vcA, vcB, nil];
// Ensure a view controller is loaded
self.switchViewControllers.selectedSegmentIndex = 0;
[self cycleFromViewController:self.currentViewController toViewController:[self.allViewControllers objectAtIndex:self.switchViewControllers.selectedSegmentIndex]];
}
#pragma mark - View controller switching and saving
- (void)cycleFromViewController:(UIViewController*)oldVC toViewController:(UIViewController*)newVC {
// Do nothing if we are attempting to swap to the same view controller
if (newVC == oldVC) return;
// Check the newVC is non-nil otherwise expect a crash: NSInvalidArgumentException
if (newVC) {
// Set the new view controller frame (in this case to be the size of the available screen bounds)
// Calulate any other frame animations here (e.g. for the oldVC)
newVC.view.frame = CGRectMake(CGRectGetMinX(self.view.bounds), CGRectGetMinY(self.view.bounds), CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
// Check the oldVC is non-nil otherwise expect a crash: NSInvalidArgumentException
if (oldVC) {
// Start both the view controller transitions
[oldVC willMoveToParentViewController:nil];
[self addChildViewController:newVC];
// Swap the view controllers
// No frame animations in this code but these would go in the animations block
[self transitionFromViewController:oldVC
toViewController:newVC
duration:0.25
options:UIViewAnimationOptionLayoutSubviews
animations:^{}
completion:^(BOOL finished) {
// Finish both the view controller transitions
[oldVC removeFromParentViewController];
[newVC didMoveToParentViewController:self];
// Store a reference to the current controller
self.currentViewController = newVC;
}];
} else {
// Otherwise we are adding a view controller for the first time
// Start the view controller transition
[self addChildViewController:newVC];
// Add the new view controller view to the ciew hierarchy
[self.view addSubview:newVC.view];
// End the view controller transition
[newVC didMoveToParentViewController:self];
// Store a reference to the current controller
self.currentViewController = newVC;
}
}
}
- (IBAction)indexDidChangeForSegmentedControl:(UISegmentedControl *)sender {
NSUInteger index = sender.selectedSegmentIndex;
if (UISegmentedControlNoSegment != index) {
UIViewController *incomingViewController = [self.allViewControllers objectAtIndex:index];
[self cycleFromViewController:self.currentViewController toViewController:incomingViewController];
}
}
#end
Original example (iOS 4 or before):
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
// Create the score view controller
AddHandScoreViewController *score = [self.storyboard instantiateViewControllerWithIdentifier:#"AddHandScore"];
// Create the penalty view controller
AddHandPenaltyViewController *penalty = [self.storyboard instantiateViewControllerWithIdentifier:#"AddHandPenalty"];
// Add Score and Penalty view controllers to the array
self.allViewControllers = [[NSArray alloc] initWithObjects:score, penalty, nil];
// Ensure the Score controller is loaded
self.switchViewControllers.selectedSegmentIndex = 0;
[self switchToController:[self.allViewControllers objectAtIndex:self.switchViewControllers.selectedSegmentIndex]];
}
#pragma mark - View controller switching and saving
- (void)switchToController:(UIViewController *)newVC
{
if (newVC) {
// Do nothing if we are in the same controller
if (newVC == self.currentViewController) return;
// Remove the current controller if we are loaded and shown
if([self.currentViewController isViewLoaded]) [self.currentViewController.view removeFromSuperview];
// Resize the new view controller
newVC.view.frame = CGRectMake(CGRectGetMinX(self.view.bounds), CGRectGetMinY(self.view.bounds), CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
// Add the new controller
[self.view addSubview:newVC.view];
// Store a reference to the current controller
self.currentViewController = newVC;
}
}
- (IBAction)indexDidChangeForSegmentedControl:(UISegmentedControl *)sender {
NSUInteger index = sender.selectedSegmentIndex;
if (UISegmentedControlNoSegment != index) {
UIViewController *incomingViewController = [self.allViewControllers objectAtIndex:index];
[self switchToController:incomingViewController];
}
}
I'd say it's much simpler to change subviews within a UIViewController, you can set up your subviews in your storyboards and hook them up with IBOulets in your controller you can set the hidden property of your views to YES or NO depending on the control that was clicked.
Now, if you use #Robotic Cat's approach which is also a good solution you can have a little more modularity in how your app works, considering you'd have to place all your logic in one controller using the solution I presented.
UISegmentedControl is a little different in that it doesn't have a delegate protocol, you have to use the "add target" style. In your case what you want to do is add a target to be notified when the UISegmentedControl changes (which is likely the parent view controller), and then that target can deal with the tab switching.
For example:
[self.mainSegmentedControl addTarget:self action:#selector(changedSegmentedControl:) forControlEvents:UIControlEventValueChanged];
In this example, the code is being invoked from some view/controller that has access to the variable for the segmented control. We add ourself to get the changedSegmentedControl: method invoked.
Then you would have another method like so:
- (void)changedSegmentedControl:(id)sender
{
UISegmentedControl *ctl = sender;
NSLog(#"Changed value of segmented control to %d", ctl.selectedSegmentIndex);
// Code to change View Controller goes here
}
Note: this is untested code written from memory -- please consult the docs accordingly.
Take a look at this pod: https://github.com/xmartlabs/XLMailBoxContainer. It makes the UI animation among the view controllers. These view controller can extend UITableViewController or any other view controller.
I hope this help you!
I'm trying to make a form that spans three tabs. You can see in the screenshot below where the tabs will be. When the user taps a tab, the Container View should update and show a particular view controller I have.
Tab 1 = View Controller 1
Tab 2 = View Controller 2
Tab 3 = View Controller 3
The view controller shown above has the class PPAddEntryViewController.m. I created an outlet for the Container view within this class and now have a Container View property:
#property (weak, nonatomic) IBOutlet UIView *container;
I also have my IBActions for my tabs ready:
- (IBAction)tab1:(id)sender {
//...
}
- (IBAction)tab2:(id)sender {
//...
}
- (IBAction)tab3:(id)sender {
//...
}
How do I set the container in those IBActions to change the view controller that the Container View holds?
Among a few other things, here's what I've tried:
UIViewController *viewController1 = [self.storyboard instantiateViewControllerWithIdentifier:#"vc1"];
_container.view = viewController1;
...but it doesn't work. Thanks in advance.
Switching using Storyboard, Auto-layout or not, a Button of some sort, and a series of Child View Controllers
You want to add the container view to your view and when the buttons that 'switch' child view controllers are pressed fire off the appropriate segue and perform the correct setup work.
In the Storyboard you can only connect one Embed Segue to the Container View. So you create an intermediate handling controller. Make the embed segue and give it an identifier, for example EmbededSegueIdentifier.
In your parent view controller wire up the button or whatever you want and keep are reference to your child view controller in the prepare segue. As soon as the parent view controller loads the segue will be fired.
The Parent View Controller
#property (weak, nonatomic) MyContainerViewController *myContainerViewController;
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"EmbeddedSegueIdentifier"]) {
self.myContainerViewController = segue.destinationViewController;
}
}
It should be fairly easy for you to delegate to your container controller the button presses.
The Container Controller
This next bit of code was partly borrowed from a couple of sources, but the key change is that auto layout is being used as opposed to explicit frames. There is nothing preventing you from simply changing out the lines [self addConstraintsForViewController:] for viewController.view.frame = self.view.bounds. In the Storyboard this Container View Controller doesn't do anything more that segue to the destination child view controllers.
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"%s", __PRETTY_FUNCTION__);
[self performSegueWithIdentifier:#"FirstViewControllerSegue" sender:nil];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UIViewController *destinationViewController = segue.destinationViewController;
if ([self.childViewControllers count] > 0) {
UIViewController *fromViewController = [self.childViewControllers firstObject];
[self swapFromViewController:fromViewController toViewController:destinationViewController];
} else {
[self initializeChildViewController:destinationViewController];
}
}
- (void)initializeChildViewController:(UIViewController *)viewController
{
[self addChildViewController:viewController];
[self.view addSubview:viewController.view];
[self addConstraintsForViewController:viewController];
[viewController didMoveToParentViewController:self];
}
- (void)swapFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController
{
[fromViewController willMoveToParentViewController:nil];
[self addChildViewController:toViewController];
[self transitionFromViewController:fromViewController toViewController:toViewController duration:0.2f options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) {
[self addConstraintsForViewController:toViewController];
[fromViewController removeFromParentViewController];
[toViewController didMoveToParentViewController:self];
}];
}
- (void)addConstraintsForViewController:(UIViewController *)viewController
{
UIView *containerView = self.view;
UIView *childView = viewController.view;
[childView setTranslatesAutoresizingMaskIntoConstraints:NO];
[containerView addSubview:childView];
NSDictionary *views = NSDictionaryOfVariableBindings(childView);
[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[childView]|"
options:0
metrics:nil
views:views]];
[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[childView]|"
options:0
metrics:nil
views:views]];
}
#pragma mark - Setters
- (void)setSelectedControl:(ViewControllerSelectionType)selectedControl
{
_selectedControl = selectedControl;
switch (self.selectedControl) {
case kFirstViewController:
[self performSegueWithIdentifier:#"FirstViewControllerSegue" sender:nil];
break;
case kSecondViewController:
[self performSegueWithIdentifier:#"SecondViewControllerSegue" sender:nil];
break;
default:
break;
}
}
The Custom Segues
The last thing you need is a custom segue that does nothing, going to each destination with the appropriate segue identifier that is called from the Container View Controller. If you don't put in an empty perform method the app will crash. Normally you could do some custom transition animation here.
#implementation SHCDummySegue
#interface SHCDummySegue : UIStoryboardSegue
#end
- (void)perform
{
// This space intentionally left blank
}
#end
I recently found the perfect sample code for what I was trying to do. It includes the Storyboard implementation and all the relevant segues and code. It was really helpful.
https://github.com/mhaddl/MHCustomTabBarController
Update: UITabBarController is the recommended way to go, as you found out earlier. In case you'd like to have a custom height, here is a good start: My way of customizing UITabBarController's tabbar - Stackoverflow answer
As of iOS 5+ you have access to customize the appearance via this API; UIAppearance Protocol Reference. Here is a nice tutorial for that: How To Customize Tab Bar Background and Appearance
The most obvious way to achieve what you're looking for is to simply manage 3 different containers (they are simple UIViews) and implement each of them to hold whatever content view you need for each tab (use the hidden property of the containers).
Here is an example of what's possible to achieve with different containers:
These containers "swapping" can be animated of course. About your self-answer, you probably chose the right way to do it.
have a member variable to hold the viewController:
UIViewController *selectedViewController;
now in the IBActions, switch that AND the view. e.g.
- (IBAction)tab1:(id)sender {
UIViewController *viewController1 = [self.storyboard instantiateViewControllerWithIdentifier:#"vc1"];
_container.view = viewController1.view;
selectedViewController = viewController1;
}
to fire view did appear and stuff call removeChildViewController, didMoveToParent, addChildViewController, didMoveToParent
I got this to work by using a UITabBarController. In order to use custom tabs, I had to subclass the TabBarController and add the buttons to the controller in code. I then listen for tap events on the buttons and set the selectedIndex for each tab.
It was pretty straight forward, but it's a lot of junk in my Storyboard for something as simple as 3 tabs.
This problem is driving me crazy. I'm trying to change the viewController when the user changes the selected "tab" of the segmented control. I've spent a couple hours researching and haven't been able to find an answer that works or is done through storyboard.
It really bother me since setting a tab application is so easy, but trying to use the segmented control like the tab application is just not working. I already know how to detect which index is selected in the segmented control. How can I achieve this?
Thank you very much.
NOTE: Answer updated with view controller containment code for iOS 5+ including #interface section
In an app of mine, I have a view controller with a Segment Control in the Navigation Bar and clicking on the "tabs" switches view controllers. The basic idea is to have an array of view controllers and switch between them using the Segment Index (and the indexDidChangeForSegmentedControl IBAction.
Example code (iOS 5 or later) from my app (this is for 2 view controllers but it's trivially extended to multiple view controllers); the code is slightly longer than for iOS 4 but will keep the object graph intact. Also, it uses ARC:
#interface MyViewController ()
// Segmented control to switch view controllers
#property (weak, nonatomic) IBOutlet UISegmentedControl *switchViewControllers;
// Array of view controllers to switch between
#property (nonatomic, copy) NSArray *allViewControllers;
// Currently selected view controller
#property (nonatomic, strong) UIViewController *currentViewController;
#end
#implementation UpdateScoreViewController
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
// Create the score view controller
ViewControllerA *vcA = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewControllerA"];
// Create the penalty view controller
ViewControllerB *vcB = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewControllerB"];
// Add A and B view controllers to the array
self.allViewControllers = [[NSArray alloc] initWithObjects:vcA, vcB, nil];
// Ensure a view controller is loaded
self.switchViewControllers.selectedSegmentIndex = 0;
[self cycleFromViewController:self.currentViewController toViewController:[self.allViewControllers objectAtIndex:self.switchViewControllers.selectedSegmentIndex]];
}
#pragma mark - View controller switching and saving
- (void)cycleFromViewController:(UIViewController*)oldVC toViewController:(UIViewController*)newVC {
// Do nothing if we are attempting to swap to the same view controller
if (newVC == oldVC) return;
// Check the newVC is non-nil otherwise expect a crash: NSInvalidArgumentException
if (newVC) {
// Set the new view controller frame (in this case to be the size of the available screen bounds)
// Calulate any other frame animations here (e.g. for the oldVC)
newVC.view.frame = CGRectMake(CGRectGetMinX(self.view.bounds), CGRectGetMinY(self.view.bounds), CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
// Check the oldVC is non-nil otherwise expect a crash: NSInvalidArgumentException
if (oldVC) {
// Start both the view controller transitions
[oldVC willMoveToParentViewController:nil];
[self addChildViewController:newVC];
// Swap the view controllers
// No frame animations in this code but these would go in the animations block
[self transitionFromViewController:oldVC
toViewController:newVC
duration:0.25
options:UIViewAnimationOptionLayoutSubviews
animations:^{}
completion:^(BOOL finished) {
// Finish both the view controller transitions
[oldVC removeFromParentViewController];
[newVC didMoveToParentViewController:self];
// Store a reference to the current controller
self.currentViewController = newVC;
}];
} else {
// Otherwise we are adding a view controller for the first time
// Start the view controller transition
[self addChildViewController:newVC];
// Add the new view controller view to the ciew hierarchy
[self.view addSubview:newVC.view];
// End the view controller transition
[newVC didMoveToParentViewController:self];
// Store a reference to the current controller
self.currentViewController = newVC;
}
}
}
- (IBAction)indexDidChangeForSegmentedControl:(UISegmentedControl *)sender {
NSUInteger index = sender.selectedSegmentIndex;
if (UISegmentedControlNoSegment != index) {
UIViewController *incomingViewController = [self.allViewControllers objectAtIndex:index];
[self cycleFromViewController:self.currentViewController toViewController:incomingViewController];
}
}
#end
Original example (iOS 4 or before):
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
// Create the score view controller
AddHandScoreViewController *score = [self.storyboard instantiateViewControllerWithIdentifier:#"AddHandScore"];
// Create the penalty view controller
AddHandPenaltyViewController *penalty = [self.storyboard instantiateViewControllerWithIdentifier:#"AddHandPenalty"];
// Add Score and Penalty view controllers to the array
self.allViewControllers = [[NSArray alloc] initWithObjects:score, penalty, nil];
// Ensure the Score controller is loaded
self.switchViewControllers.selectedSegmentIndex = 0;
[self switchToController:[self.allViewControllers objectAtIndex:self.switchViewControllers.selectedSegmentIndex]];
}
#pragma mark - View controller switching and saving
- (void)switchToController:(UIViewController *)newVC
{
if (newVC) {
// Do nothing if we are in the same controller
if (newVC == self.currentViewController) return;
// Remove the current controller if we are loaded and shown
if([self.currentViewController isViewLoaded]) [self.currentViewController.view removeFromSuperview];
// Resize the new view controller
newVC.view.frame = CGRectMake(CGRectGetMinX(self.view.bounds), CGRectGetMinY(self.view.bounds), CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
// Add the new controller
[self.view addSubview:newVC.view];
// Store a reference to the current controller
self.currentViewController = newVC;
}
}
- (IBAction)indexDidChangeForSegmentedControl:(UISegmentedControl *)sender {
NSUInteger index = sender.selectedSegmentIndex;
if (UISegmentedControlNoSegment != index) {
UIViewController *incomingViewController = [self.allViewControllers objectAtIndex:index];
[self switchToController:incomingViewController];
}
}
I'd say it's much simpler to change subviews within a UIViewController, you can set up your subviews in your storyboards and hook them up with IBOulets in your controller you can set the hidden property of your views to YES or NO depending on the control that was clicked.
Now, if you use #Robotic Cat's approach which is also a good solution you can have a little more modularity in how your app works, considering you'd have to place all your logic in one controller using the solution I presented.
UISegmentedControl is a little different in that it doesn't have a delegate protocol, you have to use the "add target" style. In your case what you want to do is add a target to be notified when the UISegmentedControl changes (which is likely the parent view controller), and then that target can deal with the tab switching.
For example:
[self.mainSegmentedControl addTarget:self action:#selector(changedSegmentedControl:) forControlEvents:UIControlEventValueChanged];
In this example, the code is being invoked from some view/controller that has access to the variable for the segmented control. We add ourself to get the changedSegmentedControl: method invoked.
Then you would have another method like so:
- (void)changedSegmentedControl:(id)sender
{
UISegmentedControl *ctl = sender;
NSLog(#"Changed value of segmented control to %d", ctl.selectedSegmentIndex);
// Code to change View Controller goes here
}
Note: this is untested code written from memory -- please consult the docs accordingly.
Take a look at this pod: https://github.com/xmartlabs/XLMailBoxContainer. It makes the UI animation among the view controllers. These view controller can extend UITableViewController or any other view controller.
I hope this help you!
Wow, I gave a big thought on the question!
So I have a view Controller called "ContentView" within another view Controller (Main VC). The Main VC has a Navigation Controller which was created using Storyboards. And the contentView loads 3 different view controllers (vc1, vc2 and vc3) depending on the options that a Segmented Control has. So the question now is:
How can I load a new View Controller from the button within one of the subviews (vc2) that will appear once the user selects the option from the segmented control?
Although I have the visible view controller (vc2) in my storyboard, obviously I cannot connect an action to the button to the vc2' file's owner since the Nav Controller is on the Main VC.
I tried to access it with the following code:
AppDelegate *del = (AppDelegate *)[UIApplication sharedApplication].delegate;
UINavigationController *navigationController = (UINavigationController*)del.window.rootViewController;
DetalleMisMarcas *detalleMarcas = [[DetalleMisMarcas alloc] initWithNibName:#"DetalleMarcas" bundle:nil];
[navigationController pushViewController:detalleMarcas animated:YES];
But it does not work.
I have tried to find a solution from this forum, but I had no luck. Most consider the existence of a Main Window Xcode 4.2 does not have.
Finally, the way I load the 3 subviews, is here:
-(IBAction)segmentCtrlChanged:(id)sender {
UISegmentedControl *seg = sender;
if (seg.selectedSegmentIndex == 0)
{
MMViewController *mm= [self.storyboard instantiateViewControllerWithIdentifier:#"MMView"];
mm.view.frame = CGRectMake(0, 0, 320, 240);
[contentView addSubview:mm.view];
}
else if (seg.selectedSegmentIndex == 1) {
MPViewController *mp = [self.storyboard instantiateViewControllerWithIdentifier:#"MPView"];
mp.view.frame = CGRectMake(0, 0, 320, 240);
[contentView addSubview:mp.view];
}
}
-(IBAction)mainSubView:(id)sender
{
MMViewController *mm = [[MMViewController alloc] initWithNibName:#"MTView" bundle:nil];
[contentView addSubview:theMTView];
mm.view.frame = CGRectMake(0, 0, 320, 240);
}
Any ideas?
EDIT: Based on your comment, I think delegates will work better. This is how you may implement it.
Let's say in contentView controller class, add a delegate
// in the header file
#property (weak, nonatomic) id <ContentViewControllerDelegate> delegate;
#protocol ContentViewControllerDelegate <NSObject>
- (void)contentViewDidSelectAView:(UIView *)view;
#end
// in the implementation
- (IBAction)segmentCtrlChanged:(UISegmentedControl *)sender {
case 0:
[self.delegate contentViewDidSelectAView:[[self.storyboard instantiateViewControllerWithIdentifier:#"MMView"] view];
break;
// and the rest of the code
}
// in MainView header file
#interface MainViewController : UIViewController <ContentViewControllerDelegate>
// in MainView implementation
- (void)contentViewDidSelectAView:(UIView *)view {
[self.view addSubView:view];
}
I haven't actually implemented the above code. But I think this should be the way for a controller to talk to its parent. Hope this one helps.
I am not sure what exactly you are doing but let's say you have a UINavigationController in the root, and mainViewController is linked via segue as a relationship. As you have implemented, the segmentedControl in mainVC should be linked to -(IBAction)segmentCtrlChanged:(id)sender with valueChange event.
In this method you can switch the index, rather than using if, although they work the same, and you can also use UISegmentedControl * instead of id :
-(IBAction)segmentCtrlChanged:(UISegmentedControl *)sender {
switch(sender.selectedSegmentIndex) {
case 0: {
MMViewController *mm= [self.storyboard instantiateViewControllerWithIdentifier:#"MMView"];
[self.navigationController pushViewController:mm animated:YES];
break;
}
case 1: {
MPViewController *mp = [self.storyboard instantiateViewControllerWithIdentifier:#"MPView"];
[self.navigationController pushViewController:mp animated:YES];
break;
}
case 2: {
// similar to above
break;
}
default:
break;
}
vc1, vc2 and vc3, -- I assume -- are instances of UIViewController. So make sure that their class is linked, and that their identifiers are set properly. and then, just leave them as they are, because you are not going to make any segue for them individually. They are going to be pushed to the stack via code.
Hope it helps.