Swift popToViewController does not working - swift

I'm new in Swift and iOS dev however I think that I know how to eat it.
I have a problem when trying to use popToViewController. The problem is that I want to "clear" UINavigation stack when pressing the button.
So I did this like here:
navigationController?.popToViewController((navigationController?.viewControllers[0])!, animated: false)
I want to have only first ViewController in the stack.
Before I click on the button stack contains
([<EmpMan.ViewController: 0x7fd191535ba0>,
<EmpMan.TableViewController: 0x7fd191564be0>,
<EmpMan.NewEmployee: 0x7fd1916c0320>])
When I click on the button the stack looks:
([<EmpMan.ViewController: 0x7fd191535ba0>,
<EmpMan.TableViewController: 0x7fd191564be0>,
<EmpMan.NewEmployee: 0x7fd1916c0320>,
<EmpMan.TableViewController: 0x7fd19171dbf0>])
I think after popToViewController the stack should contains only ViewController. Why it does not work?

If what the button does is a segue, than just do the "Show detail segue and you are done.
Otherwise, make a boolean in the viewController with the button that is set to true when you press the button. Create a secondary segue out of each of the possible segue routes out of the controller. Then if that boolean is set to true, use a "Show Detail Segue" as your next segue instead of a "Show Segue".
Show Detail automatically clears the navigation stack. There are other ways of handling this as well, but I am just throwing you an acceptable answer that works. Read more about segues and custom segues and the navigation stack to get a thourough understanding. It is worth your time if you want to get heavy into iPhone App dev.

Related

How to create an unwind segue w/default navigation item back button?

With a navigation controller in Swift/Xcode, can I create an unwind segue from a View Controller to the preceding one without creating a custom UIBarButton?
Basically, if I replace the "back" button that's created by default on the navigation item, I could link that to "Exit" and create an unwind segue. However, I wanted to see if there's a way to use the existing "back" button and link a segue identifier to it.
(The one thing I've found out from research which is useful is the default "back" button actually belongs to the preceding View Controller, but this doesn't help me solve me problem.)
I found an answer here closely related to my question:
Unwind segue with Navigation back button
Unfortunately I didn't find this in my initial research and only stumbled upon it in the "related" section after I posted my question.
If you don’t want to insert new UIBarButton then you will most probably end up tracking this event with viewWillDisappear or didMoveToParentViewController method.
Here’s the same question, you might find it helpful:
How to trap the back button event

Move Between different Navigation Controllers in Xcode Swift

I want to move from one UINavigatioController to other UINavigatioController. Scenario is First I am at ContactVC and after pressing any cell I will be move to ChatVC. Now when I press the back button on chatVC I want to move back on InboxVC. How can I achieve this scenario.
A UINavigationController is a stack based navigation system. This means when you navigate forwards you simply add another UIViewController to the stack, and when you navigate backwards you pop a UIViewContrller off the stack.
To do what you are proposing you will need to change the underlying UIViewController's that are currently in the navigation stack. You can do this by using func setViewControllers(_ viewControllers: [UIViewController], animated: Bool) on UINavigationController programatically, but you can't do it through a storyboard as thats not how a UINavigationController is intended to be used.
UINavigationController controller keeps a stack of the views (in simple words to understand) when a push a new view controller onto it. So when you press back it essentially pops the last view controller to display its parent controller. This is also normal user behaviour on the iOS platform.
The behaviour you are looking in counter-intuitive to the user since you are breaking their flow of navigation. But if you can a have a strong case for it. You can manually create a back button which can push to the inbox controller to get this sort of behaviour in code or in interface builder.
Another way to achieve this perhaps what you can see in other apps is:
you have a Signup view controller (which might be the form). When the signup is completed.
change the root view controller of the window to Inbox controller and
The same time, push the Chat controller so when your user pushes the back button, they will be taken back to the Inbox.

iOS 10 go back to root in nav stack with parameters

