I am trying to perform a segue that will be call form another class than ui view. The final target is that the main view will wait that an URL request is done to go to the next view.
Here my ui view:
class LoadingViewController: UIViewController {
#IBOutlet weak var activityIndicator: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
CardList.retrieveAllCards()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func goToNextView() {
performSegue(withIdentifier: "segueToMainController", sender: LoadingViewController.self)
}
class ShowNext {
class func view(fromViewController: LoadingViewController) {
fromViewController.goToNextView()
}
}
}
and here how I call my segue from the other class
let task = session.dataTask(with: request){
data,response,error in
do
{
if let jsonResult = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary {
jsonResultEnd = jsonResult
//print("SUCCESS:\(jsonResult)")
LoadingViewController.ShowNext.view(fromViewController: LoadingViewController())
print("loading ended")
}
} catch let error as NSError {
print("ERROR request manager: \(error.localizedDescription)")
}
}
Here is the error
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver () has no segue with identifier 'segueToMainController''
It doesn't make any sense to instantiate a new LoadingViewController and try to pass that into your class func. What you need to do is get a reference to your existing LoadingViewController and call goToNextView() on that.
There are many ways to do this, but one way would be to pass a reference to your LoadingViewController as a variable on your other view, and when your async call finishes you can call self.loadingViewController.goToNextView().
You could also use NotificationCenter to broadcast from your view that this async call is complete, and observe that notification on your LoadingViewController and use that event to trigger the segue:
In your LoadingViewController's viewDidLoad method:
NotificationCenter.default.addObserver(self, selector: #selector(goToNextView), name: Notification.Name("loadingComplete"), object: nil)
And in your other view's async callback:
NotificationCenter.default.post(name: Notification.Name("loadingComplete"), object:
Related
I want to receive the same callback in the ViewController that is opened at in the time that server response in my Swift Application.
I have two ViewControllers. The first ViewController registers a callBack from a class "NetworkService".
The second ViewController is Opened from the first ViewController and the second receives the "NetworkService" from the firstViewController initialized in a variable, and then registers the same callBack.
When I try to receive the callback from the server, if the first ViewController is opened I get the response. If I open the second ViewController and I resend the response I get this correctly in the second ViewController.
BUT if I return to the first ViewController and I get the response, its' only received on the Second ViewController all times.
class NetworkService {
var onFunction: ((_ result: String)->())?
func doCall() {
self.onFunction?("result")
}
}
class MyViewController: UIViewController {
let networkService = NetworkService()
override func viewDidLoad() {
super.viewDidLoad()
networkService.onFunction = { result in
print("I got \(result) from the server!")
}
}
}
I open the secondViewController like:
let vc = self.storyboard!.instantiateViewController(withIdentifier: "second") as! SecondViewController
vc. networkService = networkService
self.navigationController?.pushViewController(vc, animated: true)
And the Second ViewController:
class SecondViewController: UIViewController {
var networkService: NetworkService?
override func viewDidLoad() {
super.viewDidLoad()
networkService!.onFunction = { result in
print("I got \(result) from the server!")
}
}
}
How would it be possible to receive the response in the first ViewController again, then return to first ViewController from the second calling the popViewController?
self.navigationController?.popViewController(animated: false)
How about calling the function within viewDidAppear on both ViewControllers so that you get your response every time you switch between the two views? You wouldn't need to pass networkService between the ViewControllers.
override func viewDidAppear(_ animated: Bool) {
networkService!.onFunction = { result in
print("I got \(result) from the server!")
}
}
You can use notification but you will have to register and deregister VC as you switch between views. Other option is to use delegate, you will need to share NetworkService instance. Quick example of how this could work with protocol.
protocol NetworkServiceProtocol {
var service: NetworkService? { get }
func onFunction(_ result: String)
}
class NetworkService {
var delegate: NetworkServiceProtocol?
func doCall() {
self.delegate?.onFunction("results")
}
func update(delegate: NetworkServiceProtocol) {
self.delegate = delegate
}
}
class VC1: UIViewController, NetworkServiceProtocol {
var service: NetworkService?
init(service: NetworkService? = nil) {
self.service = service
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.service?.update(delegate: self)
}
func onFunction(_ result: String) {
print("On Function")
}
}
I have a view with a table on it, on every table cell I can add some data, so I put it in modal which actually is another view. In my modal I have this code
#IBAction func closeModal(_ sender: Any) {
if let amountVal = amount.text {
if let amountInt = Int16(amountVal) {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let activity = Activity(context: context)
activity.created_at = Date() as NSDate
activity.amount = amountInt
countedObject?.addToActivities(activity)
do {
try context.save()
navigationController?.popViewController(animated: true)
} catch let error {
NSLog(error.localizedDescription)
}
}
}
dismiss(animated: true, completion: nil)
}
So I update core data entity and close modal, but after closing modal the first view was not updated so it does not reflect the changes I made until restart of the simulator. In my first view I have this
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(_ animated: Bool) {
populateCountObjects()
}
This works for simple segues but not for modal, what should I use in this case?
So I did it in another way, I added to ViewController this
static var sharedInstace : ViewController?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
ViewController.sharedInstace = self
}
and in the modal after dismiss method
ViewController.sharedInstace?.didCloseModal()
Posible duplicate : duplicate
You need to use a delegate with a dedicated method
protocol ModalDelegate {
func didCloseModal();
}
In your modal controller class, create an instance of ModalDelegate protocol. And call 'delegate.didCloseModal()' before dismiss
Then make your parent controller implement ModalDelegate protocol
and implement the function didCloseModal like viewDidAppear
Let the system do the work for you. In this case you get a notification when the context is saved.
override func viewDidLoad() {
super.viewDidLoad()
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let center = NotificationCenter.default
center.addObserver(self, selector: #selector(contextDidSave(_:)), name: .NSManagedObjectContextDidSave, object: context)
}
func contextDidSave(_ notification: Notification) {
populateCountObjects()
}
If that doesn't work for you, you can always go lower and get notified whenever an object is changed.
override func viewDidLoad() {
super.viewDidLoad()
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let center = NotificationCenter.default
center.addObserver(self, selector: #selector(contextObjectsDidChange(_:)), name: .NSManagedObjectContextObjectsDidChange, object: context)
}
func contextObjectsDidChange(_ notification: Notification) {
populateCountObjects()
}
I guess your viewDidAppear method isn't called again. Try to use NsNotification to notify your first VC if the context saved and redraw/refresh/reload...etc what you want.
I have already looked in Stackoverflow but I can't get an answer. I want to create function that stop playing the sound in another ViewController. But when I clicked the stop button, it cracked and showed "EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)". This is my code.
First ViewController
import UIKit
import AVFoundation
class FirstVC: UIViewController {
var metronome: AVAudioPlayer!
override func viewDidLoad() {
super.viewDidLoad()
do {
let resourcePath1 = Bundle.main.path(forResource: "music", ofType: "mp3")
let url = NSURL(fileURLWithPath: resourcePath1!)
try metronome = AVAudioPlayer(contentsOf: url as URL)
metronome.prepareToPlay()
metronome.play()
} catch let err as NSError {
print(err.debugDescription)
}
}
and another Viewcontroller is
import UIKit
class SecondVC: UIViewController {
var metronomePlay = FirstVC()
#IBAction func stopBtnPressed(_ sender: Any) {
metronomePlay.metronome.stop() //"EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)"
}
}
As of swift 4.1 today, this code worked for me:
Put this in sending controller:
NotificationCenter.default.post(name: Notification.Name(rawValue: "disconnectPaxiSockets"), object: nil)
Put this in receiving controller viewDidLoad() or viewWillAppear():
NotificationCenter.default.addObserver(self, selector: #selector(disconnectPaxiSocket(_:)), name: Notification.Name(rawValue: "disconnectPaxiSockets"), object: nil)
and then the following function in your receiving controller class:
#objc func disconnectPaxiSocket(_ notification: Notification) {
ridesTimer.invalidate()
shared.disconnectSockets(socket: self.socket)
}
Swift 5:
Put this in the Action
NotificationCenter.default.post(name: Notification.Name("NewFunctionName"), object: nil)
Put this in viewdidload() in a different viewcontroller (where is the function you want to use)
NotificationCenter.default.addObserver(self, selector: #selector(functionName), name: Notification.Name("NewFunctionName"), object: nil)
The function
#objc func functionName (notification: NSNotification){ //add stuff here}
I hope I was helpful
You are creating a NEW copy of FirstVC and calling stop on something that is not yet initialised.
You should really use a delegate in this case, something like
protocol controlsAudio {
func startAudio()
func stopAudio()
}
class FirstVC: UIViewController, controlsAudio {
func startAudio() {}
func stopAudio() {}
// later in the code when you present SecondVC
func displaySecondVC() {
let vc = SecondVC()
vc.delegate = self
self.present(vc, animated: true)
}
}
class SecondVC: UIViewController {
var delegate: controlsAudio?
// to start audio call self.delegate?.startAudio)
// to stop audio call self.delegate?.stopAudio)
}
So you are passing first VC to the second VC, so when you call these functions you are doing it on the actual FirstVC that is in use, rather than creating a new one.
You could do this without protocols if you like by replacing the var delegate: controlsAudio? with var firstVC: FirstVC? and assigning that, but I wouldn't recommend it
I use this way to call my functions from another viewControllers:
let sendValue = SecondViewController();
sendValue.YourFuncion(data: yourdata);
You can call function from other viewControllers in many ways.
Two ways that are already discussed above are by delegates & protocols and by sending notifications.
Another way is by passing closures to your second viewController from firstVC.
Below is the code in which while segueing to SecondVC we pass a closure to stop the metronome.
There will be no issue because you are passing the same firstVC (not creating a new instance), so the metronome will not be nil.
class FirstVC: UIViewController {
var metronome: AVAudioPlayer!
override func viewDidLoad() {
super.viewDidLoad()
do {
let resourcePath1 = Bundle.main.path(forResource: "music", ofType: "mp3")
let url = NSURL(fileURLWithPath: resourcePath1!)
try metronome = AVAudioPlayer(contentsOf: url as URL)
metronome.prepareToPlay()
metronome.play()
} catch let err as NSError {
print(err.debugDescription)
}
let secondVC = SecondVC()
secondVC.stopMetronome = { [weak self] in
self?.metronome.stop()
}
present(secondVC, animated: true)
}
}
class SecondVC: UIViewController {
var metronomePlay = FirstVC()
var stopMetronome: (() -> Void)? // stopMetronome closure
#IBAction func stopBtnPressed(_ sender: Any) {
if let stopMetronome = stopMetronome {
stopMetronome() // calling the closure
}
}
}
var metronomePlay = FirstVC()
you are creating a new instance on FirstVC, instead you should perform the function on the same instance that of already loaded FirstVC.
Updating #Scriptable's answer for Swift 4
Step 1 :
Add this code in your view controller, from which you want to press button click to stop sound.
#IBAction func btnStopSound(_ sender: AnyObject)
{
notificationCenter.post(name: Notification.Name("stopSoundNotification"), object: nil)
}
Step 2:
Now its final step. Now add this below code, to your result view controller, where you want to automatically stop sound.
func functionName (notification: NSNotification) {
metronomePlay.metronome.stop()
}
override func viewWillAppear(animated: Bool) {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "functionName",name:"stopSoundNotification", object: nil)
}
You are initialising metronome in viewDidLoad method of FirstVC.
In SecondVC, you are initialising metronomePlay as a stored property, but never asking for ViewController's view and thus viewDidLoad of FirstVC is not getting called which results in metronome(stored property) not getting initialised.
You initialize metronome on FirstVC in viewDidLoad, which won't happen until you load the view of metronomePlay instantiated in SecondVC.
You have to call _ = metronomePlay.view, which will lazily load the view of SecondVC and subsequently execute viewDidLoad, before actually calling metronomePlay.metronome.
Try this in SecondVC. var metronomePlay = FirstVC().metronome
Either use the notification process to stop from anywhere or use same FirstVC instance from SecondVC class.
I created a class that implements NSURLProtocol which will tells me about the UIWebView requests. I am looking to tell the UI that we hit a URL of interest and run code back on the ViewController to access the WebView.
I believe Protocols are the solution but cant seem to wrap my head around how to get this to work. Any suggestions and code example would be much appreciated. Here is what I've tried so far.
UI View Controller.swift
class WebViewController: UIViewController,WebAuthDelegate {
#IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "http://example.com")
let request = NSURLRequest(URL: url!)
webView.loadRequest(request)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func onBackClick(sender: AnyObject) {
if(webView.canGoBack){
webView.goBack()
}
}
#IBAction func onFwdClick(sender: AnyObject) {
if(webView.canGoForward){
webView.goForward()
}
}
#IBAction func onRefresh(sender: AnyObject) {
webView.reload()
}
func getUserToken() {
print("RUN THIS AFTER I HIT URL IN URL PROTOCAL CLASS")
}
}
Here is my NSURLProtocol implemented class along with the attempted protocol(which please tell me if its the wrong approach)
class CUrlProtocol: NSURLProtocol {
//let delegate: WebAuthDelegate? = nil
override class func canInitWithRequest(request: NSURLRequest) -> Bool {
print("URL = \(request.URL!.absoluteString)")
if request.URL!.absoluteString == "http://api-dev.site.com/token" {
//Tell the UI That we now have the info and we can access the UIWebView Object
}
return false
}
}
protocol WebAuthDelegate{
func getUserToken()
}
The best way to achieve this would probably be to post an NSNotification from your protocol when you match the URL.
In CUrlProtocol, when you find a match (notification name can be your choosing):
let notification:NSNotification = NSNotification(name: "MyURLMatchedNotification", object: nil)
NSNotificationCenter.defaultCenter().postNotification(notification)
In your WebViewController:
// During init (or viewDidAppear, if you only want to do it while its on screen)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "getUserToken", name: "MyURLMatchedNotification", object: nil)
...
// During dealloc (or viewWillDisappear)
NSNotificationCenter.defaultCenter().removeObserver(self, name: "MyURLMatchedNotification", object: nil)
You can also pass something with your notification, if you need information from that request. Just set the object parameter when you create your notification and change your getUserToken to accept a single parameter that has a type of NSNotification and access its object property.
I'm building an app with a container view holding a tableView controller. I create this tableView, but I don't know how to access this object again so I can call function on it. Currently there is a BucketTableViewController object being created automatically (maybe from the storyboard). Then later I want to call a function on it and create another BucketTableViewController object. I can verify they are unique with print statement on that method. How do I set a variable for an object that is the original object?
import UIKit
class FirstViewController: UIViewController {
var bigArray = ["M", "A", "R", "C"]
let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
override func viewDidLoad() {
super.viewDidLoad()
reachForWebsite()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func reachForWebsite(){
let url = NSURL(...)
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
do {
...
// HERE IS THE ISSUE
var bucketsVC = BucketTableViewController()
bucketsVC.updateBuckets(self.bigArray)
} catch let myJSONError {
print(myJSONError)
}
}
task!.resume()
}
}
You can grab a reference to it from prepareForSeque(_:sender:) in the view controller that owns the container. Make sure that identifier matches the name of the identifier you've set on the segue from the storyboard in Interface Builder. Or you can omit the identifier part if you know for certain that there are no other segues with destination's of type BucketTableViewController.
class BucketTableViewController: UITableViewController {}
class FirstViewController: UIViewController {
var bucketViewController: BucketTableViewController!
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
super.prepareForSegue( segue, sender: sender )
if let vc = segue.destinationViewController as? BucketTableViewController where segue.identifier == "embeddedBuketViewcontroller" {
self.bucketViewController = vc
}
}
}
A comment is too tight for this, so I'm making it an answer. You can make bucketsVC` an instance variable:
class FirstViewController: UIViewController {
var bucketsVS : BucketTableViewController?
func reachForWebsite(){
...
do {
self.bucketsVC = BucketTableViewController()
self.bucketsVC!.updateBuckets(self.bigArray)
} catch {
...
}
// Now you can use it anywhere within your UIViewController
}
}