remove ViewControllers from stack - ios

I have a viewController (let's call it VC1), which is displayed when the application starts, it has a menu button that opens modalVC, in modalVC there are three buttons that open VC1, VC2, VC3 (at VC2, VC3 also have this menu button )
I run my application (on VC1), I press on the menu button (modalVC), turn on VC1.
It turns out that I have now two VC1 open? Because every time I press on VC1, is added to the memory of 1-2 mb. How can I remove them from memory?
Must somehow after selecting the controller menu (modalVC) to remove all that was earlier ..how?
The transition from the menu on the VC1, VC2, VC3 do so:
UINavigationController * navController = [[UINavigationController alloc] initWithRootViewController: VC1];
[self presentViewController: navController animated: YES completion: nil];
If i will push menu button and always select VC2 (10 times will do that) app will slow
i tried this after select VC in menu (modalVC)
NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray: self.navigationController.viewControllers];
[allViewControllers removeAllObjects];
self.navigationController.viewControllers = allViewControllers;

Your menu implementation is flawed by design. You are creating new instances of each ViewController whenever the user taps on a button, instead of using the one you had already created before. A menu like that should be implemented using ViewController Containment. This way, the container ViewController will create the VC1 VC2 and VC3 whenever they are needed, and won't re-create them if they are already there.
To find out how exactly you're gonna do that, just read the section called "Implementing a Custom Container View Controller" in Apple docs

Related

How do i handle navigation Back button view hierarchy?

Here is the problem
1) Rootview controller - MYAssetVC-> Embedded with NavigationController here pushing for button to another Addfilevc.
2) Addfilevc Has dropdownTextfield It will push to another vc have tableview selected row will display in the textfield.
3)if i select another value from the dropdown textfield it will push to the vc again there i will select.
Navigation bar back button will navigate to all my view hierarchy i want to handle this one. if i go to same view it should navigate back only once that to the recent visit how to do this.
As i am new to iOS. give any suggestion.
Navigation from 1->2->3
navigation backbtn 3->2->1
if i navigate like this 1->2->3-> backbutton 3->2 again 2->3 backbutton 3->2 again 2->3
IF i navigate now using back it is displaying all my route path it should navigate like 1->2->3> and 3->2->1 if any number of times i perform actions in 2 & 3.
1,2,3 are view controllers.
Create an IBAction for the back button and use popViewController.
[self.navigationController popViewControllerAnimated:YES];
This will help you to go back one page. You have to write this in all the pages where there is a back button and you want to go back one page.
If you want to go back directly to rootViewController, try this:
[self.navigationController popToRootViewControllerAnimated:YES];
And if you want to pop to any specific viewController in the stack, you run a for loop to find the viewController you want to navigate to and then simply popToViewController, like this:
for (UIViewController *viewController in self.navigationController.viewControllers) {
if ([viewController isKindOfClass:[Addfilevc class]]) {
[self.navigationController popToViewController:viewController animated:YES];
}
}
Hope this helps to clear your concept.
EDIT
In swift:
The [self.navigationController popViewControllerAnimated:YES]; will become self.navigationController?.popViewControllerAnimated(true)
The [self.navigationController popToRootViewControllerAnimated:YES]; will become navigationController?.popToRootViewControllerAnimated(true)
And the for loop will be as below:
You can use this if you are using storyboard
let switchViewController = self.storyboard?.instantiateViewControllerWithIdentifier("view2") as ComposeViewController
self.navigationController?.pushViewController(switchViewController, animated: true)
And the for-in loop
if let viewControllers = self.navigationController?.viewControllers {
for viewController in viewControllers {
self.navigationController!.popToViewController(viewController, animated: true);
}
}
Thanks.

IOS - Objective C - How do I remove Tableview Menu from my Login View Controller?

