Swift: How a Button Can Perform a Sequence - ios

How can a button unhid textfields each time its pressed and then after 4 times of being pressed the button becomes inactive? Here is my code so far. Thanks in advance!
import UIKit
class AddNewGameViewController: UIViewController {
#IBOutlet weak var Player1: UITextField!
#IBOutlet weak var Player2: UITextField!
#IBOutlet weak var Player3: UITextField!
#IBOutlet weak var Player4: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
Player1.hidden = true
Player2.hidden = true
Player3.hidden = true
Player4.hidden = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func addPlayer(sender: UIButton) {
sender.enabled = false
Player1.hidden = false
Player2.hidden = false
Player3.hidden = false
Player4.hidden = false
}

change your #IBOutlets to a #IBOutlet Collectionlike this :
#IBOutlet var players: [UITextField]!
then connect each textfield to it Player1->Player2-> Player3->Player4
finally in the btn action :
for textField in players {
if textField.hidden {
textField.hidden = false
if textField == players.last {
sender.enabled = false
}
break
}
}
update
you can hide all them with players.forEach {$0.hidden = true} in viewDidload
or hide them in xib/storyboard.
connect image :

Related

Not woking UIView ishidden after change UILabel text value in swift

I want to change uilabel value and show uiview that has hide like a popup windows.
When I touch a button, that code print "setPopupView 0".
but doesn't show settingView.
and I touch a button again.
print "setPopupView 0" and show settingView.
so When "settingLabelValueUnit text" has changed not show settingView,
but when "settingLabelValueUnit text" has not changed show settingView.
(It means "settingLabelValue text" is same as input value)
I don't know why.
Anyone can help me?
Thank you
here is my swift code.
class ViewController: UIViewController {
#IBOutlet weak var workoutScrollView: UIScrollView!
#IBOutlet weak var settingView: UIView!
#IBOutlet weak var settingLabelValueUnit: UILabel!
#IBOutlet weak var imgSettingBGcal: UIImageView!
#IBOutlet weak var imgSettingBGdis: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
self.imgSettingBGcal.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(calSettingClick)))
self.imgSettingBGdis.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(disSettingClick)))
}
func calSettingClick(sender: UITapGestureRecognizer) {
setPopupView(0)
}
func disSettingClick(sender: UITapGestureRecognizer) {
setPopupView(1)
}
func showPopup (_ settype:Int) {
switch settype {
case 0:
print("setPopupView 0")
self.settingLabelValueUnit.text = "Set1"
self.workoutScrollView.isHidden = true
self.settingView.isHidden = false
case 1:
print("setPopupView 0")
self.settingLabelValueUnit.text = "Set2"
self.workoutScrollView.isHidden = true
self.settingView.isHidden = false
}
}
}

How do i get the Id of text view with radio Buttons clicked

For the radioButtons I have used Class in the button and for the indicator to buttons i have used the labels.
These are My TextView
#IBOutlet weak var TextViewEveryNewMessage: UILabel!
#IBOutlet weak var TextViewWeekly: UILabel!
#IBOutlet weak var TextViewDaily: UILabel!
#IBOutlet weak var TextViewMonthly: UILabel!
These are my Radio Buttons
#IBOutlet weak var RadioBtnMonthly: SSRadioButton!
#IBOutlet weak var RadioBtnWeekly: SSRadioButton!
#IBOutlet weak var RadioBtnDefaultChecked: SSRadioButton!
#IBOutlet weak var RadioBtnDaily: SSRadioButton!
As soon as i press the ok button i need to save the checkedRadio Button text somewhere and i have done it like this.
#IBAction func OkButton(sender: UIButton) {
SwiftSpinner.show(kLoadingText)
if RadioBtnDefaultChecked.selected{
preferencesConditions.notificationFrequency = self.TextViewEveryNewMessage.text
}else if RadioBtnDaily.selected{
preferencesConditions.notificationFrequency = self.TextViewDaily.text
}else if RadioBtnWeekly.selected{
preferencesConditions.notificationFrequency =
self.TextViewWeekly.text
}else{
preferencesConditions.notificationFrequency = self.TextViewMonthly.text
}
Is this correct way i am doing.Or there are any other approach. Please suggest me.
Instead of assigning a value for preferencesConditions.notificationFrequency in OkButton(sender: UIButton), you should do it in each of the SSRadioButton buttons and that's by calling SSRadioButtonControllerDelegate - didSelectButton:
func didSelectButton(aButton: UIButton?) {
print(aButton)
if aButton === RadioBtnDefaultChecked {
preferencesConditions.notificationFrequency = self.TextViewEveryNewMessage.text
}else if aButton === RadioBtnDaily {
preferencesConditions.notificationFrequency = self.TextViewDaily.text
}else if aButton === RadioBtnWeekly {
preferencesConditions.notificationFrequency =
self.TextViewWeekly.text
}else{
preferencesConditions.notificationFrequency = self.TextViewMonthly.text
}
}
Don't forget to conform the delegate to the viewController:
var radioButtonController: SSRadioButtonsController?
override func viewDidLoad() {
radioButtonController = SSRadioButtonsController(buttons: RadioBtnDefaultChecked, RadioBtnDaily, RadioBtnWeekly)
radioButtonController!.delegate = self
radioButtonController!.shouldLetDeSelect = true
}
The IBAction should be like:
#IBAction func OkButton(sender: UIButton) {
SwiftSpinner.show(kLoadingText)
}
For more information, check SSRadioButtonsController.
Hope this helped.

