How to add a view to UINavigationController that is visible above any child view - ios

I'm creating an app which shows data in a UITableView. This data also contains location information, so I want to add a MapView to display this data. When a user taps a UITableViewCel, I need to segue to another UITableView to show the detailed data. I still want to show the MapView with the same data above this detailed data, and I still want the NavigationBar to update, i.e. the back buttons returns the user to the first UITableView.
In image form to make it easier to understand
I was thinking of making a subclass of a UINavigationController and putting the container underneath the MapView, but I don't really know where to go from there.

Have VC0 embedded in a navigation controller or subclass UINavigationController. Create an Info ViewController and push that on the navigation controller.
In your particular case you'll want to use the didSelectRowAt method from UITableViewDelegate.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// use indexPath.section and indexPath.row to use your data
let infoVC = yourInfoViewController()
infoVC.title = "Info Cell Name"
navigationController?.pushViewController(infoVC, animated: true)
}
Edit after first comment:
I'm not sure what the best way of doing this is but one way you can do it is:
Modify your table view based on what is being shown, ie: first set of data or cell detail data
Update the navigation bar's title and left bar buttons per set of data you're displaying
One of those buttons will only appear in the cell detail data set and it sets your switch back to displaying the first set of data

Related

Tab Bar disappears on segue

What I want to achieve:
I have a "Login" ViewController which segues to a Tab Bar Controller.
My Tab Bar Controller goes to a Navigation Controller and then "Home" ViewController with a Table View. This view is my "Home/Welcome" page. I want there to be a Tab Bar at the bottom as well as a static table which I can edit with Prototype Cells. Each cell goes to a different ViewController.
Then, I want to be able to tap each Cell (I have "Info" setup for now) and it segue to the corresponding ViewController. For now, the "Info" cell should go to the second Navigation Controller and then "Information" ViewController.
From there, I want another static table which I can edit with Prototype Cells. Each cell will go to a seperate stack of ViewControllers.
My problem:
When I segue from the "Info" cell in the "Home" ViewController, the Information tab bar "pushes" up, and the Tab Bar disappears. I've experimented with other segue forms but none of them maintain the Tab Bar.
I want it so you can use either the Tab Bar to navigate to the Info ViewController or the cell on the Home/Welcome ViewController to navigate to the Info page.
I have no code at all at this point, I am just using these segues.
Does anyone have any idea how I can achieve what I want to achieve, and if so, provide as much sample code as I'm extremely new to Swift.
Any help is appreciated! Thanks :)
Don't add segue to open the "Information" ViewController which is part of UITabBarController.
You must have used didSelectRowAt for cell selection in tableview. Just add one line in it as it is :
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true) {
self.tabBarController?.selectedIndex = 1
}
This will change the tab and open the "Information" ViewController.
If you want to load the "Information" ViewController with data relevant to cell selection you can go with Protocols or NotificationCentre which can pass data to "Information" ViewController from Homeviewcontroller on cell selection.

Xcode: TableView cell to create ViewController

I've recently started using Xcode and Beginner to creating apps. I've come across something I'd like to implement but research hasn't been quite clear/complicated to understand.
I have a TableView with Days of Week and also a segmentedControl that duplicates these days 3 times. I'm wondering how I'm able to get user click to take me to an alternative ViewController depending on which day it is and what segment of the segmentedController is selected without having to create 21 viewcontrollers in the storyboard.
I've used a ViewController and made an outlet to a tableView for this setup.
in you tableview delegate method -> didSelectedRowAtIndexPath
you can simply write
let vc = UIViewController()
/*
you can add informations here
*/
present(vc, animated: true)
and inside this method you can use the indexPath to get the cell you've clicked
let cell = tableView.cellForRowAt(indexPath)
now you can get the information in this cell
You can have one viewcontroller which is basically a recipe for the viewcontroller you want to go to.
For example you want to show a viewcontroller with a label that states a combination of the number from the segmented control and the date which the user tapped on.
If you now create a ViewController which has a label in storyboard, the text on the label doesnt matter, you can use code to change the text to anything you would like.
In the tableviewcontroller you can use the didSelect delegate method of your tableview to present the viewcontroller and set the label to the values of the segmented control and the cell which is tapped.
I think you just need some more information about iOS and programming in general, therefore I suggest you watch the following iTunes U course:
https://itunes.apple.com/us/course/developing-ios-10-apps-with-swift/id1198467120
It has everything you need to know
Create a detail view controller with two variables (Ex - dayName and selectedSegmentIndex). And in your current viewController "tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)" push detailsViewController with these details like this
let objViewController: DetailsViewController? = UIStoryboard.mainStoryboard().instantiateViewController(withIdentifier: "DetailsViewController") as? DetailsViewController
objViewController?.dayName = dayName
objViewController?.selectedSegment = segmentControl.selectedSegmentIndex
navigationController?.pushViewController(objViewController ?? UIViewController(), animated: true)

