How to save On-Off value of a button? I have a On-Off button, when a user opens the app and click the Off button, I want the app to save it, so that when the app is reopened, the value of the button is still Off. Hopefully, this can be done by using User Defaults. My code:
class ViewController: UIViewController {
var soundEnable:Bool = true
var soundImage:UIImage?
#IBAction func soundbtn(_ sender: UIButton) {
let soundButton = sender
if (soundEnable) {
soundImage = UIImage.init(named: "mute.png")
soundEnable = false
} else {
soundImage = UIImage.init(named: "sound.png")
soundEnable = true
}
soundButton.setImage(soundImage, for: UIControlState.normal)
}
}
You need to set the image one more place at viewDidLoad.
You input the wrong parameter for setImage:
soundButton.setImage(soundImage, for: UIControlState())
it should be a specific state:
soundButton.setImage(soundImage, for: UIControlState.normal)
Please write these lines in viewDidLoad method
override func viewDidLoad() {
super.viewDidLoad()
if (UserDefaultsManager.useDarkTheme) {
soundImage = UIImage.init(named: "mute")
print(UserDefaultsManager.useDarkTheme)
} else {
soundImage = UIImage.init(named: "sound")
print(UserDefaultsManager.useDarkTheme)
}
}
#IBOutlet weak var backgroundImage: UIImageView!
let ImageNameKey = "ImageNameKey"
let soundbg = UIImage(named: "sound")
let mutebg = UIImage(named: "mute")
override func viewDidLoad() {
super.viewDidLoad()
//Sound.play(file: "bg", fileExtension: "wav", numberOfLoops: -1)
let defaults = UserDefaults.standard
if let name = defaults.string(forKey: ImageNameKey) {
if (name == "sound") {
backgroundImage.image = soundbg
} else {
backgroundImage.image = mutebg
}
}
}
#IBAction func clickbtn(_ sender: UIButton) {
let defaults = UserDefaults.standard
if(backgroundImage.image == soundbg){
backgroundImage.image = mutebg
defaults.set("mute", forKey: ImageNameKey)
Sound.enabled = false
}
else{
backgroundImage.image = soundbg
defaults.set("sound", forKey: ImageNameKey)
Sound.enabled = true
}
}
Related
An oldish retired DEC PDP 8 assembly programmer needs another set of eyes. I created a Hello World program that displays images of the family. The animation works fine, but I would like the user to be able to stop the animation so I added a button to “Pause” the image so that the user can view it at their leisure. However, the .stopAnimation function clears the view. How can this be prevented. Please be kind.
import UIKit
class FamilyUnitController: UIViewController {
#IBOutlet weak var familyMember: UILabel!
#IBOutlet weak var familyPhotos: UIImageView!
var familyName: String = ""
var familyUnitArray = [String]()
#IBOutlet weak var pauseButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
enter code here
configureImageView()
}
#IBAction func pauseImage(_ sender: UIButton) {
if familyPhotos.isAnimating {
familyPhotos.stopAnimating()
pauseButton.setTitle("Paused", for: [])
} else {
familyPhotos.startAnimating()
pauseButton.setTitle("Pause", for: [])
}
}
func configureImageView() {
familyMember.text = familyUnitArray[0]
familyMember.sizeToFit()
let imagePrefix = familyUnitArray[1]
print ("configureImageView called...")
let maxImageCnt = 10
if let imageView = familyPhotos {
print ("Processing images...")
let fileManager = FileManager.default
let path = Bundle.main.resourcePath!
print (#function,">Path> ",path)
let enumerator:FileManager.DirectoryEnumerator = fileManager.enumerator(atPath: path)!
var imageArr = [UIImage]()
var imageCnt: Int = 0
while let element = enumerator.nextObject() as? String {
print (#function,"> element.description",element.debugDescription)
let tmpStr = element.lowercased()
let tmpImagePrefix = imagePrefix.lowercased()
if (element.hasSuffix("png") || element.hasSuffix("jpg") || element.hasSuffix("jpeg"))
&& tmpStr.hasPrefix(tmpImagePrefix)
&& imageCnt < maxImageCnt {
let elementWithPath = path+"/"+element
imageArr.append(UIImage(contentsOfFile: elementWithPath)!)
imageCnt += 1
}
}
imageView.isUserInteractionEnabled = true
imageView.animationImages = imageArr
imageView.contentMode = .scaleAspectFit
imageView.backgroundColor = UIColor.white
imageView.animationDuration = 15
imageView.startAnimating()
}
}
}
You may refer here to for pausing the animation / not stopping it.
Is there a way to pause a CABasicAnimation?
In your case ,the code should be like the following, you may need to change a little to make it perfect.
#IBAction func pauseImage(_ sender: UIButton) {
if familyPhotos.layer.speed != 0 {
let time = familyPhotos.layer.convertTime(CACurrentMediaTime(), to: nil)
familyPhotos.layer.speed = 0
familyPhotos.layer.timeOffset = time
pauseButton.setTitle("Paused", for: [])
} else {
familyPhotos.layer.beginTime = familyPhotos.layer.timeOffset - CACurrentMediaTime()
familyPhotos.layer.timeOffset = 0
familyPhotos.layer.speed = 1
pauseButton.setTitle("Pause", for: [])
}
}
I try to implement search behavior like in Xcode: if you enter something in search field, icon changes color.
I delegate both searchFieldDidStartSearching and searchFieldDidEndSearching to controller and change the image.
The problem is icon's image changes only when window lose it's focus.
class ViewController: NSViewController {
#IBOutlet weak var searchField: NSSearchField!
func searchFieldDidStartSearching(_ sender: NSSearchField) {
print("\(#function)")
(searchField.cell as! NSSearchFieldCell).searchButtonCell?.image = NSImage.init(named: "NSActionTemplate")
}
func searchFieldDidEndSearching(_ sender: NSSearchField) {
print("\(#function)")
(searchField.cell as! NSSearchFieldCell).searchButtonCell?.image = NSImage.init(named: "NSHomeTemplate")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
}
Thanks in advance for any ideas/suggestions.
Although I don't know the reason, it works:
NSApp.mainWindow?.resignMain()
NSApp.mainWindow?.becomeMain()
Here is the whole code:
class MyViewController: NSViewController {
private lazy var searchField: NSSearchField = {
let searchField = NSSearchField(string: "")
if let searchButtonCell = searchField.searchButtonCell {
searchButtonCell.setButtonType(.toggle)
let filterImage = #imageLiteral(resourceName: "filter")
searchButtonCell.image = filterImage.tinted(with: .systemGray)
searchButtonCell.alternateImage = filterImage.tinted(with: .systemBlue)
}
searchField.focusRingType = .none
searchField.bezelStyle = .roundedBezel
searchField.delegate = self
return searchField
}()
...
}
extension MyViewController: NSSearchFieldDelegate {
func searchFieldDidStartSearching(_ sender: NSSearchField) {
sender.searchable = true
}
func searchFieldDidEndSearching(_ sender: NSSearchField) {
sender.searchable = false
}
}
extension NSSearchField {
var searchButtonCell: NSButtonCell? {
(self.cell as? NSSearchFieldCell)?.searchButtonCell
}
var searchable: Bool {
get {
self.searchButtonCell?.state == .on
}
set {
self.searchButtonCell?.state = newValue ? .on : .off
self.refreshSearchIcon()
}
}
private func refreshSearchIcon() {
NSApp.mainWindow?.resignMain()
NSApp.mainWindow?.becomeMain()
}
}
extension NSImage {
func tinted(with color: NSColor) -> NSImage? {
guard let image = self.copy() as? NSImage else { return nil }
image.lockFocus()
color.set()
NSRect(origin: NSZeroPoint, size: self.size).fill(using: .sourceAtop)
image.unlockFocus()
image.isTemplate = false
return image
}
}
I was having the same issue. A simple override fixed this issue for me
extension NSSearchField{
open override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
}
}
As you can see when you click inside the view it's still focussed on the search text field(as you can still type in it after you clicked underneath it). Since the change image is on when it loses focus, you should check if you clicked outside of the text field.
Solve problem by subclassing NSSearchFieldCell and assign this class to field's cell.
You don't even need to subclass NSSearchFieldCell.
When you create your NSSearchField from code, you can do something like this:
if let searchFieldCell = searchField.cell as? NSSearchFieldCell {
let image = NSImage(named: "YourImageName")
searchFieldCell.searchButtonCell?.image = image
searchFieldCell.searchButtonCell?.alternateImage = image // Optionally
}
If you're using storyboards, you can do the same in didSet of your #IBOutlet.
Here is the error that I am seeing.
The "cardButton" is responsible for showing the next question. This is a small card app game that I am trying to make and as you can see from the image this is where I am having the issue with the code.
Here is the code:
import UIKit
class MultipleViewController: UIViewController {
#IBOutlet weak var questionLabel2: UILabel!
#IBOutlet var answerButtons: [UIButton]!
#IBOutlet weak var cardButton: UIButton!
#IBAction func cardButtonHandle(_ sender: Any) {
cardButton.isEnabled = true
if questionIdx < count(mcArray) - 1 { // There are still more questions
questionIdx += 1 //
} else {
questionIdx = 0
}
nextQuestion()
}
#IBAction func answerButtonHandle(_ sender: UIButton) {
if sender.titleLabel?.text == correctAnswer{
sender.backgroundColor = UIColor.green
print("Correct!")
} else {
sender.backgroundColor = UIColor.red
print("Wrong Answer")
}
for button in answerButtons{
button.isEnabled = false
if button.titleLabel?.text == correctAnswer {
button.backgroundColor = UIColor.green
}
}
cardButton.isEnabled = true // next question
}
var correctAnswer: String? // correct answers
var answers = [String]() // answers
var question : String? // Questions
var questionIdx = 0 // not sure what this is ?
override func viewDidLoad() {
super.viewDidLoad()
// titleForButtons() // call buttons as soon its loaded..
cardButton.isEnabled = false
nextQuestion()
}
func nextQuestion (){
let currentQuestion = mcArray![questionIdx]
answers = currentQuestion["Answers"] as! [String]
correctAnswer = currentQuestion["CorrectAnswer"] as? String
question = currentQuestion["Question"] as? String
titleForButtons ()
}
func titleForButtons (){
for (idx,button) in answerButtons .enumerated() {
button.titleLabel?.lineBreakMode = .byWordWrapping
button.setTitle(answers[idx],for:.normal)
button.isEnabled = true
}
questionLabel2.text = question
}
}
The following should work, you did not have the correct syntax for the length of the array. Note that if you have not initialized your questions array, this would cause a crash. Therefore you might want to add a guard into your code. Perhaps use the following
#IBAction func cardButtonHandle(_ sender: Any) {
cardButton.isEnabled = true
if questionIdx < (mcArray!.count) - 1 { // There are still more questions
questionIdx += 1 //
} else {
questionIdx = 0
}
nextQuestion()
}
I have a tableView that has a UISwitch in it I am using notificationCenter to get the row of the UISwitch. How do I now use that info to turn off every other switch except the switch that I clicked?
func creatNotification() {
let switchNotification = Notification.Name("answer")
NotificationCenter.default.addObserver(self,selector: #selector(getSwitchRow),name:switchNotification, object:nil)
}
#objc func getSwitchRow(notification: NSNotification){
rowNumber = notification.object as! Int
print("dataView2, getSwitchRow, rowNumber:", rowNumber)
}
#IBAction func `switch`(_ sender: UISwitch) {
var dataViewObject = DataView2()
dataViewObject.creatNotification()
let switchNotification = Notification.Name("answer")
NotificationCenter.default.post(name: switchNotification, object: rowNumber)
let switchNotification2 = Notification.Name("switch")
NotificationCenter.default.post(name: switchNotification, object: answerSwitch.isOn)
if(answerSwitch.isOn == true){
}
}
you can define an attribute : var previousSwitch: UISwitch?
Then the Selector :
#objc func setOn(sender: UISwitch,indexPath: IndexPath){
if previousSwitch != nil {
previousSwitch?.isOn = false
}
let sw = sender
let isSetOn = sw.isOn
if (isSetOn){
print("on")
}else{
print("off")
}
previousSwitch = sw
}
and in Cell :
sw.addTarget(self, action: #selector(setOn(sender:indexPath:)), for: .valueChanged)
cell?.contentView.addSubview(sw)
I hope you can help.
There is a label and when any user swipe on it. It will make a call to core data and show values on label. Data will depend upon gesture. If its left/right data will be different on each time. Below is the code that i have written. Please suggest whether its correct or not?
class ViewController: UIViewController {
var helloArray = [Tasks]()
var currentArrayIndex = 0
#IBOutlet weak var textField: UITextField!
#IBOutlet weak var helloLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.handleSwipes(sender:)))
let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.handleSwipes(sender:)))
leftSwipe.direction = .left
rightSwipe.direction = .right
view.addGestureRecognizer(leftSwipe)
view.addGestureRecognizer(rightSwipe)
ouputData()
}
#objc func handleSwipes(sender: UISwipeGestureRecognizer) {
if sender.direction == .left {
currentArrayIndex = (currentArrayIndex + 1) % 3
}
}
func ouputData() {
do {
helloArray = try context.fetch(Tasks.fetchRequest())
for each in helloArray {
helloLabel.text = each.name
}
} catch {
}
appDelegate.saveContext()
}
#IBAction func btnPressed(_ sender: Any) {
let infoTasks = Tasks(context: context)
infoTasks.name = textField.text
appDelegate.saveContext()
do {
try context.save()
} catch {
print("Error")
}
textField.text = ""
}
}
I think you need to modify your handleSwipes and outputData function.
You outputData should only fetch all the data from CD in viewDidload.
Once you have the data source you can fetch item from source and populate your helloLabel based on index.
#objc func handleSwipes(sender: UISwipeGestureRecognizer) {
if sender.direction == .left {
currentArrayIndex = (currentArrayIndex + 1) % 3
} else if sender.direction == .right {
currentArrayIndex = (currentArrayIndex - 1) % 3
}
helloLabel.text = helloArray[currentArrayIndex].name
}
And:
func ouputData() {
do {
helloArray = try context.fetch(Tasks.fetchRequest())
} catch {
}
appDelegate.saveContext()
}
Hope it helps.