How can I change UIStepper value when changed from UITextField?

I'm very new to all of this and I found some code that got me understanding some of this syntax. I'm trying to create a textfield that lets me type in a value that updates the stepper's value. The stepper currently works (updates the uitextfield) but when I change the value in the textfield it doesn't update the stepper's value, so when I click on the stepper, it reverts back to whatever value it was before I typed in a value... Can anyone tell me why the two functions STracksValueDidChange and CTrackValueDidChange have errors?
Here's my code so far:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var STracks: UITextField!
#IBOutlet weak var STracksStepper: UIStepper!
#IBOutlet weak var CTracks: UITextField!
#IBOutlet weak var CTrackStepper: UIStepper!
override func viewDidLoad() {
super.viewDidLoad()
STracksStepper.autorepeat = true
STracksStepper.maximumValue = 100.0
STracksStepper.minimumValue = 2.0
STracksStepper.stepValue = 2.0
print(STracksStepper.value)
STracks.text = "\(Int(STracksStepper.value))"
STracksStepper.addTarget(self, action: "SstepperValueDidChange:", forControlEvents: .ValueChanged)
STracks.addTarget(self, action: "STextValueDidChange:", forControlEvents: .ValueChanged)
CTrackStepper.autorepeat = true
CTrackStepper.maximumValue = 100.0
CTrackStepper.minimumValue = 2.0
CTrackStepper.stepValue = 2.0
print(CTrackStepper.value)
CTracks.text = "\(Int(CTrackStepper.value))"
CTrackStepper.addTarget(self, action: "CstepperValueDidChange:", forControlEvents: .ValueChanged)
CTracks.addTarget(self, action: "CTextValueDidChange:", forControlEvents: .ValueChanged)
}
//Steppers will update UITextFields
func SstepperValueDidChange(stepper: UIStepper) {
let stepperMapping: [UIStepper: UITextField] = [STracksStepper: STracks]
stepperMapping[stepper]!.text = "\(Int(stepper.value))"
}
func STracksValueDidChange(SText: UITextField) {
let STextMapping: [UITextField: UIStepper] = [STracks: STracksStepper]
STextMapping[SText]!.value = "(SText.text)"
}
func CstepperValueDidChange(stepper: UIStepper) {
let stepperMapping: [UIStepper: UITextField] = [CTrackStepper: CTracks]
stepperMapping[stepper]!.text = "\(Int(stepper.value))"
}
func CTrackValueDidChange(CText: UITextField) {
let CTextMapping: [UITextField: UIStepper] = [CTracks: CTrackStepper]
CTextMapping[CText]!.value = "(CText.text)"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Try something like this.
CTrackStepper.value = Double(Textfield.text)
I am not so sure what the mapping is in your code.
But i don't think you need it for changing the value.
Update, made a project my self:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var textfield: UITextField!
#IBOutlet weak var stepper: UIStepper!
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 stepperValueChanged(sender: UIStepper) {
textfield.text = String(sender.value)
}
#IBAction func valueChanged(sender: UITextField) {
if Double(sender.text!) != nil {
stepper.value = Double(sender.text!)!
}
}
}
For steppervaluechanged and valuechanged just drag from uistepper and textfield and choose action and change the Anyobject to Uistepper of Uitextfield.
Good luck :)

UIActivityIndicator shows for a split second