Open URL in different ViewController when tableView cell is pressed

I am using SWRevealViewController for a slide-out menu in my app. The main View Controller contains a WKWebView to open URLs. When the slide-out button is pressed, a table view Controller appears.
I want to have the name of a website displayed in a Table View Cell in the Table View Controller in the slide-out menu and open that website in the WKWebView in the main View Controller when the cell is pressed.
This may seem like an easy question, but I can't seem to find anything about it online.
Thanks in advance for your help.
As far as I remember the implementation of SWRevealViewController, it uses the static table view.
If you are okay with the static table view that will have your links then you may just create a delegate method that will be called based on the cell that was chosen on the Table View Controller. This delegate will notify your ViewController and say which link to open.
Hope it's clear. Let me know if you need more details.
In SideMenuViewController (view controller in which you have table with links)
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let mainViewController = MainViewController()
mainViewController.link = linksArray[indexPath.row]
self.revealViewController().setFrontViewController(mainViewController, animated: true)
}
init new main view controller that have WKWebView
pass link as a property
set that view controller as new front view controller

How can I get a Table View to register taps when each cell has an Image View covering it?

I've been trying to create table view cells each with a UIImageView serving as a background for them. However, when I tap on each cell the table view will not register the click and will not transition to the view controller I have hooked up, even while I'm using the didSelectRowAtIndexPath function.
I realize it's probably an issue with the ImageView obstructing the table views ability to register the cell tap. The cells will transition however when I drag my finger/mouse on it from left to right, just not on taps. I tried to use a Tap Gesture Recognizer on the Image View however it only worked for the cell at the very top and not all of them. How do I tackle this issue?
Here is an image of the table view cells so you have an idea of what I'm working with: http://imgur.com/a/Ku4uD. Thank you!
If you uncheck User Interaction Enabled on your Image View, the problem should be solved. When running into a problem always check the user interaction of the most child view and work your way up.
One trick I have also learned is to create a subclass of a child and override touchesShouldCancel like so:
override func touchesShouldCancel(in view: UIView) -> Bool {
print("touchesShouldCancel")
//Run extra code that you want when the user selects this view
//then still retrieve the tap by its parent.
return false
}
I am unsure of exactly what your problem is, but I would delete whatever segue that you have, add a new one by dragging from the yellow circle on the left side of the center portion of the top of your tableView ViewController inside the storyboard, to the viewController that you desire it to segue to. Give the segue an appropriate identifier, and then inside your tableView class under tableView didSelectRow add performSegue(withIdentifier: "ChosenIdentifier", sender: indexPath)
Then in prepare forSegue add in:
if let vc = sender.destination as? TheViewControllerYouAreSegueingTo {
if let indexPath = sender as? IndexPath {
vc.variableIdentifyingWhatCellWasClicked = indexPath.row
}
}
with whatever adjustment is needed to meet your specific needs.

Swift UITableViewController Reduce Loading Time

I'm currently creating an iOS app in Swift 1.2 with a master-detail UITableViewController setup so that MasterTableViewController has cells that push to DetailTableViewController.
DetailTableViewController has fairly complex cells that pull data from a web client to generate a chart, similar to in Apple's Health app. It usually takes about 5-10 seconds for all the data to download.
As a result, there is a 5-10 second delay when a cell in MasterTableViewController is tapped before DetailTableViewController is shown.
I would ideally like to push immediately from the MasterTableViewController to the DetailTableViewController and then display an activity indicator on the DetailTableViewController page that is dismissed when all the data is downloaded.
Could someone point me in a good direction to accomplish this? Thank you! :)
My code is below for pushing from master to detail. Fairly basic. I feel like if I change something in the viewDidLoad or cellForRowAtIndexPath method, I could get this done fairly easily.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: false)
let detailVC = DetailTableViewController()
detailVC.navigationItem.title = "Your Charts"
self.navigationController?.pushViewController(detailVC, animated: true)
}
On your detail view controller you can create a UIActivityIndicatorView variable.
In viewDidAppear you can call the activity indicator, bring it to front and start animating it.
The create a function that populates an array that will store the data your cells will use and re work your cellForRowAtIndexPath to use it. (Instead of fetching the data in each cell).
Then in your function to fetch data, right after (or before) you update your table tell the activity indicator to stop animating.

Resources