i'm using here switch condition to navigate to another page using picker view and i already mentioned it in my controller but it's just navigate to one of the pages when i press on the second choice in picker view it takes me to the same page of the choice one.
i create a variable calling optionSelector and i gave him value 0 and i made the switch condition but still working on one page.
#IBAction func donePressed(_ sender: Any) {
mainPV.isHidden = true
doneBtn.isHidden = true
optionV.isHidden = true
switch optionSelector{
case 0:
FiltersController.instance.showAreaFilter(nc: self.navigationController!)
case 1:
FiltersController.instance.showTrainStations(nc: self.navigationController!)
case 2:
FiltersController.instance.showMapFilter(nc: self.navigationController!)
default:
FiltersController.instance.showResidintialFilter(nc: self.navigationController!)
}
}
You might not be updating the value of optionSelector on pickerView scroll.
Change optionSelector value on pickerView delegate method as:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
// put your logic here.
self.optionSelector = row
}
you might not have the correct value in optionSelector
You can use the selected value of the picker view by:
// Assuming you only have 1 component
// By default you only have 1 component
pickerView.selectedRowInComponent(0)
So effectively your done method will be:
#IBAction func donePressed(_ sender: Any) {
mainPV.isHidden = true
doneBtn.isHidden = true
optionV.isHidden = true
switch pickerView.selectedRowInComponent(0) {
case 0:
FiltersController.instance.showAreaFilter(nc: self.navigationController!)
break
case 1:
FiltersController.instance.showTrainStations(nc: self.navigationController!)
break
case 2:
FiltersController.instance.showMapFilter(nc: self.navigationController!)
break
default:
FiltersController.instance.showResidintialFilter(nc: self.navigationController!)
}
}
Related
I have a view controller with a segmented control(only two segments: Songs and Playlists) and a bar button item. When the songs segment is selected, I want the bar button item to perform action1, and when the playlists segment is selected, I want the bar button item to perform action2.
In an attempt to do this I created this function which I declared in the viewDidLoad and in the viewDidAppear:
func settearButton() {
let indice = segmentFiltroMusica.selectedSegmentIndex
switch indice {
case 0: //Canciones
btnAgregarMusica.action = #selector(irAAgregarCancionesVC)
btnAgregarMusica.target = self
case 1: // Playlists
btnAgregarMusica.action = #selector(irAAgregarPlaylistsVC)
btnAgregarMusica.target = self
default: print("")
}
}
However, the bar button item is only performing action1, no matter what segment is selected. How could I fix this?
I don't think you actually want to change the action of the button. Handle the button tap, check the state of your segment control, then call the appropriate method.
func settearButton() {
let index = segmentFiltroMusica.selectedSegmentIndex
switch index {
case 0:
irAagregarCancionesVC()
case 1:
irAggregarPlaylistsVC()
default:
break
}
}
First you should put your function in the class and not the viewDidLoad() or viewDidAppear(). You can make it private since you seem to use it only localy.
If you are using storyboards with a ViewController you probably know that you can drag actions and outlets of your segment and button onto your viewController Class.
Here is how I would do this, by using a Boolean value and change this to select between my actions:
private var myChoice: Bool = false
#IBOutlet weak var segment: NSSegmentedControl!
#IBAction func segmentAction(_ sender: Any) {
let index: Int = self.segment.selectedSegment
switch index {
case 0:
myChoice = true
case 1:
myChoice = false
default:
fatalError("This segment does not exist!")
}
}
#IBAction func buttonAction(_ sender: Any) {
if myChoice {
// Do case one
} else {
// Do case two
}
}
If you have more elements in the segment to choose from, take an enum instead of the Boolean. I kept your switch which is good if you will add an enum later, else you can just replace the content of the segmentAction() with:
myChoice = self.segment.selectedSegment == 0
My Scenario, I am loading JSON data into tableview here I am maintaining two segment controller button for single tableview with search-bar. First segment button click to search I can get search result well and if I click segment button two there is also showing same search result. So, when I click segment one to two I need to clear search result and load normal data. Same scenario working well when I click close button within searchBar.
My Code
#IBAction func switchTableviewAction(_ sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0:
print(“one”)
self.searchResultClear()
currentTableView = sender.selectedSegmentIndex
self.tableView.reloadData()
case 1:
print(“two”)
self.searchResultClear()
currentTableView = sender.selectedSegmentIndex
self.tableView.reloadData()
default:
break;
}
}
// MARK: Search Result Clear working but not clearing result
func searchResultClear() {
//self.searchBar.text = ""
//self.searchBar.showsCancelButton = false
//self.filteredLanguages.removeAll()
//self.tableView.reloadData()
}
Along with clearing search bar, you also need to remove the filtering from data source. By data source, I mean the array of objects you are showing in table view. You must be using a filter function with the filter text. If you want to clear the search bar, you also need to restore the original JSON data (the unfiltered one) and then call reloadData
You just need to call the searchBar textDidChange method with empty text when segment control selection is changed
#IBAction func switchTableviewAction(_ sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0:
self.searchBar(self.searchBar, textDidChange: "")
case 1:
self.searchBar(self.searchBar, textDidChange: "")
default:
break;
}
}
I'm using a third party library for my UISegmentedControl. The pages are initialised as following:
func carbonTabSwipeNavigation(carbonTabSwipeNavigation: CarbonTabSwipeNavigation, viewControllerAtIndex index: UInt) -> UIViewController {
switch index {
case 0:
return self.storyboard!.instantiateViewControllerWithIdentifier("FolderOverviewController") as! FolderOverviewController
case 1:
return self.storyboard!.instantiateViewControllerWithIdentifier("TopFoldersTab") as! TopFoldersTab
case 2:
return self.storyboard!.instantiateViewControllerWithIdentifier("CategoriesFolderTab") as! CategoriesFolderTab
default:
return self.storyboard!.instantiateViewControllerWithIdentifier("CategoriesFolderTab") as! CategoriesFolderTab
}
}
When I press the third segment, the user can go further down to see more details (via subviews on the same page). I would like the page to reload, every time I select the third segment again. (go back to the original CategoriesFolderTab page) . Currently I'm doing this with a ViewDidLoad(), but this is slowing down the app when you do it multiple times.
Is there a more correct way to do this? Thanks in advance
I think that calling viewDidLoad() is not the right approach for achieving this, instead, implement a new function that should contains code responsible for loading data in the UI components, for example:
override func viewDidLoad() {
super.viewDidLoad()
reloadUI()
}
func reloadUI() {
// filling UI components with desired data, such as:
// myLabel.text = "Hello World"
}
And somewhere in your code (where you want to reload), instead of calling viewDidLoad(), you should call reloadUI() method.
Hope this helped.
try this
Let folderOverVC = self.storyboard!.instantiateViewControllerWithIdentifier("FolderOverviewController") as! FolderOverviewController
Let topFoldersTab = self.storyboard!.instantiateViewControllerWithIdentifier("TopFoldersTab") as! TopFoldersTab
Let categoriesFolderTab = self.storyboard!.instantiateViewControllerWithIdentifier("CategoriesFolderTab") as! CategoriesFolderTab
func carbonTabSwipeNavigation(carbonTabSwipeNavigation: CarbonTabSwipeNavigation, viewControllerAtIndex index: UInt) -> UIViewController {
switch index {
case 0:
return folderOverVC
case 1:
return topFoldersTab
case 2:
return categoriesFolderTab
default:
return categoriesFolderTab
}
}
this initializes the view controllers once, so their respective viewDidLoad methods get called once initialized, not every time you tap a tab item
i want to make 2 action for a button like that.
selected and deselected action for 1 button.
#IBAction func btntouch(sender: UIButton) {
if firsttouch
{
print bla bla
change button to selected style. maybe background color.
}
else
{
}
}
how can i do that?
In case you need to split two button statuses - like ON and OFF, try this:
var buttonSwitched : Bool = false
#IBAction func btntouch(sender: UIButton) {
//this line toggle your button status variable
//if true, it goes to false, and vice versa
self.buttonSwitched = !self.buttonSwitched
if self.buttonSwitched
{
//your UI styling
}
else
{
//your opposite UI styling
}
}
Create 2 IBActions:
#IBAction func touchDown(_ sender: AnyObject) {
print("down")
}
#IBAction func touchUp(_ sender: AnyObject) {
print("up")
}
When connecting the first one, make sure the event is set to touchDown. For the second one, make sure it is set to touchUpInside
Yes, you can. Depending on your requirements, you could store the current state of the button in the view controller or in the model.
If the visual change caused by the first touch needs to be persisted across opening and closing of your view controller, store the value indicating the change in the model; if you need to reset the visuals when the view controller shows, store the value in the view controller itself:
var firstTouch = true
#IBAction func btntouch(sender: UIButton) {
if firstTouch {
firstTouch = false
...
} else {
...
}
}
I've been trying this for hours with no luck. I have a UICollectionView collectionView. The collection view is basically a list with the last cell always being a cell with a big plus sign to add another item. I've enabled reordering with the following. What I'd like for it to do is when I start the interactive movement, the plus sign cell goes away, and then when the user is done editing, it appears again. This is a basic version of the code I have:
func handleLongGesture(gesture: UILongPressGestureRecognizer) {
switch(gesture.state) {
case UIGestureRecognizerState.Began:
...
self.collectionView.beginInteractiveMovementForItemAtIndexPath(selectedIndexPath)
removeAddCell()
case UIGestureRecognizerState.Changed:
case UIGestureRecognizerState.Ended:
...
collectionView.endInteractiveMovement()
replaceAddCell()
default:
collectionView.cancelInteractiveMovement()
}
}
func removeAddCell(){
print("Reloading data - removing add cell")
data_source.popLast()
self.collectionView.reloadData()
}
func replaceAddCell(){
print("Reloading data - replacing add cell")
data_source.append("ADD BUTTON")
self.collectionView.reloadData()
}
It's very rough pseudocode, but I can't even get the simplest version of this to work. With the code I have, it gives me the dreaded "Fatal error: unexpectedly found nil while unwrapping an Optional values" on the line where I reference the UICollectionViewCell after removing the items from the data source.
If anyone who has done something like this could share their approach I'd really appreciate it! Thank you!
-Bryce
You can do something like this:
func longPressed(sender: UILongPressGestureRecognizer) {
let indexPath = NSIndexPath(forItem: items.count - 1, inSection: 0)
let cell = collectionView.cellForItemAtIndexPath(indexPath) as! YourCollectionViewCell
switch sender.state {
case .Began:
UIView.animateWithDuration(0.3, animations: {
cell.contentView.alpha = 0
})
case .Ended:
UIView.animateWithDuration(0.3, animations: {
cell.contentView.alpha = 1
})
default: break
}
}
this way it gradually disappears instead of abruptly.
I've done something like this. The data source for the collection view tracks a BOOL to determine whether or not to show the Add Item Cell. And call insertItemsAtIndexPaths: and deleteItemsAtIndexPaths: to animate the Add Item Cell appearing and disappearing. I actually use a Edit button to toggle the modes. But you can adapt this code to use your gesture recognizer.
basic code:
self.editing = !self.editing; // toggle editing mode, BOOL that collection view data source uses
NSIndexPath *indexPath = [self indexPathForAddItemCell];
if (!self.editing) { // editing mode over, show add item cell
if (indexPath) {
[self.collectionView insertItemsAtIndexPaths:#[indexPath]];
}
}
else { // editing mode started, delete add item cell
if (indexPath) {
[self.collectionView deleteItemsAtIndexPaths:#[indexPath]];
}
}