-14

I have 3 buttons on a view controller and a collection view on another view controller. I want to display corresponding images on collection view which corresponds to button clicked. I have buttons A, B,C and and like to display images A1 to A4 on another view controller if the button A is clicked and so on. I am new to swift and would appreciate if you can let me know if there is a way to do this.

pedrouan
  • 12,762
  • 3
  • 58
  • 74
alvin44
  • 1
  • 2
  • 3
    Welcome to Stack Overflow. Try to post what you've tried first. The public will not want to type the code for you. Show eventual troubles you are having and you are likely to get good answer. Please edit. – pedrouan Aug 25 '16 at 10:09
  • Is your image data static? I mean do you know exactly which images to be shown? – Vishal Sonawane Aug 25 '16 at 10:09
  • Use notification Center. – Bista Aug 25 '16 at 10:09
  • @VishalSonawane, yes I have array of images from A1 to A4 for button A, B1 to B4 for button B and C1 to C4 for button C – alvin44 Aug 25 '16 at 10:13
  • Study the properties and passing values between view controllers. – Raj Aggrawal Aug 25 '16 at 10:14
  • @RajAggrawal , I was only able to find info for passing data between two collection views or from table view to collection views. I would appreciate if you can direct me to a link where they pass data between buttons and collection view – alvin44 Aug 25 '16 at 10:17
  • @alvin44 Take a property array in viewcontroller2. and show these image on collection view. Now Pass the images you want to show on click of the button from ViewController1 to ViewController2. – Raj Aggrawal Aug 25 '16 at 10:20
  • http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – Raj Aggrawal Aug 25 '16 at 10:21
  • 2
    @Mr.UB appreciate you for introducing me to NSNotification Center and understanding what I need to achieve result for my scenario :) – alvin44 Aug 25 '16 at 10:23
  • @RajAggrawal Appreciate the link and your comment – alvin44 Aug 25 '16 at 10:32
  • And where will be your other viewController? – Vishal Sonawane Aug 25 '16 at 10:34

2 Answers2

1

Swift 4

First, add a notification in each button action:

NotificationCenter.default.post(name: Notification.Name("buttonAIsPressed"), object: nil)

now add a notification listener in the viewDidLoad of the collection view class and call the function in the selector:

 NotificationCenter.default.addObserver(self, selector: #selector(self.YourFunctionName), name: Notification.Name("buttonAIsPressed"), object: nil)

now whenever a button is pressed the function you named will be called.

mahdi
  • 439
  • 3
  • 15
-2

You need to use closure or global observer.

In view did load

weekCalendar.calendarclosure = { (calendar) in
  code
}

In function to send

var calendarclosure: ((NSDate) -> ())?
Derek Wang
  • 10,098
  • 4
  • 18
  • 39
Piotr
  • 175
  • 1
  • 2
  • 11