Custom view from NSWindowController Cocoa - objective-c

I have an application with two windows, the main window opens the second window that is a NSWindowController and in its xib file there is a custom view, is there any way to draw in this custom view from NSWindowController?
thanks

Override - (void) drawRect:(NSRect) dirtyRect in your custom NSView to do the drawing.
If you need to inform this drawRect method from your (custom) NSWindowController, you can use delegate or data source patterns by setting an outlet from the view to the NSWindowController.

solved, I have declared two IBOutlets, one on NSView:
IBOutlet MyNSWindowController *wc;
and one on NSWindowController:
IBOutlet MyNSView *view;
then, I have to connected them to custom view.
Now I can to use its methods simply calling its IBOutlets.

Related

Can I load nib contents in to UIView using only Interface Builder?

Does anyone know a way to, in a storyboard, load a UIView's contents from another nib? I know I can do this easily with code, but I am trying to figure out how to do more in IB.
I have a storyboard with my main UI layout, I have a UIScrollView and I want to design its contents in IB. The only way I could figure out how to do this was to design the UIView in its own .nib, but then my issue is, how do I load the nib without coding it to do so? Is this even possible? It doesn't seem too far fetched to me.
I'm assuming you simply want to lay out your UIScrollView in IB, that a .nib is mentioned because that was an approach you were exploring, but if you could do this strictly in your storyboard that would be acceptable, if not preferable:
First, create a new file in Xcode that is a subclass of UIScrollView.
In your storyboard, drag a UIScrollView out to the scene (viewcontroller) where you want to display this scroll view.
In the Identity inspector, set the Custom Class of the UIScrollView to your subclass of UIScrollView.
Create an outlet for this UIScrollView by ctrl+dragging the UIScrollView into the .h file of the ViewController subclass it's displayed in. Name it something like myScrollView
In your ViewController's -viewDidLoad method, set the contentSize property of the UIScrollView to whatever size you want it to be. So it will look something like:
self.myScrollView.contentSize = CGSizeMake(800,800);
Now, drag out UI objects to your UIScrollView and design.
IMPORTANT: To create outlets to these objects is a little tricky. Let's say you've dragged out a UILabel. You need to manually go into your UIScrollView subclass and add to the .h
#property (nonatomic, weak) IBOutlet UILabel* myLabel;
and to the .m
#synthesize myLabel = _myLabel;
Now you need to get your outline view on screen along with your storyboard and ctrl+drag FROM YOUR SCROLL VIEW TO YOUR LABEL to create an outlet. This is kind of the reverse of what you're used to.
Now you can reference that outlet from within the viewcontroller or the scrollview subclass . For instance, in the viewcontroller -viewDidLoad you could say:
self.scrollView.myLabel.text = #"Hello World";
HTH!
If what you want is to edit inside a scrollview from IB, it's a pain, but doable.
Have a look at my answer on this question.
Add a generic UIView in the IB, setting its custom class to the name of your nib file.
Replace GradientControl with the name of your nib file (minus the '.xib').

ios UIview in views

I have a uiview that has all my buttons, that has its controller. How can I include the button view in all my uiviews using the interface builder? The buttons must work when click. The purpose is to increase maintainability.
You can create a custom UIView in interface builder like this:
Loading custom UIView from nib, all subviews contained in nib are nil?
Then add your custom view to other views in IB by adding a normal UIView and changing the class to your subclass.
Try this. I'm not sure that adding UIView in Interface Builder will work, but adding subview programmatically always works.

(BOOL)windowShouldClose:(id)sender doesn't work in view that i set as contentview of mainmenu's window

I set a NSViewController's view as contentview of mainmenu's window,now how should i use - (BOOL)windowShouldClose:(id)sender ?
i use <NSWindowDelegate> in #interface and put - (BOOL)windowShouldClose:(id)sender on .m but doesn't work
-(BOOL)windowShouldClose:(id)sender only gets sent to the delegate of the window that will be sending the message. (if you're not crystal clear on what delegates are, they're kind of like the army commander that everybody reports to)
Here, in order to be able to use -windowShouldClose: in your NSViewController, you need to set the NSViewController as the window's delegate. There's two steps to this:
Make it possible for the 'NSViewController' to be the window
delegate. Usually the window's delegate is the NSWindowController
(it's named that for a reason), but if you want to make
'NSViewController' the delegate you need to use the
<NSWindowDelegate> protocol, which you've already done.
Actually set the view controller as the delegate. As Ken Thomases
noted in his comment, you can do this in Interface Builder by
dragging the window's delegate outlet and connecting it to your
NSViewController. Or you could do it programmatically:
[self.view.window setDelegate:self] as you mentioned should work.

How do I set up a UITableView element within a UIViewController with other UI elements?

I am using Storyboards. I have a UI ViewController and within it, I have a bunch of labels and buttons before the UITableView.
I know how to do it using UITableViewControllers, which have the datasource protocols built in with them. How do I implement the same using a UIViewController with UITableView? Do I use IBOutlets to access the UITableView? How do I populate it from there?
The implementation of the methods is the same as it would be in a UITableViewController.
The only difference is that if you are adding a table view manually, you need to connect the datasource and delegate outlets yourself (and declare that you implement the protocols). This is done for you if you use UITableViewController, but that comes with the price of not being able to add other views.
In the .h file of your view controller subclass, declare that you implement the protocols:
#interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDatasource>
In storyboard, select the table view, open the outlets inspector, drag from the delegate and datasource outlets to your view controller object.

Redrawing an NSView

Sorry if this has been asked before or it's a really dumb question, but I can't figure it out. I have an NSView in my interface, and I have created a subclass of NSView in Xcode. Then using the identity inspector, I set my NSView's class to be the newly created NSView subclass. The view draws fine, but now I need to redraw it to change a string inside the view. I'm pretty sure this has to do with setNeedsDisplay, but what do I send the message to? I don't have a particular instance of my view in the code, since it's in Interface Builder, so what do I do?
Again, sorry if this is dumb. I haven't done much with NSView yet. Ask for more info if you need it. Thanks!
In the view controller subclass you have, add an ivar with type of your NSView subclass. Declare a property on it, and mark it as an outlet.
// ViewControllerSubclass.h
ViewType *myView;
#property(readwrite, assign) IBOutlet ViewType *myView;
// ViewControllerSubclass.m
#synthesize myView;
Now you have an outlet, connect it to the view you designed via IB. To do so, right click in IB on your view controller subclass (the file's owner), you should see the outlet in the list.
Once you have done that, you are now able to send messages to the view in your code.
To mark the view as needing redraw :
[myView setNeedsDisplay:YES];