Unable to retain the selected rows when navigating the UIVIewControllers in swift - swift

I have two uiviewcontrollers for filter purpose, from the first controller using the button i could present the second view controller, from the second view controller i have a tableview and select their rows and passing those row values to the first view controller. It passes the values good, but i again click the same button of the first controller, like wise previous it opens the second controller but the rows i already selected seem unselected.
sample code: for clicking the First controller button for opening the view controller
#objc func empFilterButton(_ sender: Any){
let controller = EmployeeFilterViewController()
self.navigationController?.pushViewController(controller, animated: true)
}
sample code: for selecting the rows and pass those values to first view controller
#objc func applyFilter(_ sender: Any){
let empViewCtrl = EmployeeViewController()
empViewCtrl.deptIdString = deptIdString
empViewCtrl.dateIdString = dateIdString
empViewCtrl.shiftIdString = shiftIdString
empViewCtrl.locationIdString = locationIdString
self.navigationController?.pushViewController(empViewCtrl, animated: true)
}

Related

Segue removes tabBarItem.badgeValue

I have a custom TabBarControllerand on one of the Tab Bar Items I have a round red counter. The first Tab Tab Item presents a summary View Controller. The second Tab Bar Item presents a menu embedded in a Navigation Controller The Tab Bar Counterworks fine across the board, except when I segue back from the summary View Controller. Then the counter disappears. I have tried to load the counter in the customTabBarController:
import UIKit
class CustomTabBarController: UITabBarController {
override func viewDidAppear(_ animated: Bool) {
if tabBarCounter != 0
{
let vc = self.tabBarController?.viewControllers?.last
vc?.tabBarItem.badgeValue = "\(tabBarCounter)"
vc?.tabBarItem.badgeColor = UIColor.red
}
}
}
Screendump of storyboard. (The bottom ViewController is the one I´m having trouble with. Segue highlited)

How to detect tab bar index changes in swift?

I have 3 tabs in a Tab Bar Controller: Main, TabOne, TabTwo(tab index 0,1,2 respectively)
Main view controller has a table view will show all the elements in a an array. In view controllers TabOne and TabTwo, they all have buttons:
#IBAction func mypost(_ sender: Any) {
tabBarController?.selectedIndex = 0}
The array in Main view controller should be changed differently based on the tab index of the previous view controller. Ex.
If the previous view controller is TabTwo, then the table view will only show all the elements of even indices
BUT
If the previous view controller is TabOne, then the table view will only show all the elements of odd indices
How can I do it?
One possible solution is to have your main view controller set itself up as the delegate of the tab bar controller. Then implement the delegate method shouldSelect and look at the tab controller's currently selected index.
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool
let index = tabBarController.selectedIndex
if index == 1 {
// load data appropriate for coming from the 2nd tab
} else index == 2 {
// load data appropriate for coming from the 3rd tab
}
return true
}

How to make textfield with pop out table view

Hi I am looking to make a pop out table view similar to the one below and with selectable cells. Any help would be great thanks!
Here is what I would like to style it after
You will want to make use of UIPopoverPresentationController.
Make a tableviewController with the data you want to be able to select
Make a button that will send the action so you can present the popover
Do something with the item selected from the popover.
The second item above looks something like this (my own code example):
#IBAction func shareAction(_ sender: UIButton) {
let controller = ShareVC()
controller.socialPost = self.item
controller.modalPresentationStyle = .popover
// configure the Popover presentation controller
let popController: UIPopoverPresentationController? = controller.popoverPresentationController
popController?.permittedArrowDirections = [.up, .down]
popController?.delegate = self
// in case we don't have a bar button as reference
popController?.sourceView = sender
popController?.sourceRect = sender.frame
popController?.backgroundColor = .white
self.parentViewController?.present(controller, animated: true, completion: { })
}
In my case the 'ShareVC" is a UITableViewController with item sharing options. Pretty similar to your screenshot as far as layout goes.
This will present that popover from the button that was tapped. Then again, you just need to handle the selection of the items from that ViewController.

