How to show and hide a view on button click - ios

I have one uiview , Button .At initial my view will be hidden So when my button click I need to show my uiview and when I click same button I need to hide same view.
How to do that in swift 2.0.Now what I did is when I click first time - its showing.
#IBAction func PressRefine(sender: AnyObject) {
menuView.hidden = false
}
But again when I press it should hide.
How to do that???

Try an if statement.
#IBAction func PressRefine(sender: AnyObject) {
if menuView.hidden {
menuView.hidden = false
} else {
menuView.hidden = true
}
}
or as #TedHuinink suggested, with less code.
#IBAction func PressRefine(sender: AnyObject) {
menuView.hidden = !menuView.hidden
}

first make view outlet
#IBOutlet weak var newpptview: UIView!
#IBAction func newpresentation(_ sender: Any) {
if newpptview.isHidden{
newpptview.isHidden = false
} else{
newpptview.isHidden = true
}
}
its better that to all
OR
#IBAction func newpresentation(_ sender: Any) {
newpptview.isHidden = !newpptview.isHidden
}
swift 3 and 4

Related

How to hide button and disable button

I'm trying to create small restaurant app for employees. There I have table numbers as a button if the user clicks that button I want that clicked button to be disabled and I want textfield and another ok button to appear. And if I click on disable button I want that to be enabled.
import UIKit
class ViewController: UIViewController {
var total = 0
#IBOutlet weak var okButton: UIButton!
#IBOutlet weak var userInput: UILabel!
#IBOutlet weak var userValue: UITextField!
#IBAction func okButton(_ sender: UIButton) {
if userValue.text != nil{
userInput.text = String(0)
let userValueint: Int? = Int(userValue.text!)
total = total + userValueint!
let convertText = String(total)
userInput.text = convertText
userValue.text = String(0)
userValue.isHidden = true
okButton!.isHidden = true
} else {
print("Please Inter values")
}
}
#IBAction func buttenPressed(_ sender: UIButton) {
userValue.isHidden = false
okButton.isEnabled = true
}
override func viewDidLoad() {
super.viewDidLoad()
userValue.isHidden = true
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
okButton.isHidden = false
}
}
So far I'm able to hide textField at the beginning and able to enabled when table button is clicked, but I can't hide ok button and disable the table button. Any suggestion?
First of all, you can't clicked a disabled button. Second, use viewDidLoad instead of viewWillAppear.
I guess your table button is sender so disable sender and show ok button
#IBAction func buttenPressed(_ sender: UIButton) {
sender.isEnabled = false
userValue.isHidden = false
okButton.isHidden = false
okButton.isEnabled = true
}

Is hidden not working on image views in Swift 3?

#IBOutlet weak var allStocksSelected: UIImageView!
#IBOutlet weak var shortStocksSelected: UIImageView!
#IBOutlet weak var longStocksSelected: UIImageView!
#IBOutlet weak var myStocksTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
myStocksTableView.delegate = self
myStocksTableView.dataSource = self
}
override func viewDidAppear(_ animated: Bool) {
self.shortStocksSelected.isHidden = true
self.longStocksSelected.isHidden = true
self.allStocksSelected.isHidden = true
}
#IBAction func allStocksButtonPressed(_ sender: Any) {
print("ALL!")
self.stockTypeChanged(stockType: allStocksSelected)
}
#IBAction func shortStocksButtonPressed(_ sender: Any) {
print("SHORT!")
self.stockTypeChanged(stockType: shortStocksSelected)
}
#IBAction func longStocksButtonPressed(_ sender: Any) {
print("LONG!")
self.longStocksSelected.isHidden = false
self.shortStocksSelected.isHidden = true
self.allStocksSelected.isHidden = true
//stockTypeChanged(stockType: longStocksSelected)
}
#IBAction func addNewStockButtonPressed(_ sender: Any) {
}
#IBAction func signOutButtonPressed(_ sender: Any) {
}
private func stockTypeChanged(stockType: UIImageView) {
let stockTypes: [UIImageView] = [allStocksSelected, shortStocksSelected, longStocksSelected]
for (stock) in stockTypes {
if (stock == stockType) {
stock.isHidden = false
} else {
stock.isHidden = true
}
}
}
As shown from my code above, I am basically just trying to hide and show certain image views when buttons are pressed.
I am 100% sure that all of the buttons actions and IB outlets are properly connected, yet the image views are still not hiding and showing, and I cannot figure out why.
I was running into this same issue. Placing your isHidden code to execute on the main thread should solve the problem.
DispatchQueue.main.sync {
}

Radio Button Group Swift 3 Xcode 8

