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 !
Related
I searched the whole internet but only found solutions for Swift 2 or outdated Xcode versions which doesnt help me :/
Also Im very new to Xcode and Swift, so please excuse that.
I want the user to input some numbers in several UITextFields and then press a "calculate" button which shows the result in a UILabel.
What I tested so far:
Convert input data to Integer in Swift
Swift calculate value of UITextFields and return value
So my code looks really poor so far. Nothing worked so thats all I have and the comment shows how I want it to be:
import UIKit
class ViewController: UIViewController {
//elements from the viewcontroller
#IBOutlet weak var input1: UITextField!
#IBOutlet weak var input2: UITextField!
#IBOutlet weak var calculate: UIButton!
#IBOutlet weak var output: UILabel!
//calculation
//Just simple math like
//output = input1 + input2
//or output = (input1 + 50) + input2
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.
}
}
Thank you for helping me out =)
First of all, you need to create an IBAction for your button by dragging from Storyboard. I think it is not a problem.
//imagine this is your IBAction function on calculate click
#IBAction func calculate(_ sender: UIView) {
output.text = String(Int(input1.text!)! + Int(input2.text!)!)
}
I skipped all validations, but for the happy path it should work
Connect your calculate button to below action.
#IBAction func calculate(sender: Any) {
let result = Int(input1.text!) + Int(input2.text!)
output.text = "\(result)"
}
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 am trying to add a photo reveal quiz piece to a larger app that I'm creating. The picture of the set up looks like this so far(rough draft so to speak)
The 16 labeled items are there just to give a sense of the end product(they will be white tiles in the app). How can I program it so that they begin disappearing when they user taps reveal picture? NSTimer? Should they be labels or UIViews or ?? Thanks for any hints.
I can get this far, but would want to add code to make the tiles disappear randomly and roughly each second
import UIKit
class ViewController: UIViewController {
var gameTimer : NSTimer!
#IBOutlet weak var imageView: UIImageView!
#IBOutlet weak var tile15: UILabel!
#IBOutlet weak var tile16: UIView!
#IBOutlet var photoTilesHandler: [UIView]!
//#IBOutlet weak var viewTile: UIView!
//#IBOutlet var pictureTiles: [UILabel]!
#IBOutlet var buttonHandler: [UIButton]!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func revealButton(sender: AnyObject) {
Hide()
print("button pressed")
}
func Hide() {
var dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(1 * Double(NSEC_PER_SEC)))
dispatch_after(dispatchTime, dispatch_get_main_queue(), {
self.tile15.hidden = true
})
}
override func viewDidDisappear(animated: Bool) {
gameTimer.invalidate()
}
}
Put all of your UILabels/UIViews/etc in an IBOutletCollection. Then inside of dispatch_after's callback do the following steps:
0.- You might want to keep track of which tiles are already facedown. So this way you can know at every time which index you have already used and pick always new ones.
One way to achieve this is storing the full list of indexes available to select and instead of taking into account your IBOutletCollection for the following steps take as reference your unused indexes list and, after you randomly pick one, remove it from the list.
1.- Determine a/(a set of) random integers between 0...[your IBOutletCollection].length - 1]
2.- Use this numbers as an index to set the hidden property of [your IBOutletCollection] corresponding element to true
As your other question, my recommendation would be to use UIViews with your UILabel inside or UIButtons. The tap zone of labels is a bit hard to use.
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.
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()
}
}