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

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)

Related

How to segue from tab bar view controller to another view which is a child of a navigation controller

I laid out my home screen (Summary screen) as shown on the screenshot below:
I added the "+" button on the UITabBarController subclass. This button will segue to a view controller and show it modally. To give you a bit of context on how I've structured my storyboards, please refer to the screenshot below:
This modal view controller has a form that the user needs to fill in, and once they're done, there's a 'Done' button which will 1) dismiss the modal, 2) take them back to the root view which is either the 'Summary' tab or the 'Details' tab then 3) take them to a list view showing the most recent data they've entered on a list (destination view – highlighted in red).
Now, doing the segue when the user taps the "+" button is simple enough. With the following code:"
menuButton.addTarget(self, action: #selector(menuButtonAction(sender:)), for: .touchUpInside)
then
func addNewExpense(action: UIAlertAction){
self.performSegue(withIdentifier: "segue_formModal", sender: self)
}
On the modal I've setup a protocol which will then send the form data back to the tab view controller.
extension XtabBarViewController: NewInfoDelegate{
func newInfoSubmitted(formData: FormData) {
performSegue(withIdentifier: "segue_ListingPage", sender: self)
}
}
But it seems that the perform segue is not working. It says the segue does not exist. "segue_ListingPage" is that segue that connects the Summary view and the destination view.
How can I segue from the tab bar view controller to the destination view? Any help is appreciated.
Thanks in advance!
You can just make the navigation view controller the entry point.
I figured it out on my own. For the benefit of those who run into the same issue here's my solution.
I simply added the code from the tab bar view controller the code:
let childVC = children[0].children[0]
childVC.performSegue(withIdentifier: "segue_TXListPage", sender: self)
So what I did here is call the performSegue from the view controller that segues to the destination controller.

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.

Invoking a segue associated with the Detail View from the Main View

I have a split view controller and the main view has a "Note" button on a navigation bar which should push a view(Note Gallery ViewController) that contains a list of saved notes onto Detail View. In the following screenshot, "Categories" is the Master View and "List of Events" is the Detail View. As you can see, there is a segue between the "List of Events" and "Note Gallery".
What I would like to do is pushing the "Note Gallery" as a Detail view when "Note" button on the Main View is tapped. So far, I have tried using delegate/protocal approach where protocol is defined inside the Master View and it is implemented inside the Detail View. If I call self.performSegue to push "Note Gallery" view, "Note Gallery" view does not show navigation bar (Even if I force it to show with setNavigationBarHidden with false value). If I call performSegue from the navigation controller (self.navigationController?.performSegue), I get identifier not found exception. I don't want to move Note button to the Detail view since I want to make Note button accessible from the Main View. How can I resolve this issu, and here's an excerpt of my code :
/* Master View */
// Define delegate for updating children views
protocol ChildViewDelegate: class {
func pushInNoteView()
}
class MasterViewController: UITableViewController, NSFetchedResultsControllerDelegate {
weak var delegate: ChildViewDelegate?
// Handles tapping on Note button
#IBAction func noteTapped(_ sender: UIBarButtonItem) {
// Push Note View onto any active detailed view
self.delegate?.pushInNoteView()
}
}
/* Detail View */
extension CategoryDetailTableViewController: ChildViewDelegate {
func pushInNoteView() {
// This approach does not show navigation bar
self.performSegue(withIdentifier: "showNoteView", sender: self)
// This approach gives identifier not found exception
self.navigationController?.performSegue(withIdentifier: "showNoteView", sender: self)
}
}
I assume the problem is that you did not give identifier to your segue in main.storyboard.
Here is how to do it.
In your main.storyboard select your segue as shown in image and assign it an identifier in identity inspector as shown in top right side of the image.
Now create navigation controller and make "root view controller" relationship with your Master View. Then use performSegue(withIdentifier: "segue", sender: nil) to move to next view controller.
Hope that I've solved your problem!
Here are the mistakes I have made :
The reason why I'm getting identifier not defined error is because the segue is not connected between the navigation controller and the "Note Gallery" view. It's connected between the "Note Gallery" view and the "Category Detail" view which in turn connected to the navigation controller. Therefore, the identifier is not defined for the navigation controller.
The reason why navigation bar is hidden is because I'm not pushing "Note Gallery" view onto the navigation controller. Instead, I was just calling prepare segue.
Solution :
It's fairly simple. I just push the "Note Gallery" view on the Navigation Controller just like this :
if let noteVC = storyboard?.instantiateViewController(withIdentifier: "NoteGalleryView") {
self.navigationController?.pushViewController(noteVC, animated: true)
}

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

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

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.

Resources