Can not stop music when back to first view controller [closed] - ios

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I've build a second view controller for a radio app to send text's to the radiostation. So, when i press play on the first view controller it play's the stream and you can check what's playing now and coming up. I've made a button to the second view controller, the music is still playing, that's good! But i can't stop it in ControlCenter and when i go back to the first view controller the button is back to "Play" and not "Stop" in the app.
Seems like the first view controller is getting a "reset?" when you go to the second view controller. How can i fix this?
Image: http://imgur.com/wZtAyz6
Dennis

You can also declare your audioPlayer in the appDelegate :
// inside AppDelegate
var ap : AVAudioPlayer? = nil
then simply declare in your viewController :
var ap : AVAudioPlayer? {
get {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
return appDelegate.ap
}
set {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.ap = newValue
}
}

Related

Using a function to move a user between View Controllers [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I currently have the following function, once clicked, it takes the user to the ‘homeViewController’
func transitionToHome() {
let homeViewController = storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewController) as? HomeViewController
view.window?.rootViewController = homeViewController
view.window?.makeKeyAndVisible()
}
However, I am looking to use this function when a user logs out to take them to the ‘UIViewController’, however whenever I use UIViewController instead of homeViewController in the above code, it produces the following error.
Type 'Constants.Storyboard' has no member 'UIViewController'
What do I need to do in order to make this work and take the user to the very first viewcontroller for the app?
Here’s the view of the view controller I am trying to redirect the user to.
You need to change the Class value from ViewController to HomeViewController, as well as the Storyboard ID to HomeViewController for the initialisation to work.
Custom Class
Class HomeViewController
Identity
Storyboard ID HomeViewController
Then can instantiate the view controller like this:
func transitionToHome() {
let homeViewController = storyboard?.instantiateViewController(identifier: "HomeViewController") as? HomeViewController
view.window?.rootViewController = homeViewController
view.window?.makeKeyAndVisible()
}

making and presenting app tutorial views in Xcode