I have searched various sources and could not find a clear and simple solution for creating the equivalent of a radio button group in Swift 3 with Xcode 8.3 for an iOS application.
For example if I have 3 buttons in one group and only one should be selected at a time. Currently I am implementing this by changing the state of 2 buttons in the group to not selected when the other one is selected and vice versa.
#IBAction func buttonA(_ sender: Any) {
buttonB.isChecked = false
buttonC.isChecked = false
}
#IBAction func buttonB(_ sender: Any) {
buttonA.isChecked = false
buttonC.isChecked = false
}
#IBAction func buttonC(_ sender: Any) {
buttonA.isChecked = false
buttonB.isChecked = false
}
However I would expect a more efficient way to do this.
Any help on a more efficient solution will be appreciated.
You can connect all your button's IBAction to one single method.
#IBAction func buttonClick(_ sender: UISwitch) { // you're using UISwitch I believe?
}
You should add all the buttons into an array:
// at class level
var buttons: [UISwitch]!
// in viewDidLoad
buttons = [buttonA, buttonB, buttonC]
Then, write the buttonClick method like this:
buttons.forEach { $0.isChecked = false } // uncheck everything
sender.isChecked = true // check the button that is clicked on
Alternatives:
Try using a UITableView. Each row contains one option. When a row is selected, change that row's accessoryType to .checkMark and every other row's to .none.
If you are too lazy, try searching on cocoapods.org and see what other people have made.
Just make a single selector for all three button's touchUpInside event, and set radio_off image for normal state and radio_on image for selected state in your IB, then only you have to connect btnClicked method to all button's touchUpInside event
class ViewController: UIViewController {
#IBOutlet weak var btnFirst:UIButton!
#IBOutlet weak var btnSecond:UIButton!
#IBOutlet weak var btnThird:UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func btnClicked(sender:UIButton){
let buttonArray = [btnFirst,btnSecond,btnThird]
buttonArray.forEach{
$0?.isSelected = false
}
sender.isSelected = true
}
Depending on your UI, you could take multiple approaches.
UITableView - Use a UITableView with a checkmark decorator. If your layout for these radio buttons is fairly traditional, this is the correct paradigm. If the layout is a grid instead of a list, you could use UICollectionView.
You can use the func table(_ table: WKInterfaceTable, didSelectRowAt rowIndex: Int) in UITableViewDelegate to capture the selection. You can call indexPathForSelectedRow on the tableView when you want to commit the change to determine which cell was selected.
Apple's tutorial on UITableView can be found at:
https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/CreateATableView.html
Manage a group of UIButtons - You could store an array of references to UIButton objects that are part of your radio button group.
protocol RadioButtonDelegate: class {
func didTapButton(_ button: UIButton)
}
class RadioButtonGroup {
private var buttons: [UIButton] = []
weak var delegate: RadioButtonDelegate?
var selectedButton: UIButton? { return buttons.filter { $0.isSelected }.first }
func addButton(_ button: UIButton) {
buttons.append(button)
}
#objc private func didTapButton(_ button: UIButton) {
button.isSelected = true
deselectButtonsOtherThan(button)
delegate?.didTapButton(button)
}
private func deselectButtonsOtherThan(_ selectedButton: UIButton) {
for button in buttons where button != selectedButton {
button.isSelected = false
}
}
}
class MyView: UIView {
private var radioButtonGroup = RadioButtonGroup()
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
let button1 = UIButton(type: .custom)
button1.setTitle("Eeeny", for: .normal)
let button2 = UIButton(type: .custom)
button2.setTitle("Meeny", for: .normal)
let button3 = UIButton(type: .custom)
button3.setTitle("Miny", for: .normal)
self.radioButtonGroup.addButton(button1)
self.radioButtonGroup.addButton(button2)
self.radioButtonGroup.addButton(button3)
addSubview(button1)
addSubview(button2)
addSubview(button3)
}
}
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var maleLB: UIButton!
#IBOutlet weak var femaleLB: UIButton!
#IBOutlet weak var otherLB: UIButton!
var gender = "Male"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if gender == "Male"{
femaleLB.isSelected = true
}
}
#IBAction func maleBtn(_ sender: UIButton) {
if sender.isSelected {
sender.isSelected = false
femaleLB.isSelected = false
otherLB.isSelected = false
}
else{
sender.isSelected = true
femaleLB.isSelected = false
otherLB.isSelected = false
}
}
#IBAction func femaleBtn(_ sender: UIButton) {
if sender.isSelected {
sender.isSelected = false
maleLB.isSelected = false
otherLB.isSelected = false
}
else{
sender.isSelected = true
maleLB.isSelected = false
otherLB.isSelected = false
}
}
#IBAction func otherBtn(_ sender: UIButton) {
if sender.isSelected {
sender.isSelected = false
maleLB.isSelected = false
femaleLB.isSelected = false
}
else{
sender.isSelected = true
maleLB.isSelected = false
femaleLB.isSelected = false
}
}
}

