How do I make a button add one view at a time? - swift

I'm practicing creating an app that has 3 views. Each view has a button to hide itself. Now, I added a button outside the views to re-add them after I hide them, but, when i remove the views and I press it, it shows me all of the three views. I want it to add one view at a time (without removing the other views). What can I do?
This is my code, I was even thinking about adding multiple add buttons but it wouldn't be clear.
#IBAction func addViewButton(_ sender: Any) {
view1.isHidden = false
view2.isHidden = false
view3.isHidden = false
}

Declare a variable like currentVisibleViews in your ViewController, and lets say at first there is only first one is visible so we start off the number as 1, and also add your views to an array to control easier:
var currentVisibleViews = 1
var viewArray = [UIView]()
In viewDidLoad, add your views to viewArray:
view1.isHidden = false
view2.isHidden = true
view3.isHidden = true
viewArray.append(view1)
viewArray.append(view2)
viewArray.append(view3)
Then in your button:
#IBAction func addViewButton(_ sender: Any) {
if currentVisibleViews > 2 {
viewArray.forEach { (view) in
view.isHidden = true
}
currentVisibleViews = 0
} else {
viewArray[currentVisibleViews].isHidden = false
currentVisibleViews += 1
}
}
Code above works as this:
First all views are visible
First tap will hide all views
Then each tap will make a view visible
Go to step 2 (once all views are visible)
By this way, you can only have one function to control all your views, it doesnt have to be IBAction anymore, it can be just a method of your ViewController

Related

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.

Swapping bar button items

I'd like to swap a bar button item, depending on a UISegmentControl which I thought would be fairly straight forward.
My initial plan was:
Create an IBAction for when the segment changes
Work out which segment we are on
Hide the appropriate button
The code was something like this
#IBAction func segmentChanged(sender: UISegmentedControl) {
UIView.animateWithDuration(0.25) { () -> Void in
if(sender.selectedSegmentIndex == 0) {
barButtonA.hidden = true
barButtonB.hidden = false
} else {
barButtonA.hidden = false
barButtonB.hidden = true
}
}
}
That hid the right button as expected, but oddly, the space where the button was still remained, so it looked really strange.
Next I saw that in the interface builder there is a parent container (UIBarButtonItems) for each of the buttons. I'm not sure why they're needed, but I hooked them up via IBOutlets so I could hide/show them in the function, but that didn't work either as they don't have a hidden property.
Finally, I tried to use those new outlets with following code:
#IBAction func segmentChanged(sender: UISegmentedControl) {
UIView.animateWithDuration(0.25) { () -> Void in
if(sender.selectedSegmentIndex == 0) {
self.navigationItem.rightBarButtonItem = self.barButtonItemA
} else {
self.navigationItem.rightBarButtonItem = self.barButtonItemB
}
}
}
But no luck! Here, it just seems to animate swapping the two button positions/order, rather than actually removing either one.
What am I doing wrong? Thanks!
Yo should combine the two function you have.
So swap them and change the hidden state.
Update:
Did you tried to first delete the navigation.barbutton.
self.navigationItem.rightBarButtonItem = nil

Hide add button

I am trying to have a button, so the user can hide the add banner if they want to, this works but when the add banner is not displaying I want the button to also be hidden. So I added the following code, but as soon as I did so, the button stopped working. Any ideas as to why?
thank you!
override func viewDidLoad() {
super.viewDidLoad()
if Banner.hidden == false{
hideAdd.hidden = false
} else {
hideAdd.hidden = true
}
The following is the code I used to hide the banner
#IBAction func HideAddButton(sender: AnyObject) {
Banner.hidden = true
}
The easiest way to hide and unhide objects on a MainStoryboard is to change the alpha to 0 in the attributes inspector. Then when you want it to appear you can just change the alpha in your code to 1 when you want it to appear in ViewController.swift. Or Vice Versa.

Swift hide just one button on tabbar

I have seen lots of code to hide the whole tabbar. I am using tab bar controller in storyboard.
If I had third and fourth buttons how could I hide just the second but still have 1, 3 and 4 buttons shown?
Well just hide the button itself.
button2.hidden = true
You will need to create an outlet.
#IBOutlet var button2 : UIButton!
And link it to the button in Interface Builder
Ok to hide the button the following can be done. I used this code in a table view to add the editing function and change the title of the button based on the the click. But I modified it for this post just to show that when I clicked on the button again it would make it disappear.
var condition: Bool = true
#IBAction func buttonEdit(sender: UIBarButtonItem) {
if(condition == true) {
tableView.editing = true
sender.title = "Done"
condition = false
} else {
tableView.editing = false
sender.title = "" // This clears the title
sender.enabled = false // This disables the button
sender.image = UIImage() // This would clear the image
condition = true
}
}

swipe gesture and uisegmented control

In the notification center (iOS 7) it is possible to swipe between the "Today", "All", and "Missed" options of (what I presume is ) a segmented control. I would like to do this in my code but am unsure how to contiunue. I have a uisegmented control and a uitableviiew as my view.
I accomplished this by following the respective steps:
Add 2 swipe gesture recognisers(one to swipe left and the other to
swipe right)
For each of them on the connections inspector reference the outlet
collection as your main view.
Selecting actions when swiping as follows:
viewSelector is the UISegmentedControl and firstView, secondView and
thirdView are 3 views used to select which one is being shown.
Code:
#IBAction func indexChanged(sender: UISegmentedControl) {
switch sender.selectedSegmentIndex
{
case 0:
firstView.hidden = false
secondView.hidden = true
thirdView.hidden = true
case 1:
firstView.hidden = true
secondView.hidden = false
thirdView.hidden = true
case 2:
firstView.hidden = true
secondView.hidden = true
thirdView.hidden = false
default:
break;
}
}
#IBAction func swipeLeft(sender: UISwipeGestureRecognizer) {
viewSelector.selectedSegmentIndex = (viewSelector.selectedSegmentIndex + 1) % viewSelector.numberOfSegments
indexChanged(viewSelector)
}
#IBAction func swipeRight(sender: UISwipeGestureRecognizer) {
viewSelector.selectedSegmentIndex = (viewSelector.selectedSegmentIndex - 1) % viewSelector.numberOfSegments
if(viewSelector.selectedSegmentIndex == -1){
viewSelector.selectedSegmentIndex = viewSelector.numberOfSegments-1
}
println(viewSelector.selectedSegmentIndex)
indexChanged(viewSelector)
}
This could be accomplished using a UIScrollView with paging enabled, and when the user scrolls through the pages, the UISegmentedControl's selectedSegmentIndex is updated.
It looks like it isn't a scrollView with paging enabled to me. I think it could be easier with a UIGestureRecognizer. If it recognises a gesture, slide the view of the current tab and update the segmented control.