Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Thomas Gordon
2,284 PointsContainer View to multiple View Controllers
I finally got the view controller to stick to the size of the container. The issue I have now is getting my buttons to call the proper view controller to the container. I'm using regular UIButtons and not the toolbar and that is not an option for what I'm working on. Thanks for any suggestions and help.
1 Answer
Thomas Gordon
2,284 Points-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:SegueIdentifierHome]) {
if (!self.homeVC ) {
self.homeVC = segue.destinationViewController;
}
if (self.childViewControllers.count > 0) {
[self swapFromViewController:[self.childViewControllers objectAtIndex:1] toViewController:self.homeVC];
}
else
{
[self addChildViewController:segue.destinationViewController];
((UIViewController *)segue.destinationViewController).view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:((UIViewController *)segue.destinationViewController).view];
[segue.destinationViewController didMoveToParentViewController:self];
}
}
else if ([segue.identifier isEqualToString:SegueIdentifierCloset])
{
if (!self.closetVC ) {
self.closetVC = segue.destinationViewController;
}
[self swapFromViewController:[self.childViewControllers objectAtIndex:0] toViewController:self.closetVC];
}
else if ([segue.identifier isEqualToString:SegueIdentifierSocial])
{
if (!self.socialVC ) {
self.socialVC = segue.destinationViewController;
}
[self swapFromViewController:[self.childViewControllers objectAtIndex:0] toViewController:self.socialVC];
}
else if ([segue.identifier isEqualToString:SegueIdentifierStore])
{
if (!self.storeVC ) {
self.storeVC = segue.destinationViewController;
}
[self swapFromViewController:[self.childViewControllers objectAtIndex:0] toViewController:self.myStoreVC];
}
else if ([segue.identifier isEqualToString:SegueIdentifierShop])
{
[self swapFromViewController:[self.childViewControllers objectAtIndex:0] toViewController:segue.destinationViewController];
}
else if ([segue.identifier isEqualToString:SegueIdentifierMyStore])
{
if (!self.myStoreVC ) {
self.myStoreVC = segue.destinationViewController;
}
[self swapFromViewController:[self.childViewControllers objectAtIndex:0] toViewController:self.myStoreVC];
}
}
This the code I'm halfway through. Here's what was happening. I have a storyboard with a view controller that has a container on it. That container connects to multiple view controllers that I want called with a button (UIButton) press. You run the program and it starts at home, then press any button and the associated view controller fills the container. This is all perfect. The issue now is that after you press any button once, any subsequent button press gives nothing. The buttons' NSLog does show up so the button is showing as pressed, it's just down to this series of if statements that I'm trying to get it to function with any button an infinite amount of times.
Thanks Amit for looking into this. I love your videos on iOS and have had tons of fog removed from my thoughts on iOS development. I need to get back into your videos, just been super busy with school and this app.
After some testing it seems that is not where my problem is. I will leave that code but for all the segues in the above, I have a method for each button and will post one of them here.
-(void)swapViewControllerSocial; { if (self.transitionInProgress) { return; } NSLog(@"%@", self.currentSegueIdentifier); self.transitionInProgress = YES;
if ((self.currentSegueIdentifier = ([self.currentSegueIdentifier isEqualToString:SegueIdentifierHome]) ? SegueIdentifierSocial : SegueIdentifierHome )) {
[self performSegueWithIdentifier:self.currentSegueIdentifier sender:nil];
}
}
I'm fairly certain my problem is here. I just can't seem to get it to allow a subsequent button press but will continue grinding away until I get it to work. Thanks again Amit.
Amit Bijlani
Treehouse Guest TeacherYou don't seem to be resetting the transitionInProgress property anywhere.
Amit Bijlani
Treehouse Guest TeacherAmit Bijlani
Treehouse Guest TeacherCan you provide some code or screenshots? It's not clear what the problem is or what you are trying to achieve.