How to use one button to add scores for different integers (swift3)

Right now i have a purple and red score. I would like to use the 1 and 2 buttons to add the score for the colors. I know how to do this by creating just a sets of buttons to the specific color but doing this again and again will create problems. So what I am trying to do is if purple is selected and if and only if 1 is pressed then the total score of purple will = 1. I guess what I am trying is perform a switch statement for the buttons.
Before purple/red button is pressed.
After purple/red button is pressed.
import UIKit
class ViewController: UIViewController {
var purpleScore = 0
var redScore = 0
#IBOutlet var plus1: UIButton!
#IBOutlet var plus2: UIButton!
#IBOutlet var addRed: UIButton!
#IBOutlet var addPurlple: UIButton!
#IBOutlet var totalScorePurple: UILabel!
#IBOutlet var totalScoreRed: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
plus1.isHidden = true
plus2.isHidden = true
}
#IBAction func enteringRed(_ sender: Any) {
plus1.isHidden = false
plus2.isHidden = false
}
#IBAction func enteringPurple(_ sender: Any) {
plus1.isHidden = false
plus2.isHidden = false
}
#IBAction func plus1(_ sender: Any) {
}
#IBAction func plus2(_ sender: Any) {
}
}
If I understand correctly as the comment above, this should work
Create 2 Bool for red and purple to see weather they are checked or not, this is easiest way, else you can just check the redButton color got changed (if it does) or .selected = true
var redChecked = false
var purpleChecked = false
#IBAction func enteringRed(_ sender: Any) {
redChecked = !redChecked
//change color etc..
}
#IBAction func enteringPurple(_ sender: Any) {
purpleChecked = !purpleChecked
//change color etc..
}
#IBAction func plus1(_ sender: Any) {
if redChecked {
redScore+=1
}
if purpleChecked {
purpleScore+=1
}
}
#IBAction func plus2(_ sender: Any) {
if redChecked {
redScore+=2
}
if purpleChecked {
purpleScore+=2
}
}

Back Button on UIWebView

I'm trying to figure out how to create a back button that allows the user to go back one page. I read through Apple's Docs (which still go way over my head) and found out that I need to setup the canGoBack and goBack's. I have tried this, but for some reason it is not working. My UIWebView is named viewWeb, and I created and attached an outlet to my Back button, named backButton, and also tagged it as 1. Here is my code that I wrote in the View Controller:
// Back button:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
[_backButton addTarget:_viewWeb
action:#selector(goBack)
forControlEvents:UIControlEventTouchDown];
if ([_viewWeb canGoBack]) {
NSLog(#"Back button pressed.");
[_viewWeb goBack];
}
}
else return;
}
Does anyone know what I need to change / add to get this working?
actionSheet:clickedButtonAtIndex: is for UIActionSheet objects, not UIButton actions.
You should probably write an IBAction method that looks something like this:
- (IBAction)backButtonTapped:(id)sender {
[_viewWeb goBack];
}
and connect it to the Touch Up Inside action from the button.
You can search for more info on IBAction but that's likely what you want
For swift 4 and swift 5
#IBAction func refreshAction(_ sender: Any) {
self.webView.reload()
}
#IBAction func backAction(_ sender: Any) {
if(self.webView.canGoBack) {
self.webView.goBack()
}
}
#IBAction func forwardAction(_ sender: Any) {
if self.webView.canGoForward{
self.webView.goForward()
}
}
I think it should be more specific like this
- (IBAction)backButtonTapped:(id)sender {
if ([_viewWeb canGoBack] ) {
[_viewWeb goBack];
}
}
I have struggled to get a working code for this function written in swift and finally here's what i came up with.
#IBOutlet weak var goBackBtn: UIBarButtonItem!
#IBOutlet weak var goForwardBtn: UIBarButtonItem!
#IBOutlet weak var itemWebView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL (string: "www.google.com")
let requestObj = NSURLRequest(URL: url)
itemWebView.loadRequest(requestObj)
itemWebView.delegate = self
goBackBtn.enabled = false
goForwardBtn.enabled = false
}
func webViewDidFinishLoad(webView: UIWebView) {
goBackBtn.enabled = itemWebView.canGoBack
goForwardBtn.enabled = itemWebView.canGoForward
}
#IBAction func forward(sender: AnyObject) {
itemWebView.goForward()
}
#IBAction func back(sender: AnyObject) {
itemWebView.goBack()
}

Resources