Open window from objective-c OSX - cocoa

so on start I sorry ;-)
I would open Window from my plugin/library
I want to use xib because is simpler than writing code and positioning.
Of course I have MyWindow.xib
NSWindowController *windowController = [[NSWindowController alloc] initWithWindowNibName:#"MyWindow"];
[windowController.window contentView];
window is not open
How to do it ?

Related

Animated App Icon for macOS

I have a question regarding macOS app icons. I've seen a number of animated icons before, but never really paid close attention to what was going on / how they were doing it. I was just wondering if there was any way to create an animated app icon that animates in the dock.
For example:
Is it possible to do this via an animated media file, or even programatically?
Years past, there is a new package called DSFDockTile.
Like this:
For a brief dock icon animation, you might loop the following code via a timer. (Untested Obj-C).
NSDockTile *dockTile=[[NSApplication sharedApplication] dockTile];
NSImageView *dockImageView=[[NSImageView alloc] init];
NSImage *iconImage=[NSImage imageNamed: ##MY_IMAGE_FRAME##];
[dockImageView setImage: iconImage];
[dockTile setContentView: dockImageView];
[dockTile display];

How to change the NSScreen a NSWindow appears on

I have an application that will load a couple of windows depending on which button is pressed. All except one of these open on the mainScreen (the screen in which the main window is open in). One of them (the preference window) opens on the first screen (the screen with the menu bar). I cannot understand way it is doing this, is there a way to change the screen that a NSWindow opens on?
I could not get toohtik's answer to work. What I ended up doing was subclassing NSWindow and then overriding constrainFrameRect: toScreen:. This will automatically open the new window on the "main screen" of the application.
- (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen
{
AppDelegate *delegate = [[NSApplication sharedApplication] delegate];
return [super constrainFrameRect:frameRect toScreen:delegate.window.screen];
}
I dont't know why you have that behaviour but you can change it through initWithFrame method that takes NSScreen argument.

NSTextField unable to gain focus on NSStatusItem when shown in NSPopover

It seems as if this has been a reported bug already (http://openradar.appspot.com/9722231), but is there a way to get around it? Or is there an alternative to the NSPopover that can be used to create the same kind of interface?
Thanks in advance
Here's some code:
Popover creation
popover = [[NSPopover alloc] init];
popover.contentViewController = popover_controller;
popover.appearance = NSPopoverAppearanceMinimal;
popover.animates = YES;
popover.behavior = NSPopoverBehaviorTransient;
popover.delegate = self;
Displaying the Popover
[popover showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMaxYEdge];
The sender is the NSStatusItem. The popover is displayed just fine, but if you try to click on the NSTextField, you are unable to edit the text or just gain focus in general.
So it turns out that this is a core OS bug and the only way around it was to use a custom NSWindow that looks like an NSPopover. I decided to use MAAttachedWindow.

Xcode: UINavigationController in a subview

I'm new in Xcode (and also here, in stack overflow) and I'm trying to build an application which contains a small UINavigationController (with a TableView inside) on the top of the window. So it should not be in full screen, it's just a little part of the GUI (just like a textField, or any other kind of component).
I've read that UINavigationController is designed to be displayed on the entire screen, but would it be possible to do it anyway?
If I can't, I'll probably have to write my own UINavigationController-like and TableViewController-like, with all transition effect (between 2 TableView) etcetera...
Thanks in advance for your help!
I founded the solution in a book and it's quite simple. I have to create the UINavigationViewController programmatically:
tableViewController = [[MyTableViewController alloc] initWithNibName:#"MyTableViewController" bundle:nil];
navCtrl = [[NavigViewController alloc] initWithRootViewController:tableViewController];
[navCtrl.view setFrame:CGRectMake(40, 40, 150, 200)];
[window addSubview:navCtrl.view];
[self.window makeKeyAndVisible];

Programmatic equivalent of "Visible at Launch" for NSWindows

I'm trying to write the programmatic equivalent of a nib file I've setup that contains two windows: a main window and sheet that appears after launch to prompt for credentials. Wiring these up in IB works fine, so long as one remembers to uncheck the "Visible at Launch" box on the sheet/window.
However I can't figure out what the API equivalent is of "Visible at launch". When I run my app using the programmatic version the sheet is detached and not the key view in the same way my app ran with the nib when "Visible at Launch" was checked. So my assumption, then, is that I'm missing the secret visible-at-launch sauce.
Does anyone know how to do this?
P.S. I know how to make this work in IB, I specifically want to figure out the code equivalent so please don't tell me to just use the nib. I know that.
NSWindows are typically created hidden. So you shouldn't have to do anything; just don't show the window until you need it. Here's a simple example.
NSWindow *sheetWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 100, 100) styleMask:NSTitledWindowMask backing:NSBackingStoreBuffered defer:NO];
NSTextField *field = [[NSTextField alloc] initWithFrame: NSMakeRect(25, 25, 50, 50)];
[[sheetWindow contentView] addSubview:field];
[NSApp beginSheet:sheetWindow modalForWindow:[self window] modalDelegate:self didEndSelector:#selector(sheetDidEnd:) contextInfo:NULL];
The text field obtained keyboard focus when I ran the above.
In future, please provide code in cases like this—it's a lot easier to correct existing code than to write new code.

Resources