Change the view to a new view with SplitViewController - ios

I have a very simple design with 3 views (.xib):
MeuPrimeiroViewController
MeuSegundoViewController
MeuTerceiroViewController
I am using the SplitViewController class to divide the screen into two parts, by default is configured on only appdelegate.m class to split views 'MeuPrimeiroViewController' along with 'MeuSegundoViewController'.
All Im trying to do is, when the user tap the button inside my 'MeuPrimeiroViewController', he start a method existing inside my 'MeuSegundoViewController' and he push the 'MeuTerceiroViewController', for this I'm doing:
MeuPrimeiroViewController.m
-(IBAction)mudar:(id)sender{
MeuSegundoViewController *VC2 = [[MeuSegundoViewController alloc] init];
[VC2 mudar];
}
MeuSegundoViewController.m
-(void)mudar{
NSLog(#"Change Screen");
MeuTerceiroViewController *VC3 = [[MeuTerceiroViewController alloc] init];
[self.navigationController pushViewController:VC3 animated:YES];
}
The method 'mudar' is calling but the screen don't change, how to solve this problem?

If you want to change the detail view you should make in MeuPrimeiroViewController:
[self.splitViewController showDetailViewController:VC3 animated:YES];
If you want to change the the "list" view you should make in the MeuPrimeiroViewController:
[self showViewController:VC2 animated:YES];
I'm not sure if it's VC2 or VC3 you want to show, but I hope this helps!

Related

MVYSideMenu pop to root viewcontroller

I am using MVYSideMenu in my application and I can present new controller using "changeContentViewController" of MVYSideMenu but it presents new controller so from here I want to pop back to my root(HomeViewController) so I have done lot of research on this but couldn't find solution.
So if anybody knows the solution please help me.
Thanks in advance.
MVYSideMenu is making rootViewController, when you are trying to open any view from this. If you are making rootViewController, then you can not able to pop from that.
So if you want to go back, then you have to Push that view instead of making rootViewController.
I think, Right Now you are using following code for open any controller for side menu:
ChildViewController *contentVC = [[ChildViewController alloc] initWithNibName:#"ChildViewController" bundle:nil];
appDelegate.nav = [[UINavigationController alloc] initWithRootViewController:contentVC];
appDelegate.nav.navigationBarHidden=TRUE;
[[self sideMenuController] changeContentViewController:appDelegate.nav closeMenu:YES];
So Please use following code instead of above code. Here nav is object of UINavigationController in AppDelegate.
ChildViewController *contentVC = [[ChildViewController alloc] initWithNibName:#"ChildViewController" bundle:nil];
[appDelegate.nav pushViewController:contentVC animated:YES];
[[self sideMenuController] changeContentViewController:appDelegate.nav closeMenu:YES];
So You can able to push any view from side menu. And Now just try to pop this view. I am sure that you can able to pop this view after using this.
Make sure that, If you do this, then may be memory leakage is there. Because all times viewControllers are adding to the navigation controller. So My suggestion is, don't do this, if you can convince your client. :) Otherwise, you have to make your own side menu.
EDIT :-
Yes we can able to enable / disable side menu for some view controllers. Please add following methods into MVYSideMenuController.m file. And call this method from viewWillAppear of your viewControllers.
- (void)addGestures {
if (!_panGesture) {
_panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePanGesture:)];
[_panGesture setDelegate:self];
// [self.view addGestureRecognizer:_panGesture];
}
if (!_tapGesture) {
_tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(toggleMenu)];
[_tapGesture setDelegate:self];
[self.view addGestureRecognizer:_tapGesture];
}
}
- (void)removeGestures{
[self.view removeGestureRecognizer:_panGesture];
_panGesture = nil;
}
You can easily call this methods LocalNotifications. May be addGestures already there. You can remove gestures by calling removeGestures method when you needed in your controller. But do not forgot to addGestures when you needed side menu after removing gestures.
Hope, this is what you're looking for. Any concern get back to me. :)

How to switch between view controllers and get rid of the previous one

