mluton / embeddedswapping Goto Github PK
View Code? Open in Web Editor NEWDemonstration of how to make a custom container view controller manage multiple child view controllers using storyboards.
License: MIT License
Demonstration of how to make a custom container view controller manage multiple child view controllers using storyboards.
License: MIT License
I basically tried implementing the code that you so graciously posted for the iPad instead, and I keep getting this error when I run it:
2013-05-27 17:55:22.509 CustomContainers[20795:c07] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<ViewController 0x7543fe0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key swapViewControllers.'
*** First throw call stack:
(0x1c93012 0x10d0e7e 0x1d1bfb1 0xb7ce41 0xafe5f8 0xafe0e7 0xb28b58 0x232019 0x10e4663 0x1c8e45a 0x230b1c 0xf57e7 0xf5dc8 0xf5ff8 0xf6232 0x453d5 0x4576f 0x45905 0x4e917 0x1296c 0x1394b 0x24cb5 0x25beb 0x17698 0x1beedf9 0x1beead0 0x1c08bf5 0x1c08962 0x1c39bb6 0x1c38f44 0x1c38e1b 0x1317a 0x14ffc 0x1eed 0x1e15)
libc++abi.dylib: terminate called throwing an exception
Any suggestions or help on getting it to work would be very much appreciated. Thanks!
Richard
Hello ... I also need to work with three viewcontroller instead of two, but the difference for me is that I do not need three buttons to run the following but using a single button ... Could you help me understand how I can add the third ViewController not change the function of your original button?
Hello,
Will you be switching this project to 100% swift code?
I've been away from coding for a while and thought It'd be a good project to try to learn swift so I actually just tried by changing all code to swift and it loads the first viewcontroller however when I click the button to swapviewcontrollers to the second one I always get this really weird error that there is no segue named "secondSegue" present although it clearly is :/
Think it's creating a new instance of containerviewcontroller or something so everything is reset to nil I don't know!
Happy to hear from ya
Nick
I experimented further by adding a textfield and a button to the FirstViewController and making the SecondViewController a table view controller on the iPad. When the application launches, the container view is initially blank instead of displaying the first view controller properly. After pressing the swap button, the second viewcontroller with its tableview displays properly, and when the swap button is pressed again, the first view controller displays properly. Any ideas on how to fix the initially blank first view controller?
I've been trying to alter your example to work with multiple buttons and VCs. (much like your 3 buttons branch, I just discovered) So far so good, I can select 5 different views with different buttons without issue. However now I'm trying to set each VC's delegate to the main view controller (the one before the container view controller) so they can send data to it.
I've not been able to get this to work for some reason... Do you have any ideas on how to reach it?
I know it can normally be set like so:
myViewController *destination = [segue destinationviewcontroller]
destination.delegate = self
But since your example uses a previously allocated instance like so:
self.firstViewController = segue.destinationviewcontroller
I can't seem to assign its delegate in any way... I must be overseeing something.
Thanks in advance!
Hi Michael
My name's Gian. I've been learning how to develop in Objective-C for a month so I'm not that expert and maybe my issue is really stupid (and sorry also for my english but I'm italian!)
I followed your instructions and i did a container view controller that can swipe between other two v.c.
Inside one of them i put a textField but here's my issue. When I write something and than I swipe in the other view, the text "disappears" whenever I come back to that view controller.
So maybe it doesn't just swipe between the views, but it creates a new instance every time that i push the button.
What I have to do to avoid it? I hope I explained myself
thanks
Gian
i have customize your code to make it work for more than 2 viewcontrollers, it starts crashing when ever i start quickly moving on each controllers, can you please check what i m doing wrong here.
@interface ContainerViewController ()
@Property (strong, nonatomic) NSString *currentSegueIdentifier;
@Property (strong, nonatomic) VenueDetailViewController *venueDetailController;
@Property (strong, nonatomic) MenuViewController *venueMenuController;
@Property (strong, nonatomic) LocationViewController *venueMapController;
@Property (assign, nonatomic) BOOL transitionInProgress;
@implementation ContainerViewController
(void)viewDidLoad
{
[super viewDidLoad];
self.transitionInProgress = NO;
self.currentSegueIdentifier = SegueIdentifierFirst;
[self performSegueWithIdentifier:self.currentSegueIdentifier sender:nil];
}
(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Instead of creating new VCs on each seque we want to hang on to existing
// instances if we have it. Remove the second condition of the following
// two if statements to get new VC instances instead.
if (([segue.identifier isEqualToString:SegueIdentifierFirst]) && !self.venueDetailController) {
self.venueDetailController = segue.destinationViewController;
}
if (([segue.identifier isEqualToString:SegueIdentifierSecond]) && !self.venueMenuController) {
self.venueMenuController = segue.destinationViewController;
}
if (([segue.identifier isEqualToString:SegueIdentifierThird]) && !self.venueMapController) {
self.venueMapController = segue.destinationViewController;
}
// If we're going to the first view controller.
if ([segue.identifier isEqualToString:SegueIdentifierFirst]) {
// If this is not the first time we're loading this.
if (self.childViewControllers.count > 0) {
[self swapFromViewController:[self.childViewControllers objectAtIndex:0] toViewController:self.venueDetailController];
}
else {
// If this is the very first time we're loading this we need to do
// an initial load and not a swap.
[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];
}
}
// By definition the second view controller will always be swapped with the
// first one.
else if ([segue.identifier isEqualToString:SegueIdentifierSecond]) {
[self swapFromViewController:[self.childViewControllers objectAtIndex:0] toViewController:self.venueMenuController];
}
else if ([segue.identifier isEqualToString:SegueIdentifierThird]) {
[self swapFromViewController:[self.childViewControllers objectAtIndex:0] toViewController:self.venueMapController];
}
}
(void)swapFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController
{
toViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[fromViewController willMoveToParentViewController:nil];
[self addChildViewController:toViewController];
[self transitionFromViewController:fromViewController toViewController:toViewController duration:0.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) {
[fromViewController removeFromParentViewController];
[toViewController didMoveToParentViewController:self];
self.transitionInProgress = NO;
}];
}
(void)swapViewControllers :(int)segueTag
{
if (self.transitionInProgress) {
return;
}
self.transitionInProgress = YES;
switch (segueTag) {
case 1:
{
self.currentSegueIdentifier = SegueIdentifierFirst;
break;
}
case 2:
{
self.currentSegueIdentifier = SegueIdentifierSecond;
break;
}
case 3:
{
self.currentSegueIdentifier = SegueIdentifierThird;
break;
}
default:
break;
}
[self performSegueWithIdentifier:self.currentSegueIdentifier sender:nil];
}
Hi,
If I call the below code in -viewDidLoad
it is working as expected
[self performSegueWithIdentifier:self.currentSegueIdentifier sender:nil];
But when I call it in -viewWillAppear
it crashes
With the following error
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'There are unexpected subviews in the container view. Perhaps the embed segue has already fired once or a subview was added programmatically?
My flow is as follows
ViewControllerA is having the embedded container, from ViewControllerA I am presenting ViewControllerB
When I dismis ViewControllerB, I need to refresh the embeded viewcontroller child so inorder to do that is was callig the -performSegueWithIdentifier
in -viewWillAppear
but that crashes.
My Query is how do I refresh the childViewControllers or call the -performSegueWithIdentifier
in -viewWillAppear
.
Sorry to bug you again, but I was trying to see if I could load the second view controller first by just simply changing the line self.currentSegueIdentifier = SegueIdentifierFirst in the ContainerViewController's viewDidLoad method to self.currentSegueIdentifier = SegueIdentifierSecond instead. Doing so, however, always results in a crash. I get this error message in the console:
2013-06-01 12:21:14.470 CustomContainers5[5485:c07] * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array'
*** First throw call stack:
(0x159b012 0x12a8e7e 0x1550b44 0x2eb2 0x634b87 0x2cedd2 0x2597 0x2ce1c7 0x2ce232 0x758ea1 0x634b99 0x634c14 0x2ce16c 0x2ce232 0x21d3d5 0x21d76f 0x21d905 0x226917 0x1ea96c 0x1eb94b 0x1fccb5 0x1fdbeb 0x1ef698 0x25fddf9 0x25fdad0 0x1510bf5 0x1510962 0x1541bb6 0x1540f44 0x1540e1b 0x1eb17a 0x1ecffc 0x21dd 0x2105)
libc++abi.dylib: terminate called throwing an exception
(lldb)
I'd like to be able to have the ViewController class tell the ContainerViewController which of the two view controllers to display initially in the app that I'm building. Any suggestions or workarounds? Thanks!
HI There!
I´m trying to include a button inside of FirstViewController view. I add the button in the storyboard but when I create the action to print anything, it doesn´t work. I only can use elements of ViewController and I can´t use anything of this view. Buttons, scrolls... How can I send the delegate? I want to use FirstViewController, not remain with ViewController
Thank you very much
I am trying to pass data from my view controller to child view controllers via container controller. How can I do that?
Hi Michael, Thank you for posting this easy to understand straight forward method for using container views with multiple view controllers.
What I would like to know however, is how to navigate from child controller to child controller using buttons on the child controllers, not a button on the root view controller.
Can you help me get on the right track? So far I have not been able to figure it out.
Hello, there a issue when we insert into a view any scroll view element, like text scroll view, the scroll just dont work, do u know any way to resolve this issue i really need a solution
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.