I am trying to show an spinning activity indicator for my app when the user taps on a button before they are presented with the next view - it kind of works, I see it for a split second
#IBOutlet weak var myActivityIndicator: UIActivityIndicatorView!
#IBOutlet weak var eventsbtn: UIButton!
#IBOutlet weak var pubsbtn: UIButton!
override func viewWillAppear(animated: Bool) {
myActivityIndicator.stopAnimating()
myActivityIndicator.hidden = true
}
override func viewDidLoad() {
super.viewDidLoad()
eventsbtn.layer.cornerRadius = 8
pubsbtn.layer.cornerRadius = 8
}
#IBAction func getEvents(sender: AnyObject) {
myActivityIndicator.hidden = false
myActivityIndicator.startAnimating()
}
Refactor your code. You don't need to stop the activity indicator in viewWillAppear. You can hide it in viewDidLoad. Here's the code:
#IBOutlet weak var myActivityIndicator: UIActivityIndicatorView!
#IBOutlet weak var eventsbtn: UIButton!
#IBOutlet weak var pubsbtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
eventsbtn.layer.cornerRadius = 8
pubsbtn.layer.cornerRadius = 8
myActivityIndicator.hidden = true
}
#IBAction func getEvents(sender: AnyObject) {
myActivityIndicator.hidden = false
myActivityIndicator.startAnimating()
}
Make sure that the IBOutlet for the activity indicator and IBAction for getting events are correctly connected in Interface Builder.
I changed it and it doesn't work, until I go back in the navigation
#IBOutlet weak var myActivityIndicator: UIActivityIndicatorView!
#IBOutlet weak var eventsbtn: UIButton!
#IBOutlet weak var pubsbtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
myActivityIndicator.stopAnimating()
myActivityIndicator.hidden = true
eventsbtn.layer.cornerRadius = 8
pubsbtn.layer.cornerRadius = 8
}
#IBAction func getEvents(sender: AnyObject) {
myActivityIndicator.hidden = false
myActivityIndicator.startAnimating()
}
viewDidAppear is probably a more appropriate place to stop the animation and hide the spinner.

Array of UIButtons or method UIButton(named: "\(string)")

Is it possible to make an array of UIButtons?
let buttonArray: [UIButton] = [UIButton(Button1)!, UIButton(Button2)!, UIButton(Button3)!]
To reference later as
buttonArray[0].setImage(UIImage, forState: UIControlState.Normal)
Or somehow set a
var button = UIButton.whoseName = "Specific String"
button.setImage(UIImage, forState: UIControlState.Normal)
VC1:
import UIKit
class VC1: UIViewController {
#IBOutlet weak var Card1: UIButton!
#IBOutlet weak var Card2: UIButton!
#IBOutlet weak var Card3: UIButton!
let Cards: [UIImage] = [UIImage(named: "Default")!, UIImage(named: "2s")!,UIImage(named: "2h")!,UIImage(named: "2c")!,UIImage(named: "2d")!,]
// var CardCaller: Array<UIButton> = [Card1, Card2, Card3]
let CardCallers: [UIButton] = [Card1, Card2, Card3] //ERROR: 'VC1.Type' does not have a member named 'Card1'
var caller = ""
var Index2 = Int()
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.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let KVC = segue.destinationViewController as VC2
KVC.source = segue.identifier!
}
#IBAction func unwind(unwindSegue: UIStoryboardSegue){
//Card1.setImage(Cards[Index2], forState: UIControlState.Normal)
//let button = UIButton(named: "Card1")!
if caller == "Card1" {Card1.setImage(Cards[Index2], forState: UIControlState.Normal)} else if caller == "Card2" {Card2.setImage(Cards[Index2], forState: UIControlState.Normal)} else if caller == "Card3" {Card3.setImage(Cards[Index2], forState: UIControlState.Normal)}
}
#IBAction func text(sender: AnyObject) {
println(caller)
println(Index2)
}
}
The IBOutlets are wired properly to Storyboard buttons; I created them with control/drag to the view controller.
This shouldn't be a problem, but it may have something to do with when you're accessing the buttons. Try creating the array globally and adding the buttons to it once they've loaded:
class VC1: UIViewController {
#IBOutlet weak var Card1: UIButton!
#IBOutlet weak var Card2: UIButton!
#IBOutlet weak var Card3: UIButton!
var CardCallers: [UIButton] = [UIButton]() // Empty UIButton array
override func viewDidLoad() {
self.CardCallers = [self.Card1, self.Card2, self.Card3] // Buttons have now loaded in the view
}
}
You can.
Two ways to do it:
var buttonsArray: Array<UIButton> = [button1, button2]
Or
#IBOutlet var buttonsArray: Array<UIButton> = []
For the later one, it can be filled from the storyboard as an IBOutletCollection
Please note that views can be assigned to an IBOutlet and an IBOutletCollection at the same time.

Resources