I have a +1 button and a -1 button and a label that starts at 0. My +1 button is working great, but for some reason, my -1 button isn't working. Can anyone help?
var sales = 0
#IBOutlet weak var numberOfSalesLabel: UILabel!
#IBOutlet weak var minusOneSaleOutlet: UIButton!
#IBAction func plusOneSale(sender: AnyObject) {
sales += 1
numberOfSalesLabel.text = "\(sales)"
if sales >= 1 {
minusOneSaleOutlet.hidden = false
}
}
override func viewDidLoad() {
}
#IBAction func minusOneSale(sender: AnyObject) {
sales -= 1
numberOfSalesLabel.text = "\(sales)"
if sales == 0 {
minusOneSaleOutlet.hidden = true
}
}
Anybody got any ideas why my minus button isn't working? I'm thinking it might have to do with me calling it as an outlet and as an action, but I'm not sure. Thanks!
p.s.-I'm not sure if this is normal.
Using the connections inspector you'll need to make sure all the connections are correct.
If at any point you delete an #IBAction from your code, then create another connection, the old one will still remain until you remove it properly from the connection inspector.
Each of your buttons should only be connected to a single #IBAction (touchUpInside)
Below is what the connections inspector looks like. The image is from one of my own projects, and shows a connected delegate. So yours will look a bit different.
If all else fails, remove all connections on this view (click the x) and link them up again by Ctrl dragging from your button, into the code viewer right on top of your #IBAction. Sometimes its just easier to start again.
Related
I've created a new window controller that hosts a tabViewController inside my app. I've added classes to the window controller, and have the same class across all the view controllers in the tab view.
I can connect buttons and give them an action and it works perfectly, however, I try to connect an outlet and attempt to change something via the outlet, it returns nil and crashes the program. For example, this exact code works in the pre-made viewController of my app, but returns:
Unexpectedly found nil while implicitly unwrapping an Optional value
when running it through the new tab view controller I created.
What's weird to me is I can use a regular view controller and the outlets connect fine, but if I want to use a tab view controller, these nil errors are happening.
I made sure that the nil was not related to grabbing the inputs for the button by printing the audio devices, and the audio devices are there and able to be printed. It seems as if the button is not there even though it is connected.
I have also tried to simply change an NSTextField color after connecting it to an outlet and this returns the same nil error.
Any idea what I might be doing wrong here? Thanks so much for the help.
class FirstLaunchViewController: NSViewController {
var FLWindow: FirstLaunchWindowController?
var selectedAudioDevice = [String]()
#IBOutlet weak var deviceListPopUpButton: NSPopUpButton!
#IBOutlet weak var visualizeButton: NSButton!
override func viewDidLoad() {
super.viewDidLoad()
self.preferredContentSize = NSMakeSize(self.view.frame.size.width, self.view.frame.size.height)
populateDeviceList()
}
#IBAction func CloseStartupGuide(_ sender: Any) {
self.view.window?.windowController?.close()
}
#IBAction func popUpDidChange(_ sender: Any) {
print("changed")
}
//grab inputs for button
fileprivate func populateDeviceList() {
deviceListPopUpButton.removeAllItems()
for device in AudioDevice.allInputDevices() {
var teststring = ""
teststring = device.uid!
print(device.name)
deviceListPopUpButton.addItem(withTitle: device.name)
deviceListPopUpButton.lastItem?.tag = Int(device.id)
selectedAudioDevice.append(device.uid!)
}
}
}
}
I would like the user to hit the button add 1. Which then will display in the middle of the green box. So the user hits a button and the number one is displayed in the middle of the green box. This is not a counter so its just one. Think of it as a score card. The green box is referenced as score display.
import UIKit
class ViewController: UIViewController {
#IBOutlet var scoreDisplay: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func add1(_ sender: Any) {
}}
If I understood correctly, you wish to hide and show some views depending upon some user behaviour.
As of Swift 3, please take a look at the isHidden property of any UIView. You must have some #IBOutlets for your buttons, not only #IBActions.
A possible solution for you (write this in your add1 function):
scoreDisplay.setTitle("1", for: .normal)
scoreDisplay.isHidden = false
Of course, do not forget to firstly set the scoreDisplay as hidden in the Interface Builder.
I made an app where I save a label that I can make it go up or down one number in my app. But if I lose out of my app and open it back up, if i make the label go up again than it resets back to zero and goes to one. This is because I have to set the variable value to zero. Here is my code:
#IBOutlet var goal: UILabel!
#IBAction func player1button(sender: AnyObject)
{
NSUserDefaults.standardUserDefaults().setValue(goal.text!, forKey:"firstGoal")
}
var goal1 = 0
#IBAction func goalUp(sender: AnyObject)
{
goal1++
goal.text = "\(goal1)"
}
override func viewDidLoad()
{
super.viewDidLoad()
goal.text = (NSUserDefaults.standardUserDefaults().objectForKey("firstGoal") as? String)
}
Im saving the goal number then calling it back later in the text. Please show me a way to fix it so it just adds on to the previous number.
in viewDidLoad() add
goal1 = Int((NSUserDefaults.standardUserDefaults().objectForKey("firstGoal") as? String)!)
this will set the int variable you are using to keep track of the goals to the correct value when the app loads. currently you are only setting the text label to the correct value.
or you can simply make var goal1 a global variable , just after the imports and before the class keyword , in case you don't need to keep the data till the next time you open the app itself
I create Start_button and make #IBOutlet and #IBAction
#IBOutlet weak var Start_button: UIButton!
#IBAction func Start_button(sender: AnyObject)
Now, i want hide button after click. I try this, but this don't work:
#IBAction func Start_button(sender: AnyObject)
{
Start_button.hidden = true;
}
Error message:
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
How i can hide this button?
Thanks for helping!
Its nil because you probably haven't connected it from your storyboard/nib. You need to connect the outlet, you can't just create an outlet in code and expect it to be connected to the visible element. The same goes for your action. #IBOutlet / #IBAction stands for Interface Builder Outlet/Action, which means you have to connect them in Interface Builder.
Also its better if your action uses the sender, and not a local variable (when its pointing to the same thing). And you shouldnt use ;at the end of the line.
#IBAction func Start_button(sender: UIButton) // Change to UIButton
{
sender.hidden = true
// OR
// (sender as! UIButton).hidden = true
}
#IBAction func button_nameA(sender: AnyObject) {
// show hidden buttons
self.Target_Object.hidden = false
}
So when you click the A buttons its automaticly send to you target and in target also you have to hide button if you working single view app
I'm creating a word game using UIKit, and I want to represent the entire alphabet for the user in order to solve the puzzle, here is my code:
var emptyPos = [0]
#IBOutlet var pos1: UILabel!
#IBOutlet var pos2: UILabel!
#IBOutlet var pos3: UILabel!
#IBOutlet var pos4: UILabel!
#IBAction func btnA(sender: UIButton) {
letters(sender)
}
#IBAction func btnB(sender: UIButton) {
letters(sender)
}
#IBAction func btnC(sender: UIButton) {
letters(sender)
}
#IBAction func btnD(sender: UIButton) {
letters(sender)
}
func moveLetter (pos: UILabel, btn: UIButton) {
UIView.animateWithDuration(0.5, animations: { () -> Void in
btn.center = pos.center
})
}
func letters (btn: UIButton) {
switch emptyPos.count {
case 1:
moveLetter(pos1, btn: btn)
emptyPos.append(0)
println(emptyPos)
case 2:
moveLetter(pos2, btn: btn)
emptyPos.append(0)
println(emptyPos)
case 3:
moveLetter(pos3, btn: btn)
emptyPos.append(0)
println(emptyPos)
case 4:
moveLetter(pos4, btn: btn)
emptyPos.append(0)
println(emptyPos)
default:
println("Error")
}
}
The idea is the user has to click on a letter after letter to move them towards the empty labels and figure out the right word, and as you can see I went with making each letter a button and each empty space a label, but was wondering if there is a better way than creating 26 buttons for each letter. Linking all the buttons to a single function will not work because then I will have to rely on sender.tag which I cannot pass to my function in order to move the letter. So should I continue with what I'm doing or is there some better way to do this ?
if you make a custom UIButton you can add extra properties to the button, then change the sender of the #IBAction to your custom class, then just pass the button to your moveLetter and it can know what to do based on the information supplied by the button. then they can all share the same button press function
then if your buttons subclass has a #property string called ButtonLetter, you can define what its value is right in the storyboard instead of manually doing it in code for all of the buttons by giving it a runtime attribute like in the screen shot below
or you could be lazy and just get the text of the button and read what letter it is, but i would say this is a more proper way of going about it, cause this can apply to any type of button where maybe the text on the button isnt actually the value you want to use to do some computation when the button is pressed, but in your case it just so happens to be that way.