Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Franz83

macrumors newbie
Original poster
Oct 15, 2020
6
0
Hello!
I'm new to Obj-C and I need to communicate to a NSCustomView (NSView) a change an integer var and refresh the content of the custom view... Any idea?
I'm using XIB interface...

Thanks
 

Senor Cuete

macrumors 6502
Nov 9, 2011
430
31
Typically you would link either the field with the integer or an OK button to a class method that would be invoked when the user edited the field or hit the button. You would declare a class method in the class' .h file and include an implementation in the class' @implementation in the .m file. Set the File's owner in the .xib to your class. Then you would link the interface object to the method implementation by option dragging from it in the .xib's object to the method in the .xib received actions sub-pane. When the user clicks on the button or edits the field the method in your class is invoked. Call DrawRect or some other method to in your @implementation to update the NSView.
 
Last edited:

Franz83

macrumors newbie
Original poster
Oct 15, 2020
6
0
Thanks... I found the solution:
1. import the NSView class in AppDelegate.m (something like #import "Curve.h")
2. create @Property IBOulet (nonatomic, weak) of the NSView (like *customView)
3. from the .xib file create a @Property (nonatomic, weak) in @interface and a @IBAction in @implementation of AppDelegate.m from the button (or any controlctrl-dragging the object into the code
4. in the AppDelegate.h IBAction communicate to the methods of the NSView clwriting in the function with something like [self.customView customViewMethod:argument];
5. finally in the IBAction execute [self.customView setNeedsDisplay:YES];
Et voilà!

I hope this could help someone else...
 

Senor Cuete

macrumors 6502
Nov 9, 2011
430
31
I neglected to mention that the you need to declare the interface object in your .h file as an
Code:
IBOutlet NSinterfaceObject type *name;
and the target method should be declared as
Code:
- (IBAction) methodName: (id) sender;
in your @interface in the .m file of your .xib's file owner.
 

Franz83

macrumors newbie
Original poster
Oct 15, 2020
6
0
Mhm... ok, but do you think that your method is better than my solution?
 

Senor Cuete

macrumors 6502
Nov 9, 2011
430
31
The way I described it is more general. When you have a window it will typically have a bunch of controls and other UI elements that need to be manged by your class that is the owner of the .xib. The class can have a bunch of methods to manage them.

Also the class can manage the state of the UI objects by receiving notifications like this in its init method:

Code:
- (id)init
{
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
   [nc addObserver: self selector: @selector(methodName:) name: @"notification" object: nil];
    return self;
}
 
Last edited:

Franz83

macrumors newbie
Original poster
Oct 15, 2020
6
0
Excuse me for the passed time...
I have some more questions:
first, what kind of class has to be the File's Owner class? Where it has to be derived from? What kind of structure it must have?
second, if I use an init method, can I add more observers with the same NSNotificationCenter?
 

Senor Cuete

macrumors 6502
Nov 9, 2011
430
31
"...what kind of class has to be the File's Owner class? Where it has to be derived from? What kind of structure it must have?..."

It will be a subclass of NSWindowController.

You can include as many addObserver statements as you want. Be sure to remove the class from the notifications center in the class dealloc method like this:
Code:
- (void)dealloc
{
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc removeObserver: self];
    [super dealloc];
}

You might not have to do this if you are using ARC. A lot of windows in a macOS application are of type NSPanel.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.