Search code examples
cocoasizesubclassviewcontrollernstextfield

Communicating between subclassed nstextfield and view controller in swift


I've run into an issue where I need to communicate between a class that is a subclass of nstextfield and the view controller I am using in a cocoa swift project.

The subclassed nstextfield changes the size of it's frame. The field itself is within a table within the view controller. Essentially, when the nstextfield changes its frame size, I want the view controller to become aware of that. I'm able to signal to the view controller that the frame has changed via the use of Nsnotification Centre where an observer in the view controller listens for the notification, that works fine. But now when that notification is triggered, I need to view controller to return to the subclassed nstextfield in order to retrieve the size of the frame. I don't believe that I can pass for example a frame size with the notification message.

Can anyone give me an idea as to how I retrieve the frame size of the nstextfield from within the view controller?

Thanks for your assistance in advance.


Solution

  • When you post your notification you can specify an object, in this case the textfield. The viewcontroller can retrieve the textfield from notification.object and ask for its frame.

    If the viewcontroller needs more info, you can use -postNotificationName:object:userInfo:, userinfo is a dictionary in which you can store whatever you want.