How can I save the SwichButton setting? [duplicate] - ios

This question already has an answer here:
Swift 4 save from the background color that was consulted in the App
(1 answer)
Closed 4 years ago.
I've got 4 SwichButton, this is standard to Off.
How can I save the setting when a switch button is set to on ?
I have this:
#IBAction func SwitchNofic(_ sender: UISwitch) {
let switchTag = sender.tag
if (switchTag == 1) && (sender.isOn == true){
print("1")
createNoficationMorgen()
}else if (switchTag == 2) && (sender.isOn == true){
print("2")
createNoficationMittag()
}else if (switchTag == 3) && (sender.isOn == true){
print("3")
createNoficationAbend()
}else if (switchTag == 4) && (sender.isOn == true){
print("4")
createNoficationNacht()
}
}

It depends if you want to save it just for the time the app is alive.
Just use a boolean variable so you know what is on and what is not, also each switchButton has a property isOn.
If you want it to be persistant try UserDefaults.
Swift 4
To save permanently :
UserDefaults.standard.set(true, forKey: “isDarkModeKey”)
To retrieve (this should be called in viewDidAppear):
let isDarkModeEnabled = UserDefaults.standard.bool(forKey: “isDarkModeKey”)
if isDarkModeEnabled {
mySwicth.setOn(true, animated : false)
//set the background to dark
} else {
mySwicth.setOn(false, animated : false)
//set the background to white
}

Related

is that possible to trigger another gestureRecognizer in one gestureRecognizer (in swift)?

if i click image long then func actionLongPress get started and as you can see, if i click image long then i want to move that image along my touch... but this code doesn't work...
there is no error. even though i click image long, image doesn't follow my touch..
do you fix this trouble???
#objc func actionLongPress(Recog : UILongPressGestureRecognizer){
if Recog.state == .began{
AudioServicesPlaySystemSound(1519)
addPanGesture(view: addedImage)
origin = addedImage.frame.origin
} else if Recog.state == .ended{
AudioServicesPlaySystemSound(1519)
}
}
You are missing one state that you need to implement ... which is state change
#objc func actionLongPress(Recog : UILongPressGestureRecognizer){
if Recog.state == .began{
AudioServicesPlaySystemSound(1519)
addPanGesture(view: addedImage)
origin = addedImage.frame.origin
} else if Recog.state == .changed {
// assign view frame here
let location = Recog.location(in: self)
your_view?.center = targetPosition
} else if Recog.state == .ended{
AudioServicesPlaySystemSound(1519)
}
}

Cannot assign value of type 'Bool' to type 'UIButton?'

