views:

286

answers:

3

Let me first describe the context of the problem. I have 2 UIViewController call AdminViewController and ButtonReorderViewController. AdminViewController contain 1 button. ButtonReorderViewController contains 1 button and 1 picture. Button in AdminViewController tie to an event call goToReorderButton. The content of goToReorderButton are below:

ButtonReorderViewController *buttonReorder = [[ButtonReorderViewController alloc] initWithNibName:@"ButtonReorderViewController" bundle:[NSBundle mainBundle]];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:buttonReorder];  //Add a Navigation Controller to the root view
[navController setNavigationBarHidden:TRUE];
buttonReorder = (ButtonReorderViewController *) navController;
[[buttonReorder view] setFrame:CGRectMake(0, -20, 320, 470)];
[self.view addSubview:buttonReorder.view];

I use UINavigationController to allow me to swipe left and right.So I am in AdminViewController, and I click on goToReorderButton, it load ButtonReorderViewController. I am able to swipe left and right (awesome !!!) So I click the button in ButtonReorderViewController call goToAdmin, simply to go back to the AdminViewController

-(void) goToAdmin{
     [self.view removeFromSuperview];
}

However, as soon as I go back to AdminViewController, I cant click anything at all. The program does not seg fault, it just that I cant click the button at all. if I remove the line buttonReorder = (ButtonReorderViewController *) navController; inside goToReorderButton, then everything work fine. Any idea how to fix this?

A: 

I am not sure but i think you can use PusViewController method of NavigationController to do your job.

jAmi
A: 

Sounds like the app crashed on that line you pointed out. Does the debug console output anything when it crashes?

Looks like the line buttonReorder = (ButtonReorderViewController *) navController; is failing on the cast. Are you sure ButtonReorderViewController extends UINavigationController? If not, then you can't cast it like that.

Joel
+1  A: 

I'm not sure that I follow you, but I think that you should use the UINavigationController methods to navigate between the view controllers:

The admin view controller should be the root view controller of the navigation controller and then you may push and pop the reorder view controller.

In app delegate (applicationDidFinishLaunching: method):

// TODO: Instantiate the adminController

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:adminController];
[window addSubview:navController.view];

In AdminViewController (button touch up inside event handler method):

// TODO: Instantiate the buttonReorder

[self.navigationController pushViewController:buttonReorder animated:YES];

In ButtonReorderViewController (back button touch up inside event handler method):

[self.navigationController popViewControllerAnimated:YES];

Of course, you have to instantiate the view controllers before using them...


Cheers,
Michael.

Michael Kessler
I have very similar structure like what u suggest, just a bit more complex. Let say `viewA` is the one I `initWithRootViewController`, since `viewA` will try to display contain from coredata. Let say first view of `viewA`, display 4 object, and there are 8 objects, then swipe right will display the rest of the object. That why `viewA` as UINavigation controller (take advantage of push and pop view). However, if I try to go admin view, then I will not `pushView`, but instead `addSubview`, and then return by `removeFromSuperView`. It work fine, but now I add another component in
Harry Pham
(Please read above first) The new component allow user to reorder object position in view. So in `Admin` view, now I spawn of `viewB` by `addSubView:viewB`. `viewB` kind of have to same function as `viewA`, so I want to have the ability to `push and pop view`. So I want to make `viewB` a `UINavigationController` like `viewA`. If I click `Return button` then I call `removeFromSuperView` to return back to `Admin`, however, as soon as I come back to `Admin` view, I cant click anything
Harry Pham
As i wrote, none of your view controllers should be a navigation controller. You have ONLY one (in the suggested structure) navigation controller and you just push and pop your (3) view controllers. By the way, the "back" button is built-in in UINavigationItem that is added automatically in the top (the header) - you don't have to implement this button by yourself. Just remove the `[navController setNavigationBarHidden:TRUE];` line... See some Apple's sample applications. Nav. controller is used almost in each sample application. And always in the same way. Don't try to reinvent the wheel...
Michael Kessler
Let me rethink my structure. The reason why I have `addSubView` and `push and pop view` is this: Let say that I am in `Admin` view, then I `push` 3 view on the stack, if I click on the `return` button (which suppose to bring me direct back to the admin view), I have to `pop` 3 times to get back to the three times (keep track of that can be a pain). But if I `addSubView`, I then can just `removeFromSuperView`, to get directly back to `Admin`
Harry Pham
One question though, when we `push and pop view` on or off the stack, is there just one stack that everything go in and out or each viewController has their own stack?
Harry Pham
1. Why do you need to push 3 view controllers at once? You should push each controller only when you want it to appear.
Michael Kessler
2. You also have `popToRootViewControllerAnimated:` method if you are in the third controller and want to go back to the first one (Admin).
Michael Kessler
3. Each UINavigationController manages one stack of view controllers. You may have more than one UINavigationController in one application. Perfect example for that is tab controller where each tab contains separate navigation controller...
Michael Kessler
I restructure my design, it work like u suggested now. TY
Harry Pham