Force refresh on another ViewController component with swift

I'm trying to pass a trigger between 2 view controller but i can't find something that works ...
I have the main controller with a NSTableView, source linked, View Base.
main Controller class :
class ViewController: NSViewController {
I have an array defined in Global variables.
on the viewDidLoad i add 2 elements in my array, then i use setDelegate and setDataSource.
It works fine.
myLib.myCoins = fillCoinsTable()
print ("My lib has \(myLib.myCoins.count) objects ")
mainCoinTable.setDelegate(self)
mainCoinTable.setDataSource(self)
I have a segue from the WINDOWS CONTROLLER to my second view Controller.
(It's a ToolBar button). The segue is "Sheet" kind.
This second controller allows me to add an element in my global variable arrays, with a button "Save" .
Code on the second controller
#IBAction func addCoinButton(sender: AnyObject) {
//We add the coin
let newCoin:coin = coin(n: textName.stringValue)
newCoin.Year = Int.init(textYear.stringValue)
myLib.myCoins.append(newCoin)
self.dismissController(self)
}
If i add a button on the main controller, to reloadData on the TableView, it works fine. The third element is added.
but i would like that to be automatic ....
I tried segue, but mine is not between view controller but with the windows controller.
Can you please help ?
Thanks,
Nicolas
I think, you want to add data from oneViewController and without pressing button, you want after navigation or pressing back button, you want to reload automatically tableview to show updated results, right.
If I'm not wrong then, this solution will work for you.
Follow this below steps:
Step 1 :
Add this code in your view controller, from which you want to add data in array or in database, instead of button click.
override func viewDidLoad()
{
super.viewDidLoad()
let backItem = UIBarButtonItem(title: "Back", style: .Plain, target: self, action: "goBack")
self.navigationItem.leftBarButtonItem = backItem
}
func goBack()
{
NSNotificationCenter.defaultCenter().postNotificationName("load", object: nil)
self.navigationController?.popViewControllerAnimated(true)
}
Step 2:
Now its final step.
Now add this below code, to your result view controller, where you want to automatically update the result.
func loadList(notification: NSNotification){
self.TableView.reloadData()
}
override func viewWillAppear(animated: Bool) {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "loadList:",name:"load", object: nil)
}
Now implement this code-stuff in your coding. Hope it works for you.
If you want the table to reload automatically when the View appears add this to viewWillAppear
self.tableView.performSelectorOnMainThread(#selector(UITableView.reloadData), withObject: nil, waitUntilDone: true)

Swift open a new view controller programmatically on top of a map from a button insider a popover and a cell

I have a bit of an odd problem. Note that everything is done programmatically, so please no storyboard suggestions. Here's the setup:
I have a Master-Detail view layout, where the Detail view is a map. The Master-view's table is filled with cells that represent annotation points on the map. When you tap on a cell, it focuses on that annotation on the map and opens a custom annotation view using a popover. Likewise, when you tap the annotation itself, the same popover opens. So far, so good.
Now, inside the popover and inside each cell there is an a button which, when pressed, should open a new view that fills the whole screen. The view would have information specific to its corresponding annotation.
Here is how I am trying to do it:
Class that has the map (MyDetailViewController)
public func openNewViewController(){
self.presentViewController(NewViewController(), animated: true, completion: nil)
}
Function call for cell button and popover button
func btnPressed(sender: AnyObject){
var dvc = MyDetailViewController()
dvc.openNewViewController()
}
When either button gets tapped, I get the same error:
Warning: Attempt to present NewViewController on DetailViewController whose view is not in the window hierarchy!
So, what does it mean that DetailViewController is not in the window hierarchy..? It must be since I see it, no?
Any idea how to fix this issue? Thanks in advance!
Your function openNewViewController() is doing nothing!
See the code
self.presentViewController(openNewViewController()) , animated : true , completion: nil)
is pointing to itself.
Instead try this :
public func openNewViewController(){
var newController = UIViewController() // or any other view you wish to instantiate
self.presentViewController(newController, animated: true, completion: nil)
}