In my iOS Project, there is a login ViewController that sends the user to a TabViewController if he has the right credentials.
This TabViewController has 5 tabs, the fifth one is for Logout, which send the user back to the Login ViewController, and of course clears out the already filled credentials of the user.
My problem is that i have the menu of the TableView shown in my Login page.
How to i get rid of this menu in my Login ViewController page ?
I use Xcode6 & Objective-C
if you need any further explanations/source code of my problem, feel free to ask.
note:
In the beginning, I mean when the Login ViewController is first shown to the user, the menu doesn't show.
It turns out that whole UI architecture of your app is based on the UITabBarController. However it is not very good practice in your case. I would like to suggest you add separate modal controller for presenting login page.
Try:
self.tabBar.hidden = YES;
Assuming that you are using a storybaord I have the given solution
I created a sample application and tried replicating your issue, so here's the look at my storyboard
The way i designed it i have a separate login view controller and two view controller (Menu List and Logout) which are embedded in a tabbar controller.
If you're new to storyboard then embedding viewControllers with tabbarController is pretty much straight forward, you select the view controller first and then go to the editors menu in Xcode
Alright now coming back to business, code which i added on the IBAction of the login screen button is given below where MainTabbar is the storyboardID of the TabMaster controller
AppDelegate *appdel = [UIApplication sharedApplication].delegate;
UIStoryboard *storyBoard = appdel.window.rootViewController.storyboard;
TabMasterController *tabController = [storyBoard instantiateViewControllerWithIdentifier:
#"MainTabbar"];
[appdel.window setRootViewController:tabController];
When i executed the application everything was OK and I was able to see the tabbar items after I hit the action button on login screen
Now its time to write some code for the logout tabbar item, so I selected the view controller assigned to the Main tabbar controller and added the delegate mentod of UITabbarController which looks some what like this
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
// Since i have two tabbar items 1 generally means logout in my case
if (tabBarController.selectedIndex ==1) {
AppDelegate *appdel = [UIApplication sharedApplication].delegate;
UIStoryboard *storyBoard = appdel.window.rootViewController.storyboard;
ViewController *tabController = [storyBoard instantiateViewControllerWithIdentifier:
#"LoginVC"];
[appdel.window setRootViewController:tabController];
}
}
LoginVC is the storyboardID of the Login View controller
After adding the above code when i used to tap on the logout tabbar item I was able to go back to the login screen in my storyboard.
Hope that helps.
Either you can add tab bar item programmatically or you can add in storyboard
If programmatically then add like
self.tabBarController.viewControllers = [NSArray arrayWithObjects:
[[UINavigationController alloc] initWithRootViewController:self.myContactsController],
[[UINavigationController alloc] initWithRootViewController:self.searchController],
[[UINavigationController alloc] initWithRootViewController:self.registrationController],
[[UINavigationController alloc] initWithRootViewController:self.loginController], nil];
Set tabbarController as initial view controller in storyboard
After checking login credential, On success
// to get list of current tab bar items
NSMutableArray *tbViewControllers = [NSMutableArray arrayWithArray:[self.tabBarController viewControllers]];
// to remove tab bar items using index value
[tbViewControllers removeObjectAtIndex:3];
[tbViewControllers removeObjectAtIndex:2];
// to add tab bar items
[tbViewControllers addObject:[[UINavigationController alloc] initWithRootViewController:self.myProfileController]];
[tbViewControllers addObject:[[UINavigationController alloc] initWithRootViewController:self.logoutController]];
// to set pre selected tab bar item
self.tabBarController.selectedIndex=2;
// set array items in tab bar
[self.tabBarController setViewControllers:tbViewControllers];
Fixed it, it's not the optimal solution but it worked for me:
what I did is :
1- I deleted the Tab Bar Item from my Login ViewController
2- I inserted a logout button in my table view screens
3- I added segues from my Table view screens to the login screen in my storyboard.
This way if the user clicks on the logout button , he will be directed to the login screen & cannot go back where he was unless he enters his credentials.

Dismiss a previous Viewcontroller

I am current developing an game with multiple viewcontroller.
VC1(intro) -> VC2(game selection) ->VC3,VC4,VC5(games) -> VC6(display marks)
all the arrows are modal segue
I use unwind segue to go back from VC6 to VC2 and would like to replay. However, when select a game to go to VC3, the VC3 is not brand new as expected. It appears as the finished game condition.
If I really want to replay it after showing the marks, how can I achieve this with segue and Viewcontroller?
Moreover, how can I completely remove the instance of a previous viewcontroller?
You can go back on the parentViewController chain:
UIViewController *viewController = nil;
do {
viewController = self.parentViewController;
} while (![self isViewControllerImLookingFor:viewController]);
Or you could implement a custom navigation stack manager, and store an array of navigation controllers, similar to how UINavigationController does.
You can manipulate the view controller array of your navigation controller like so
NSMutableArray *navigarray = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
[navigarray removeLastObject]; //navigarray contains all vcs
[[self navigationController] setViewControllers:navigarray animated:YES];

Push ViewController in NavigationController from side menu

I'm using a UINavigationController and I have a side menu in it. The problem is that when I want to push a UIViewController in the NavigationController from the side view it doesn't work. To do it, is use that code:
[self.slidingViewController resetTopView];
CDRecentChats* recentchats = [[CDRecentChats alloc]init];
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"conversation"];
[recentchats.navigationController pushViewController:controller animated:YES];
As you can see, I first close de side menu and then call the NavigationController to push a new ViewController. Using that code, the only thing that happens is that the menu is closed, but the NavigationController doesnnt push any ViewController. what can I do?
popToViewController means to go back to one that has already been made part of the navigation controller's stack. If you're trying to show a new one, use pushViewController.

Interface Builder IB - Navigation Controller, skipping a view

