1

From a non-view controller class I want to pop up to the ui a custom like- toast message.

I want it to appear for 2 seconds.

I know I need to use dispatch_async twice. One for unhiding the message Second for hiding the message after 2 seconds. This requires dispatch_after()?

How do I call the main view-controller from a different class? calling self.showError() will not work, because self is not the main view-controller.

I have tried this:

    dispatch_async(dispatch_get_main_queue()) {
        self.showError()
    }
    dispatch_async(dispatch_get_main_queue(), ??? 2 seconds) {
        self.hideError()
    }
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Elad Benda
  • 35,076
  • 87
  • 265
  • 471
  • yes, you want `dispatch_after`. It performs blocks asynchronously after a given time period. Have a look at https://developer.apple.com/library/ios/documentation/Performance/Reference/GCD_libdispatch_Ref/#//apple_ref/c/func/dispatch_after. Also see http://stackoverflow.com/questions/4139219/how-do-you-trigger-a-block-after-a-delay-like-performselectorwithobjectafter?rq=1 – Hamish Jan 29 '16 at 14:34
  • thanks. jsut saw it's async as well. How do I call the main view-controller from a different class? calling `self.foo()` will not work – Elad Benda Jan 29 '16 at 14:36
  • well you need a reference to the main view controller. If it's the `rootViewController`, you can access it through the `AppDelegate`, or you can setup your own delegate for this. – Hamish Jan 29 '16 at 14:39
  • Thanks! not sure how to `setup your own delegate for this` will look into it – Elad Benda Jan 29 '16 at 14:45

0 Answers0