I have an initial ViewController that is a TableView with lots of data. I have a title bar button item that links to another TableView with a summary of the data in the first ViewController. I currently have it so that when you click on a cell, it redirects the user back to the first ViewController and scrolls them to the proper cells. However, it adds the view to the stack and doesn't actually move them back properly. I think I would be able to do this through Unwind segues, but I can't seem to get them to work through cell clicks. Is there a better way that I can achieve this?
When I switch to the other view, I also need to set two variables in the initial controller to tell it what index to scroll to.
1.UNWind Segue :
suppose that we need to programmatically trigger the backward navigation, based on an interaction with something other than the default “back” button on the navigation bar. How would you do it? Yep – you’ve got it: by using an unwind segue.
2.Other way is usual : PopViewController Method
As per your question , you can go with option 2 which is more easy .
As far as Sending data from SecondViewController to FirstViewController you can use delegate pattern to send data back.
Here is few useful post : https://stackoverflow.com/a/25523091/3400991
Feel free to comment. Thanks

Adding Search Button to UINavigationBar : Swift

I have a ViewController1 and ViewController2. ViewController1 has a NavigationController set to it. I have a button (Custom Search Icon) on the NavigationBar. Now I press control+click and drag to ViewController2 and set it to Show. When I press it, it doesn't work. I also tried to drag the button to ViewController1 and set it as an IBAction but that doesn't work either.
What I want accomplished:
I want to create a button on the left side of the NavBar. When clicking it, I want to be add the searchbar and allow the user to search from an array. What is the best way about doing this? The search bar also has the cancel button, and if pressed the user should return back to his previous ViewController. I don't want anyone to the work for me but if you could point me to the right direction, it would be awesome.
I'm assuming by "Custom Search Icon", you mean a UIBarButtonItem in the navigation bar. If that's the case you should be able to segue to a controller using the technique you described, so you could try putting in a new UIBarButtonItem and try again.
But for the problem you're describing I might recommend a slightly different approach using UISearchController. Since it sounds like you're using storyboards, drag in the Search Bar and Search Display Controller from the object library to your controller, just below the navigation bar.
Check out UISearchController documentation:
[https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UISearchController/ ]
And here's a sample project from Apple:
[https://developer.apple.com/library/ios/samplecode/TableSearch_UISearchController/Introduction/Intro.html ]
Good luck!
Delete all the connections from the interface builder first. Clear the codes associated to the button.
Create an IBAction and put a break point to see if its called when u click the button.
If it does get called then push a new ViewController onto it,(ViewController2).
Add your search bar here on the ViewController 2.

What to do about "Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted."

I'm writing an iPhone app using Appcelerator Titanium Mobile. I am hiding and showing the tab group based on what window has focus.
dashWin.addEventListener("focus",function(e) {
if (dashWin.tabGroupVisible == true) {
dashWin.tabGroupVisible=false;
tabGroup.animate({bottom:-50,duration:500});
}
});
The code above hides the tab group when dashWin receives a focus event. However, I see this message in the Titanium console when the event fires while running in the iPhone simulator:
Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.
A Google search turns up one result: Another StackOverflow question that may have a hint as to what's going on.
I got this error when I linked Action Segue or Selection Segue from one view to another view through storyboard and performed the same segue programmatically again, which makes the navigation controller perform the same segue twice.
2 solutions for this case:
Removing the code that pushes the view. Just let storyboard perform the segue for you. This is good for most situations.
Replacing Action Segue or Selection Segue with Manual Section and do - (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender by yourself. You may find this solution useful when you want to customize the behavior of segue according to the sender.
Usually a tab group acts as the root of your app's navigation. When a user taps a tab, that tab's window is focused.
Next, when a user triggers an action that requires a new window appear, it usually appears either modally or on top (in the navigation stack sense) of the current window. In the latter case, tell the current tab to open the new window.
If you set the tabBarHidden property to false (when you create the new window), the tab bar will be hidden for you when the new window is opened by the current tab.
Will this more standard approach work for you?
I had segues that were leading back to my main navigation controller which was causing this. I fixed the problem by setting the main navigation controller back to the top of the stack. Here is the code:
- (void) viewDidAppear:(BOOL)animated
{
[self.navigationController popToRootViewControllerAnimated:NO];
}
Recently, I've faced the same problem. The reason was:
-I was trying to pop view controller twice by mistake.
you can check this crash by setting breakpoints on push and pop View controllers