Hello I am new to iOS and have a question about navigating through my views.
I am using IB wiring up the PREVIOUS and NEXT buttons in the nav bar that pushes my views. This all works fine. However I am having trouble finding out where exactly what I need to do or where I need to place the code so I can skip over a view. To simplify my situation...
-I have 1 Nav Controller and 4 View Controllers named VC1, VC2, VC3, VC4. Each VC has a .H/.M
-Starting from VC1, they follow one after the other.
Say I want to skip VC3 and jump right to VC4 based on a setting in VC2. Where would I put the code to do this? Would I need to unhook the IBAction method from the NAV buttons at VC3?
I do apologize if this has been covered before. If there is a tut or if you know of a post that answers this, please let me know. I did do a search but the search was returning generic posts probably due to me using the wrong terminology.
Thanks in advance.
A couple of thoughts:
If you want to push from VC2 to either VC3 or to VC4, rather than having VC3 immediately push to VC4 in special cases, I think it's better to just have VC2 push directly to the appropriate view controller. Thus you might have an IBAction in VC2 that would do this for you:
- (IBAction)pushToNext:(id)sender
{
BOOL skipToVC4 = ... // put in whatever logic you'd use to bypass VC3 and go directly to VC4
UIViewController *nextController;
if (skipToVC4)
{
nextController = [self.storyboard instantiateViewControllerWithIdentifier:#"VC4"];
// obviously, if using NIBs, you'd do something like:
// nextController = [[ViewController4 alloc] initWithNibName:#"VC4" bundle:nil];
}
else
{
nextController = [self.storyboard instantiateViewControllerWithIdentifier:#"VC3"];
}
[self.navigationController pushViewController:nextController animated:YES];
}
This way, when you pop back from VC4, you'll pop back directly to the appropriate view controller (e.g. if you pushed from VC2 to VC4, when you pop, you'll pop right back to VC2 automatically.
And, obviously, if you're using storyboards, rather than manually invoking pushViewController, you could have two segues from VC2 (one to VC3 and one to VC4), give them appropriate identifiers, and then just invoke performSegueWithIdentifier to segue to the appropriate view controller. But the idea is the same: You can define an IBAction that performs the appropriate segue depending upon whatever logic you so choose.
You say that you have "PREVIOUS and NEXT buttons in the nav bar that pushes my views", I wonder about your "PREVIOUS" button. Is that doing a popViewControllerAnimated? Generally, a "NEXT" button will push to a new view controller, but the "PREVIOUS" button should not push to the previous view, but pop back to it. If you don't pop back, you can end up with multiple instances of some of your prior view controllers. Thus, the "PREVIOUS" button should be linked to an IBOutlet that does something like:
- (IBAction)popToPrevious:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
When popping back, you'll obviously pop back to the view controller that you pushed from. If you want to skip a few of the view controllers as you're popping back in iOS versions prior to 6.0, you would use popToViewController or popToRootViewControllerAnimated. For example, let's say that you pushed from VC1 to VC2, to VC3, to VC4. If you want to pop back from VC4 all the way to VC1, you would hook up and IBAction in VC4 like:
- (IBAction)popToRoot:(id)sender
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
Or, if you wanted to pop back from VC4 to VC2, you would
- (IBAction)popToVC2:(id)sender
{
for (UIViewController *controller in self.navigationController.viewControllers)
{
if ([controller isKindOfClass:[ViewController2 class]])
{
[self.navigationController popToViewController:controller animated:YES];
return;
}
}
}
You can avoid this iteration through the navigationController.viewControllers if you passed a reference of VC2 to VC3 and then again to VC4, but sometimes the above technique is easier.
By the way, if you're supporting iOS 6 and above, only, and are using storyboards, you can also use unwind segues, which are a more elegant way of popping back to a particular view controller. But it's not clear whether (a) you're using storyboards; and (b) you're supporting iOS 6 and above only, so I'll refrain from a discussion of unwind segues at this point.
First:
Show us some code, where you are pushing new view controllers, maybe your whole navigation controller code
Second (Solution):
I assume:
Your prev/next buttons are linked to your navigationController class
you have the appropriate methods (prevPressed:/nextPressed:), which are called, when you click one of the buttons
I can help you with the following:
you know which controller is visible at the moment with the visibleViewController #property
each time you click on a button in the navBar you can ask the visibleViewController which next/previous view controller should be pushed/popped
Best solution would be, if all of your controllers VC1/2/3/4 are a subclass of a viewController class, which defines a method in it's interface:
- (Class)nextViewControllerClass;
- (Class)previousViewControllerClass;
and in the implementation:
- (Class)nextViewControllerClass {
return [VC4 class];
}
- (Class)previousViewControllerClass {
return [VC1 class];
}
And in your navigationController code the do this:
- (IBAction)next:(id)sender {
UIViewController *nextViewController = [[[self.visibleViewController nextViewControllerClass] alloc] init];
[self pushViewController:nextViewController animated:YES];
}

Resources