• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: method in controller class invoked from NSView
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: method in controller class invoked from NSView


  • Subject: Re: method in controller class invoked from NSView
  • From: "Louis C. Sacha" <email@hidden>
  • Date: Fri, 9 Jul 2004 03:24:14 -0700

Hello...

In addition to using key value observing as Larry mentioned, there are also a few other routes you could take.


If the view and the controller are tightly coupled (in other words, if any time there is an instance of your view class it will be working with an instance of the controller), there isn't any reason why the view shouldn't have an outlet that points to the controller, and then it could call the method directly and pass the necessary information.


If you want to maintain the separation between the view and the controller so that they are more adaptable and can be reused in different situations, you could also use something similar to the target/action paradigm, where the view has a reference to the target (your controller) and the selector for the action (some method in your controller). Generally the action is a method that takes a single argument, the sender of the message. If you did it that way, the only thing you would need to do is add a method to your view that provides the necessary information.

So it would work something like this:

0) At some point, perhaps awakeFromNib, you set the target and action in the view to point to your controller and its method.

1) The user clicks on the view.
2) The view calls the action method in the target (your controller).
3) The controller requests the information it needs from the view using the method you added.
4) The controller does whatever it needs to do.


If the relationship between the view and the controller is less direct, you could use notifications. When the view is clicked on, you could send a notification that contains the info you need to pass in the userInfo parameter.

That would work something like this:

0) Your controller registers as an observer for the notification in its initializer instance method or in awakeFromNib.

1) The user clicks on the view.
2) The view posts the notification with itself as the object, and stores the necessary information in the userInfo dictionary or expects the observer to call the method to get the information like was done for the target/action way.
3) The method that the controller registered as a callback for the notification is triggered, and the controller does whatever it needs to do.


Hope that helps,

Louis

I have a controller class and a NSView. I need to
invoke a method from the controller class with some
info from the NSView, when the user clicks on it. I
already implemented the click and I obtain the info. I
just need to invoke the method with this info as
parameters...

I thought a solution could be to have a global
variable that was continually checked by the
controller class in an infinite loop and invoke the
method when the variable changes but I'm sure there's
got to be a better way.

Thanks!
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


References: 
 >method in controller class invoked from NSView (From: Luis E! <email@hidden>)

  • Prev by Date: Re: split view
  • Next by Date: HeaderDoc 8.0 issue
  • Previous by thread: Re: method in controller class invoked from NSView
  • Next by thread: Usb
  • Index(es):
    • Date
    • Thread