In android, switching between activities, is fairly straightforward
you call
Intent intent = new Intent(this,NextActivity.class); <- define the next activity
startActivity(intent); <- start the next activity
finish(); < -get rid of the current activity
now in iOS i know how to do this:
UIViewController *nextviewcontroller = [[UIViewController alloc]initWithNibName:#"nextvc" bundle:nil];
[self presentViewcontroller:nextviewcontroller animated:YES completion:nil];
How do I get rid of the current view controller? so that currentviewcontroller dies after presenting nextviewcontroller ?
[self dismissViewController:YES]; doesnt seem to do the trick
the lifecycle methods viewWillDisappear and viewDidDisappear are called even if I don't call [self dismissViewController:YES];
i want "currentviewcontroller" to be removed from the memory, and from the viewcontroller stack, so that clicking "back" in "nextviewcontroller" will go to some thirdviewcontroller that was before currentviewcontroller
In iOS is different, since there's no concept of Activity and everything is more focused on the app itself (in Android you can mix activities from different apps). Therefore, there's no concept of "view controller stack".
The most similar concept is the "navigation stack" of navigation controllers, where you actually push and pop new view controller into some kind of linear navigation. A navigation bar is automatically created and populated with back buttons.
presentViewController will show your view controller modally upon the current one, but you can't thrash the presenting one since it's holding and containing ("defining context") the new one.
If you use a navigation controller for your navigation hierarchy (I don't know if you can), you can override the back button and use something like
UIViewController * prev = self.navigationController.viewControllers[self.navigationController.viewControllers.count -2 ]
[self.navigationController popToViewController:prev animated:YES]
With a modal view controller, you may try something like (I haven't tried but it may work)
[self.presentingViewController.navigationController popViewControllerAnimated:YES]
You should write one of these code into the target action of your close button.
iOS doesn't maintain a global stack of controllers in the way that Android does. Each app shows a controller at its root, and that one is responsible for showing the other controllers in the app. Controllers can display other controllers modally using presentViewcontroller:animated:completion: but the presenting controller remains underneath the presented one.
If your current controller is the root controller, then instead of using presentViewcontroller:animated:completion: you'd just do this:
self.view.window.rootViewController = nextViewController;
It's very common for the root controller to be a UINavigationController, which does manage a stack of controllers. If that is the case, and if your current controller is at the top of the stack, you'd do this:
[self.navigationController popViewControllerAnimated:NO];
[self.navigationController pushViewController:nextViewController animated:YES];
If your setup is different, you'd do something different; it's hard to say what without knowing more. But it's most likely that you'd be in the UINavigationController case.
In the viewDidAppear of your nextviewcontroller you could add :
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
NSArray *controllers = self.navigationController.viewControllers;
NSMutableArray *newViewControllers = [NSMutableArray arrayWithArray:controllers];
[newViewControllers removeObjectAtIndex:[controllers count]-2];
self.navigationController.viewControllers = newViewControllers;
}
There is nothing available like this in iOS but you can achieve it doing something like below
NSArray *viewControllers=[self.navigationController viewControllers];
NSMutableArray *newControllers=[[NSMutableArray alloc] init];
for(int i=[viewControllers indexOfObject:self];i<viewControllers.count;i++){
[newControllers addObject:[viewControllers objectAtIndex:i]];
}
[self.navigationController setViewControllers:[[NSArray alloc] initWithArray:newControllers]];
I have tried the method of storing all the view controllers in an array but it didn't work for me . When you try popViewController it will move to the View Controller which is last in the stack.
You can make 2 navigation controllers and switch between them and also switch between the view controllers of a particular Navigation Controller.
For eg.
You can switch between 2 Navigation Controller using the following code:
FirstNavController *fisrtView=[storyboard instantiateViewControllerWithIdentifier:#"firstnavcontroller"];
self.window.rootViewController = firstView;
}else{
SecondNavController *secondView=[storyboard instantiateViewControllerWithIdentifier:#"loginnavcontroller"];
self.window.rootViewController = secondView;
}
If your FirstNavController has 2 ViewControllers then you can switch between them using pushViewController
SecondViewController *sc = [self.storyboard instantiateViewControllerWithIdentifier:#"secondviewcontroller"];
[[self navigationController] pushViewController:sc animated:YES];
and popViewController
[self.navigationController popViewControllerAnimated:YES];

Switching between view controllers

I have three view controllers. I used the flipsideproject template and then added another view controller.
There is a button on the first view controller that goes to the second view controller. There is a button on the second view controller that goes back to the first one. When switching between the first and second, those buttons always work.
It is the same situation with the second and third view controller. When I try to transfer between the first to second to third and then back to first, it does not work.
(1-->2-->3-->2-/->1) My poorly drawn diagram depicts the situation.
I had all of the back buttons connected to the back IBAction, which I thought was the problem. I then made another IBAction, but it has not fixed the problem.
1st view controller = MainViewController
2nd VC = FlipSideViewController
3rd VC = ChooseAlarmSound
This is for going 2->1 (this is the problem I think. It sometimes works)
- (IBAction)done:(id)sender
{
[self.delegate flipsideViewControllerDidFinish:self];
}
This is for going 2->3
- (IBAction)chooseSound:(id)sender
{
ChooseAlarmSound *controller = [[[ChooseAlarmSound alloc] initWithNibName:#"ChooseAlarmSound" bundle:nil] autorelease];
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:controller animated:YES];
}
This is for going 3->2
- (IBAction)goBack:(id)sender
{
FlipsideViewController *controller = [[[FlipsideViewController alloc] initWithNibName:#"FlipsideViewController" bundle:nil] autorelease];
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:controller animated:YES];
}
You presented your 3rd VC (going from 2 to 3) using modalviewcontroller. But then you tried to go back to 2nd VC (from 3rd to 2nd) using another modalVC. That will not let you go back to the previous instance of 2nd VC. You need to use dismissmodalviewcontrolleranimated method to do this. Checkout Apple website on modalviewcontroller class reference for detail info on this.
As suggested by user523234, all you need to do is call
[self dismissModalViewControllerAnimated:YES]
in the
- (IBAction)goBack:(id)sender
method of the 3rd view controller, instead of what you're doing, which is creating another instance of the 2nd view controller and presenting it.
The reason it's not working now, is because when you press the done button in the 2nd view controller it calls
- (IBAction)done:(id)sender
{
[self.delegate flipsideViewControllerDidFinish:self];
}
which is sending a message the 2nd view controller's delegate, which you haven't set in the case where you're going from 3->2.

pushViewController does not cause new controller to draw view

Preface: I am not using *.xib files.
I instantiate a UINavigationController in a class that effectively serves as my 'rootViewController'. This 'rootViewController' also has two UITableViewController members that are drawn on different sections of the iPad screen. One of which is set as the root view for the navigation controller. Let's call it tableViewControllerA.
The problem is, when I invoke pushViewController on a valid UINavigationController, I see no effect:
[tableViewControllerA.navigationController pushViewController:tableViewControllerX animated:YES];
I've gathered from the posts I've searched today, that this push method should in turn cause the screen to redraw the top of stack controller.view. This is not what I'm seeing.
It seemed there was a disconnect in my implementation, and it was time to reference a working example in my environment (xcode 4.0). Assuming the canned templates would provide a working basis, I created a new navigation-based applications. I simply modified didFinishLaunchingWithOptions: as follows.
UIViewController *view1 = [[UIViewController alloc] init];
UIViewController *view2 = [[UIViewController alloc] init];
view1.title = #"view1";
view2.title = #"view2";
[self.navigationController pushViewController:view1 animated:YES];
[self.navigationController pushViewController:view2 animated:YES];
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:view1];
[view1 release];
[view2 release];
I found similar results. When I launch the simulator the screen title reads the title of whatever the self.window.rootViewController is pointing at. With the code as is, the title of the resulting top screen reads "view1". When I initWithRootViewController:view2, the resulting top screen reads "view2".
So please tell me I'm stupid cuz xyz...
Thanks.
Here are some references and suggestions:
Simple tutorial for navigation based application:
http://humblecoder.blogspot.com/2009/04/iphone-tutorial-navigation-controller.html
Here is another one to create the step by step navigation controller and adding the views:
http://www.icodeblog.com/2008/08/03/iphone-programming-tutorial-transitioning-between-views/
and here a bit advance with navigation + tab bar controller:
http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/CombiningToolbarandNavigationControllers/CombiningToolbarandNavigationControllers.html
Without seeing your code, I have 2 theories:
Your syntax and calls are wrong when you do the push. Use this as a model:
-(void)Examplemethod {
AnotherClassViewController *viewController = [[[AnotherClassViewController alloc] initWithNibName:#"AnotherClassView" bundle:nil] autorelease];
[self.navigationController pushViewController:viewController animated:YES];
}
You are never adding the navigation controller to the view hierarchy which never adds the view either. Take a look at this.

push last view from UIViewCOntroller

Hi guys i have quick question, So i have uset UIViewController to push some views on my window based application and
here is the question:
Accessibility_view_Controller * accessibility_controller = [Accessibility_view_Controller alloc];
accessibility_controller.path_for_save = self.path_2;
accessibility_controller.Supporters_survey = [ self.Supporters_survey_mut objectAtIndex:row_value];
accessibility_controller.Supporters_info = [self.Supporters_info_mut objectAtIndex:row_value];
[self.navigationController pushViewController:accessibility_controller animated:YES];
[accessibility_controller release];
i am on the accessibility_controller view now but i want to move back to the view from where i called this, i mean the parentview , i know i can do it from navigation bar but i want to do it from tapping a row. SO what will be the code for pushing the parent view controller of the accessibilty controller on the window. .
Just use [[self navigationController] popViewControllerAnimated:YES]; to go back. This is very well documented here

Resources