I have an experience where a user is rating a product, they do this by dragging sliders. Each slider has a related UILabel for the title and a UILabel for the value. I would like to avoid creating 12 functions, one for each slider and the associated label. I am new to development generally. I am guessing a class or an Array would be useful here, but am not sure how to use either. Here's the code that just updates the one value, and I know why that is, I'm just hoping to avoid declaring 12 variables for the value and 12 functions for each one.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var slider1: UISlider!
#IBOutlet weak var value1: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func sliderSlide(sender: UISlider) {
value1.text = round(sender.value*100).description
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Well,you can use Outlet Collection and 'tag',
Drag every label into a same Outlet Collection
Drag every Slider IBAction into a same function
Then set the tag of slider as the index of label in Outlet Collection.
For example,you first drag labelA to Collection,then the tag is 0
Then all the code
import UIKit
class ViewController: UIViewController {
#IBOutlet var labels: [UILabel]!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func sliderSlide(sender: UISlider) {
let index = sender.tag
let label = labels[index]
label.text = round(sender.value*100).description
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
You can connect more than one slider to the same function. That's the purpose of the sender argument. You can do a switch statement on the pointer in swift, or by a set tag of the slider if you prefer. If it's all updating the same label, why even care which slider is updating?
I can think of two ways of doing this. Make a custom view controller that looks exactly the same as the code you have. It will have the slider and label as subviews. Then you'd only have 12 container views on your storyboard (not the greatest).
The other way would be using IBOutletCollections. These are what they sound like, collections of IBOutlets. Assign each slider that you place on the storyboard a unique tag from 0-11 (this is done under the attributes inspector). The tag will act as an index for the values array. Make sure that when you add each label to the collection, you do it in the correct order (it does matter!). Using IBOutletCollections, your code would look like this:
class ViewController: UIViewController {
#IBOutlet weak var sliders: [UISlider]!
#IBOutlet weak var values: [UILabel]!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func sliderSlide(sender: UISlider) {
values[sender.tag].text = round(sender.value*100).description
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Related
error when try to assign a string to the UITextView.
coding as below:-
class ViewController: UIViewController{
#IBOutlet weak var txtResult: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
txtResult.text = "ABC"
}
}
Go to your code and delete the #IBOutlet line of code that you created
Delete this in Storyboard:
I think you have few referencing outlets there for 1 object. You need to have only 1. So better to remove all of them.
Add new outlet again.
It should work.
Providing more error details might help to solve your issue. But you can still solve it by checking few things-:
1) Check for UITextView outlet in storyboard-:
2) Check if you have given correct class to controller . In my case it's UnderlineViewController check for yours. Your controller should have same class.
2.a) Go to storyboard click on controller and select identity inspector.
2.b) check class name.
Now add text in controller class-:
import UIKit
class UnderlineViewController: UIViewController {
#IBOutlet weak var textViewData: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
textViewData.text = "Tushar"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
OUTPUT-:
MAKE SURE YOU HAVE NO MORE THEN 1 OUTLET FOR ANY VIEW IN SIMILAR
CONTROLLER.
I realize this question has been asked numerous times before, but I can't quite get the solutions to work, even by just copying and pasting them, and suspect that most swift documentation spans the three versions since swift's release.
I'm attempting to do something as simple as storing a variable from a field input and not having much luck.
import UIKit
class ViewController: UIViewController {
#IBOutlet var userNumber: UILabel!
#IBOutlet var userField: UITextField!
#IBAction func userButton(_ sender: UIButton) {
let userInput = userField.text
//some action
}
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.
}
}
You should check whether you have set your textfield's delegate with respect to parent view controller.
Go to storyboard.
Select textfield.
Right click on it.
set delegate from textfield to view controller
I'm trying to change the application language using two buttons, First I used this code in this link : https://github.com/marmelroy/Localize-Swift/blob/master/Sources/Localize.swift
Localize.swift and placed it in my project ,
And here is the code of the table view controller that has two buttons and two lables :
import UIKit
class MyTableViewVontroller: UITableViewController {
#IBOutlet weak var firstLabel: UILabel!
#IBOutlet weak var secondLabel: UILabel!
#IBOutlet weak var arabicChanger: UIBarButtonItem!
#IBOutlet weak var englishChanger: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
populateValues()
}
#IBAction func englishChanger(sender: AnyObject) {
Localize.setCurrentLanguage("en")
}
#IBAction func arabicChanger(sender: AnyObject) {
Localize.setCurrentLanguage("ar")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func populateValues(){
firstLabel.text = NSLocalizedString("PETRA",comment: "")
secondLabel.text = NSLocalizedString("AMMAN",comment: "")
}
}
But when I run it and press any of the buttons nothing changes. Even though localization works just fine as I have a Strings files for both languages and when changing language from the system it works, but it doesn't when the buttons are pressed.
let me first quote what the library's github information page says
To update the UI in the viewcontroller where a language change can
take place, observe LCLLanguageChangeNotification :
you have to add an observer for this to change, to know how an observer works,get your self familiar with NSNotificationCenter
by the way, I don't recommend to use a third party library in this case, just go with the standards for simple tasks, try using the default NSLocalizedString localization, it's great !
I want Subtract process with Button action but my code does not work.
I made the Number. This number is Main number for user's game money. I set the '1,000'. This I set the name of this "playerMoney"
After that I made the #IBAction Button for Discount the '500' number from 'playerMoney'. This button's action means buy Game item things.
I set the name of '500' number is "chocolatePrice" and Button's name is "buyChocolateButton"
I also print playerMoney on the UILable.
I set that label name is "printPlayerMoney"
I'm using the code below.
import UIKit
class ViewController: UIViewController {
var playerMoney = 1000
var chocolatePrice = 500
#IBOutlet weak var printPlayerMoney: UILabel!
#IBAction func buyChocolateButton(sender: AnyObject) {
playerMoney = playerMoney - chocolatePrice
}
override func viewDidLoad() {
super.viewDidLoad()
printPlayerMoney.text = "\(playerMoney)"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
You need to put the printPlayerMoney.text = "\(playerMoney)" inside the button function. That way, overtime you press the button, it does the calculation and then updates the label. See the updated code below:
class ViewController: UIViewController {
var playerMoney: Int = 1000
var chocolatePrice: Int = 500
#IBOutlet weak var printPlayerMoney: UILabel!
#IBAction func buyChocolateButton(sender: AnyObject) {
playerMoney = playerMoney - chocolatePrice
printPlayerMoney.text = "\(playerMoney)"
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
printPlayerMoney.text = "\(playerMoney)"
doesn't watch the value of playerMoney
After changing the value of playerMoney, we have to set the text of the UILabel again if we're expecting a visual change
I have a very simple app so far. Two view controllers. I've set up a new .swift file for the second view. On each view I have a button that when pressed, changes a label to say "Pressed". Pretty simple.
On the first view controller everything works as expected. However, on the second view controller the app crashes when I press the button. I've set up IBOutlets and actions for all appropriate parts.
Does anyone have any insight?
code:
import UIKit
class PlayViewController: UIViewController {
#IBOutlet weak var newCardButton: UIButton!
#IBOutlet weak var labelTest: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func newCardButtonPressed(sender: UIButton) {
self.labelTest.text = "Pressed"
}
}
Screenshot:
Screenshot after crash- http://i.imgur.com/CHt8kA5.png
I think you should change the sender part like this.
#IBAction func newCardButtonPressed(sender: AnyObject) {
self.labelText.text = "Pressed"
}
If your connections are not set properly your app also crash. Delete them and reconnect it.From Utilities/connections inspector.