navigating through desired views (while skipping some views) using UINavigationBar back button - ios

I am using a UICollectionView to show a list of images.
1)By clicking on any cell the image and some description about it is displayed in the resulting view controller(using a push segue).
2)When i swipe from the left/right edge(using PanGesture) i need to display the details of the previous/next image in the collection view.
3)But the back button of the navigation bar has to take me back to the collection view and not to the previous displayed details (shown by the PanGesture).
I know how to get 1 and 2 done but don't have a concrete idea to get the 3rd job done.
Any help would be appreciated.

You can find your desired UIViewController in your navigation stack using for loop. Try this. This is in Swift
for (var i = 0; i < self.navigationController?.viewControllers.count; i++) {
if (self.navigationController?.viewControllers[i].isKindOfClass(YourViewController) == true) {
println("is sw \(self.navigationController!.viewControllers[i])")
(self.navigationController!.viewControllers[i] as! YourViewController)
self.navigationController?.popToViewController(self.navigationController!.viewControllers[i] as! YourViewController, animated: true)
break;
}

When you navigate from one view to another (e.g. by showing a detail view of your images one after the other, then all these views are internally piled up as a stack.
Thus, if you want to jump directly to a view somewhere in between this stack (e.g. the collection view), then you can use the Unwind Segue.
In your case it should work like this:
First, in your collection view (i.e. your back button destination) you need to implement a UIStoryboard Segue as follows
#IBAction func myGoBackPoint(segue: UIStoryboardSegue) {
println("Jump directly back here from any other view")
}
Then, in the storyboard of your detail view you ctrl-drag directly to top rightmost Exit marker and choose the previously created back button destination:
In the code of the detail view implement the "go back instruction"
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "My Unwind Segue" {
if let myUnwindSegue = segue.destinationViewController as? MyCollectionViewController {
// prepare for segue
}
}
}
Hope this helps.

Finally after researching a lot i found that using a collection view with each cell taking up the whole screen is the best way to go about solving this issue.
Thankyou

Related

Swift 3: How to make sure that data is set before presenting in next view controller

I am using prepare for segue to set a dictionary variable in another view controller. But the issue is the view controller opens the map without the data. How can I make sure I go to next view controller only after the dictionary variable data has been set.
Here is my code
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showMapSegue"
{
if let mapVC = segue.destination as? MapVC
{
if vendorStoreKeys.count != 0{
mapVC.vendorStore = vendorStoreKeys
}
}
}
}
But the vendor store array in next view controller is still empty when the controller is visible. It sets the data after some time but at that point I am unable to reload that view controller so I need to make sure I don't go to next view controller before the mapVC.vendorStore variable is properly set.
I was able to resolve the issue. Prepare for should have set the data before calling and it was. Issue was I actually forgot to remove the segue in the storyboard which was directly going from the button to the new view controller. So here is what I did
Removed the segue which was going directly from the collection view cell button to the next story board
Created a new segue from the top of the view controller to the next view controller
Named that segue "showMapSegue"
Build and Run. I got the data successfully in the next view controller. Hope it helps.

View controller view getting misplaced when navigating back to the home screen of UITabbarController

I have a tabbarcontroller in my application and have added view controllers with the help of storyboard and segues,
When i am in the second tabbar item I have a table view embedded with a navigation controller filled with usernames and on top I have a hamburger icon, I select a particular item from the table list present in the hamburger icon and navigate to the details view controller I have used segues and given below is the code for the same
self.performSegueWithIdentifier("MY_DETAIL_SEGUE_NAME", sender: nil)
MY_DETAIL_SEGUE_NAME is actually a custom push segue of the SWRevealViewControllerSeguePushController, the push is working fine and i am able to navigate to the details screen.
The Problem: Since I am in the second tabbar item on the hit of back button i need to show the inital view / home view controller of the second tabbar item which is the friend view list, so on the back button i have written the below code in the detail screen
#IBAction func moveBackToController(sender: AnyObject) {
self.performSegueWithIdentifier("showFriendListPage", sender: nil)
}
showFriendListPage: This is a custom segue to the firendlist page using the SWRevealViewControllerSeguePushController and is pointed to the tabbarcontroller
To highlight the second tabbar item i have used the below code written in the detail screen
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
let objParentTabbar = segue.destinationViewController as! ParentTabbarController
if segue.identifier == "showFriendListPage" {
objParentTabbar.selectedIndex = 1
}
}
What happening here is when i navigate back to the home screen of the second tabbar item my view is distorted i.e. the UI elements are not placed in a correct order which they were when i first went to the second tabbar
Note: All my view controllers are embedded inside the navigation controller so the structure is
TabbarController----->NavigationController------>ViewController
Please suggest where I am going wrong and what needs to be done to resolve this, my only struggle is the view getting distorted and i dont really know why is this happening.
Call the view your controller via code in your application should do the trick
objParentTabbar.selectedViewController?.viewWillAppear(true)