I am trying to hide a button, until another condition is met. When i load the app i am using the slider and when it reaches the max, it should make the button visible, but for some weird reason it won't. I have just done it in another app with no issues and the code is almost identical.?
Can't figure out what to do tbh.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
profileVisible.isHidden = true
}
#IBAction func btnClicked(_ sender: Any) {
motionManager.startDeviceMotionUpdates(to: queue) { (motion, error) in DispatchQueue.main.async {
self.slider.value = Float((motion?.attitude.roll ?? 0 ) * 1.4)
print(self.slider.value)
if self.slider.value == 1.0 {
self.profileVisible = false
self.motionManager.stopDeviceMotionUpdates()
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate); // vibrates when payment has succeded
}
if self.slider.value == -1.0 {
profileVisible = true
self.motionManager.stopDeviceMotionUpdates()
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
}
}
}
You miss setting here
if self.slider.value == 1.0 {
self.profileVisible = false
And here
if self.slider.value == -1.0 {
profileVisible = true
Should be
profileVisible.isHidden = true/false // set it's value according to your logic
profileVisible is of type UIButton you need to set it's isHidden property not it directly

Switch visual not changing

I am using Swift3 and have the following variable str8redpickteamreminder which is either 1 or 0. I also have a switch called whichthe code can can be seen at the bottom of the question.
I thought I could use the following code which would change the visuals of the switch:
if str8redpickteamreminder == 1 {
self.teamSelectNotif = true
}
else {
self.teamSelectNotif = false
}
Yet the switches just stay as on when the code is run. How can I make the switch linked to teamSelectNotif show as false in the app programatically?
Any help is appreciated.
Switch Code:
#IBAction func `switch`(_ sender: UISwitch) {
if sender.isOn == true {
switch sender.tag {
case 1:
teamSelectNotif = true
print(toggleUpdate(toggle: "str8redpickteamreminder", setting: 1))
case 3:
resultsNotif = true
print(toggleUpdate(toggle: "str8redresults", setting: 1))
default:
break
}}
else if sender.isOn == false {
switch sender.tag {
case 1:
teamSelectNotif = false
case 3:
resultsNotif = false
default:
break
}}
}

IOS navigationController?.popToRootViewController has wrong

#IBAction func addInformation(_ sender: UIBarButtonItem) {
// check rateHourly has value
if let editedRateHourly = rateHourly.text {
print("editedRateHourly condition is \(editedRateHourly)")
if editedRateHourly != ""{
print("not nil")
// check edit value is number?
let num = Int(editedRateHourly)
if num != nil {
print("is num")
// add to database
UserDefaults.standard.set(editedRateHourly, forKey: "\(findDate())")
UserDefaults.standard.synchronize()
// back to last viewController
navigationController?.popToRootViewController(animated: true)
}else{
print("not num")
print("error alert push!!")
popErrorAlert()
}
}else {
print("nil")
print("editedRateHourly condition is nil")
popErrorAlert()
}
}
}
#IBAction func cannelInformationPage(_ sender: UIBarButtonItem) {
navigationController?.popToRootViewController(animated: true)
}
I want to create a new simple edit page. It's two problem for me that when I finish edition if-else will check the condition is correct or not and then save the data popToRootViewControlle. When I finish edition I click on "addInformation" BarButtonItem and I get UI wrong. the Other wrong is when I click on editField but I don't enter any condition. And then I click on "cannelInformationPage" UIBarButtonItem. It also get wrong.
It's what I get wrong
wrong information
Because it returns Bool
_ = navigationController?.popToRootViewController(animated: true)
_ = self.navigationController?.popViewController(animated: true)
If you want to come back to last view controller than you can try above line, hope its work for you.

MPMoviePlayerController Doesn't Play when receiving a remote control event?

So I've got a MPMoviePlayerController playing a video in the background.
If I tell it to load a different video using remote control notifications it works just fine.
However if I tell it to play the video it doesn't play?
Has anyone else had this problem or found a solution?
Code snippet:
override func canBecomeFirstResponder() -> Bool {
return true
}
override func remoteControlReceivedWithEvent(event: UIEvent!) {
if (event.type == UIEventType.RemoteControl){
if (event.subtype.toRaw() == 100 || event.subtype.toRaw() == 101){
didPressPausePlay(self)
}else if(event.subtype.toRaw() == 104){
didPressNext(self)
}else if(event.subtype.toRaw() == 105){
didPressPrevious(self)
}
}
}
#IBAction func didPressPrevious(sender: AnyObject) {
videoTitle.text = ""
if (currentIndex != 0){
currentIndex--
currentVideo = parsedVideoIds[currentIndex] as NSString
videoPlayerViewController = XCDYouTubeVideoPlayerViewController(videoIdentifier: currentVideo);
videoPlayerViewController.moviePlayer.backgroundPlaybackEnabled = true;
videoPlayerViewController.presentInView(self.view);
videoPlayerViewController.moviePlayer.controlStyle = MPMovieControlStyle.None
self.view.bringSubviewToFront(customControls);
videoPlayerViewController.moviePlayer.play()
currentImage = 0
pauseplayButton.setImage(pauseImage, forState: UIControlState.Normal)
}
}
I left out the rest because it all does the same thing.

Resources