I have an app that is not super intuitive. I want to take screenshots of the confusing screens and then use MSpaint to write instructions and doodles. When the user opens the view in the app for the first time, I want to present the series of altered screenshots along with an "OK" button. pressing OK will dismiss the screenshot and it will not be shown again. Is there an efficient way to do this? I am new to Swift and Xcode. Any help would be appreciated
You need to use NSUserDefaults to save the state of the app (tutorial shown or not) - NSUserDefaults saves data between the runs of the app to the device storage.
Then you need to change AppDelegate to change the initial view controller according to the value you saved - that way if tutorial has been shown it will not show again.
Assuming you have var called toturialShown
Set it to false and each run check its value to determine if the tutorial needs to be shown
When the user taps on dismiss tutorial button use NSUSerDefaults to save this new status
Store
UserDefaults.standard.set(toturialShown, forKey: "toturialShownKey")
Retrieve
UserDefaults.standard.bool(forKey: "toturialShownKey")
Remove - in case you want to delete it completely from storage
UserDefaults.standard.removeObject(forKey: "toturialShownKey")
On AppDelegate in applicationDidFinishWithOptions function
(Note that I didn't test the code)
var vc = ""
If toturialShown {
vc = "regularVC"
} else {
vc = "toturialVC"
}
let initialViewController = mainStoryboard.instantiateViewController(withIdentifier: vc)
let initialViewController = mainStoryboard.instantiateViewController(withIdentifier: "LoginSignupVC")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
Note1: you need to add identifiers to you VCs in storyboard
Note 2: if you set your initial VC (on storyboard) to the regular VC, you can change the code above to set the initial VC programmatically only if the tutorial has not been shown, results in slightly more elegant code

Programmatically return to first controller on event of THAT first controller [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
In my Swift project, ViewController #1 has a route of destinations listed in a table. User is driving along and we keep track of where user is using the built-in CLLocation stuff. But user has now navigated away from ViewController #1 to some other portion of the app and is now in ViewController #2. A certain event that I track in ViewController #1 fires (in my example; the user has reached the next destination on his list). I need the app to immediately segue back to ViewController #1 right when that event fires. How can it be sensed when user is in ViewController #2?
NSNotificationCenter and an Unwind Segue seems to be the way to go, this way this functionality can be reused, all you would need to do is register for a given notification on the relevant view controllers.
Something along the lines of:
class ViewControllerOne: UIViewController {
let CallForUnwindSegue = "com.yourIdentifier.unwindToVCOne"
//- your event happens
SomethingHappened {
NSNotificationCenter.defaultCenter().postNotification(CallForUnwindSegue, object: nil)
}
}
class ViewControllerTwo: UIViewController {
let CallForUnwindSegue = "com.yourIdentifier.unwindToVCOne"
override func viewDidLoad() {
super.viewDidLoad()
//- Subscribe for notifications
NSNotificationCenter.defaultCenter().addObserver(self, #selector(self.doSomething), name: CallForUnwindSegue)
}
//- Unsubscribe for notifications
override func deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
func doSomething(notification: NSNotification) {
//Handle your event here.
}
}
There's a good unwind segue article here

iOS - Programatically load view on app didFinishingLaunching

This question is mostly about how I should structure my app. When the app loads the user can select from a list of videos to play, however the actual video player is on a different view controller. At the moment I am just keeping that view controller in memory so that the video can play continuously while the user is navigating throughout the app.
So the problems is that if the user selects a video before loading that movie view controller, nothing will happen of course.
How should I structure my app so that the video can play continuously whether or not the movie player view controller is held in memory? Is this possible?
I'm not 100% sure what you're trying to do; but a default project created in XCode will "Programatically load view on app didFinishingLaunching" without any further coding needed.
Generally you should structure your app with an initial view or view controller (like a navigation controller); and start any visible actions in the "viewDidLoad" method of the target view.
In your case, I would recommend loading straight to the video controller first; and then programmatically segueing to the video selector view if no video is selected.
Usually, I create a structure like this in my projects, I think you need to take a special look in OverlayViewController, check out a example:
MainViewController - As rootViewController
|- content (Any view controller to be presented, yes inside MainViewController)
|- OverlayViewController (a view controller over content )
This structure allows you to change contents without change entire hierarchy.
At MainViewController you will need a method to change current content to another, there is a simple example of how can you do that:
// PS I'm not sure if this will work in the first try, I wrote from my mind right now :)
func changeViewController (controller: UIViewController) {
let from: UIViewController
if childViewControllers.count > 0 {
from = childViewControllers.first as! UIViewController
}
else {
presentViewController(controller)
return
}
let transitionContext = SomeViewControllerContextTransitioning (
fromViewController: from,
toViewController: controller
)
transitionContext.onAnimationComplete = { success in
if !success {
// TODO: Error fallback
}
else {
from.view.removeFromSuperview()
from.removeFromParentViewController()
controller.didMoveToParentViewController(self)
}
}
from.willMoveToParentViewController(nil)
addChildViewController(controller)
SomeAnimator().animateTransition(transitionContext)
}
And finally, in your MainViewController you can observe or control your video playback.
I hope this can help you.

NSNotification for UIAlertController [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is there an option to observe and get the information about appearing and disappearing?
I want to grayscale my elements like apple ui-elements by appearing of UIAlertController!
Since now i found out that the "_UIBackdropViewComputeAndApplySettingsNotification" was called and contains userInfo about the appearing view.
You are going to make the UIAlertController's view appear, so how can you not know? You don't need to observe it; you're doing it (by calling presentViewController...).
That takes of what happens when the alert appears. What about when it disappears? Well, it disappears because the user tapped a button. You get to write the handler for every button in the alert. So again, you know when the alert is disappearing, because your handler is running.
The solution is, everything works automatically. You just have to implement..
override func tintColorDidChange() {
self.setNeedsDisplay()
}
..and of course working with the tintColor
Thanks matt for quick answer!
To expand on the other answers: each of your UIView subclasses should implement tintColorDidChange to be notified of the change.
Here's a sample implementation:
class someLabel : UILabel {
override func tintColorDidChange() {
let isInactive = self.tintAdjustmentMode == UIViewTintAdjustmentMode.Dimmed
if (isInactive) {
// modify subviews to look disabled
self.textColor = UIColor.grayColor()
} else {
// modify subviews to look enabled
self.textColor = self.tintColor
}
}
}
A few other good code samples (albeit in Objective-C) can be found in this SO question.

Resources