Side menu with dynamic table cell will navigate page but with navigation controller coming as nil

I have create sidemenu using this.
But the difference is only i have used dynamic UITableViewCell instead of static.
Now i face navigation issue while click on side menu item.
I have used navigation code like:
dispatch_async(dispatch_get_main_queue()) {
let propertyListing = self.storyboard?.instantiateViewControllerWithIdentifier("PropertyListing") as! PropertyListingController
self.showViewController(propertyListing as UIViewController, sender: propertyListing )
}
It will navigate the page but navigation controller will be nil after that.
What can be the issue ? Any suggestion will be most appreciate.
Rather than using a "show", you need to push the new view controller.
self.navigationController.pushViewController(propertyListing, animated: true)

Back button doesn't appear in navigation bar

I'm having trouble understanding the way view controllers are presented and how to navigate between them in swift. I have a contact list, when you click on one contact it opens the related conversation, but it doesn't show any back button to the conversation list. My storyboard is :
Contact List -> Navigation Controller -> Messages Controller
There is no navigation controller before contact list because it messes up with a sliding menu.
Here is my code:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ShowConversationFromSelectedContact" {
let nav = segue.destinationViewController as! UINavigationController
let cdtvc = nav.topViewController as! MessageViewController
cdtvc.currentConversation = openConversation
}
}
More generally if you could explain or have any good ressource about the way stack of controllers work and how to navigate between them it would be great, I read a bunch articles but it's still unclear.
Re-arrange it so it is Nav Controller -> Contact list -> Message controller, and use push segues (if you're using swift 1.2+, they're called Show segues).
Inside prepareForSegue: you don't need to do anything else besides setting the contact id (or whatever data you need to pass) to the message controller.
Edit:
For a sliding menu, I suggest the following navigation model:
The Storyboard ViewControllers hierarchy goes
Nav Controller --root segue--> Contact List VC --show segue--> Message VC
Sliding menus are usually to present Top Level Views. Your Contact List VC is a top level view. Since it is also the root VC, you can use self.navigationItem.leftBarButtonItem to assign a button to show your sliding menu.

Multiple unwind in the same button - Swift

I would like to know if it is possible to assign two different unwind method at the same button. For example:
I have this views navigations:
Home->A->B
Home->C->B
A and C views navigates to B, but then I want to return to previous views using the same B view and controller.
It is possible?
I have been thinking about write assign unwind method to the button programmatically depending what view comes.
Thanks in advance
I'm sorry about my english, is not good.
Here's a Swift solution that worked well for me. The code below only works if you hookup your segues correctly in the storyboard and in code. Checkout this page for great explanations on setting up unwind segues.
In summary:
You're accessing the same view from multiple other views. So, when you segue to a view, you can pass the source view controller (the view that you're currently in) to a property in the view that you're going to.
In your view that you will unwind out of, you can check the property holding the info (the class) on where you came from, and then perform a segue based on what view it is.
The code: (using ex: Home -> A -> B or... Home -> C -> B)
Note: B is the view that will unwind to multiple different views.
In A or C: (code works the same way in both views)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "segueIdentifierInViewAthatGoesToViewB" {
let controller:B = segue.destinationViewController as! B
//the code below passes our current view controller class to a property in view B.
//So, view B will know what view we came from.
//In our example, we're coming from view A or C
controller.viewControllerNavigatedFrom = segue.sourceViewController
}
}
In B:
//setup an IBAction that will check the source view controller (that we passed when we segued to this view) and perform a segue based on where we came from. You can hook this up to a button or anything you want really.
//isKindOfClass(A) - "A" would be the name of your class
//setup a property to receive the view controller class where we are coming from
var viewControllerNavigatedFrom:AnyObject?
#IBAction func myButtonPressed(sender: AnyObject) {
if self.viewControllerNavigatedFrom!.isKindOfClass(A) {
//Unwind to view A
performSegueWithIdentifier("unwindFromBbackToA", sender: sender)
}
else if self.viewControllerNavigatedFrom!.isKindOfClass(C) {
//Unwind to view C
performSegueWithIdentifier("unwindFromBbackToC", sender: sender)
}
}
Although, question isn't very clear. But what I could understand is that you want to navigate back to the previous view i.e. B>C or B>A depending upon where user came from.
If so, then check the UINavigationController. It keeps track of the navigation history and automatically adds a back button. Kind of like the back button in our browsers.
Here is a tutorial, although a bit old: Link

Resources