Construct and manage a graphical, event-driven user interface for your macOS app using AppKit.

AppKit Documentation

Post

Replies

Boosts

Views

Activity

Binding NSArrayController to an NSMutableArray not working
I have an NSMutableArray defined as a property in my ViewController class: @interface ViewController () @property NSMutableArray *tableCities; @end When the "Add" button is clicked, a new city is added: NSString* filePath = @"/tmp/city_test.jpeg"; NSDictionary *obj = @{@"image": [[NSImage alloc] initByReferencingFile:filePath], @"name": @"testCity", @"filePath": filePath}; [_tableCities addObject: obj]; Okay, this is fine, it is adding a new element to this NSMutableArray, now what I want is to somehow bind the "name" field of each city to my NSPopUpButton. So in storyboard I created an NSArrayController and tried to set its "Model Key Path" to my NSMutableArray as shown below: Then I tried to bind the NSPopUpButton to the NSArrayController as follows: but it doesn't work either, a city is added but the NSPopUpButton isn't displaying anything, what gives?
4
0
239
Oct ’24
drawing NSView in Sequoia
In genealogy software, I have an NSView that displays a family tree where each person is in a subview. Before Sequoia, the subviews would draw in the order I add them to the parent NSView. For some reason, Sequoia calls drawRect in reverse order. For example, a tree with cells for son, father, and mother used to be drawn in that order, but Sequoia draws them as mother, father, and son. For static cell placement it would not matter, but these trees dynamically adjust as users change data or expand and contract branches from the tree. Drawing them in the wrong order messes up all the logic and many trees are corrupted. Has the new drawing order been implemented for some reason and can it be changed? Is it documented?
1
0
216
Oct ’24
Possibly Incorrect Statement in AppKit Release Notes for macOS 14.
While trying to debug some weird drawing issues under macOS 14, I remembered that there was a comment in the AppKit Release notes related to drawing and NSView.clipsToBounds. AppKit Release Notes for macOS 14 Under the section titled NSView, the following statement is made: For applications linked against the macOS 14 SDK, the default value of this property is true. Apps linked against older SDKs default to false. Some classes, like NSClipView, continue to default to true. Is this statement possibly backwards? From what I can tell, under macOS 14 NSView.clipsToBounds now defaults to false. I came across this while trying to debug an issue where views that override drawRect with the intent of calling NSFillRect(self.bounds) with a solid color are, sometimes, briefly flickering because self.bounds is NSZeroRect, even though self.frame is not (nor is the dirtyRect). This seems to be happening when views are added as subviews to a parent view. The subviews, which override drawRect, periodically "miss" a repaint and thus flicker. This seems to happen when views are frequently added or removed, like what happens in a scrolling view that is "recycling" views as they go offscreen. Views that scroll into the viewport are added as subviews and, sometimes, briefly flicker. Replacing calls to drawRect with wantsUpdateLayer and updateLayer eliminates the flickering, which makes me think something is going astray in drawRect and the various rects you can use. This is with Xcode 15.4, linking against macOS 14.5 and running on macOS 14.6.1
2
0
278
Oct ’24
Sending events to VZVirtualMachineView
I'm trying to send various user input events to a virtual machine. The app is built in SwiftUI, so the VZVirtualMachineView is part of a view controller wrapped in NSViewControllerRepresentable. Non-modified keystrokes and mouse clicks work fine, but I can't send modified keystrokes (e.g., ⌘F). These come through without the modifier (e.g., plain F). I also haven't been able to get mouse scroll wheel events to do anything in the VM. I installed a local event monitor with addLocalMonitorForEvents(matching:handler:). Before the VM boots and the VM view exists, typing myself generates events that I see with the monitor. After the VM boots, though, the monitor does not see any of my keystrokes. The monitor never sees any of my programmatically generated events. I am sending these all through NSWindow.sendEvent(_:). Is there anything special about VZVirtualMachineView that might affect how it handles injected events? It's obvious possible (likely) that I'm doing something wrong with how I build and/or send these events into the application. Can anyone point me to documentation or examples that aren't easily found through search engines?
8
0
321
Oct ’24
Binding NSPopUpButton to an NSMutableArray
My view controller has this property: @property NSMutableArray *tableCities; Whenever I press a button a new city object is added to this array, it consists of a dictionary of NSStrings containing the name and state of the city being added. Now, I have an NSPopUpButton that I'd like to bind to the city name of all cities in this NSMutableArray, how exactly can I achieve this with the Bindings Inspector?
2
0
245
Oct ’24
could someone or an apple dev help me get data on an tableview in objective c?
I understand the mvc thing kinda, theres an array as a model, a tableview as a control, and the interface as a control (right?) so like, I drag an array controller on to the form display thing, assign a delegate to it the view and app module/header, add data to it and the tableview updates after I add a function to it? in vs the events are in the event browser, do I just copy and paste the code from the tutorial? how do I add controls and do all the advanced stuff? the documentation on developer.apple.com isnt that detailed, and most books are outdated or n/a since its objective c tutorials would be nice, some home grown method would be cool too thank you unidef warrell yashizzo
2
0
307
Oct ’24
Unable to update NSProgressIndicator for the dock Icon
I have created a progress indicator to simulate some progressing download task in the dock icon. However, I can see the progress bar appearing in the dock icon but it is not getting updated when I invoked the updateProgress() method. Ideally it should have updated it, and I m not able to figure out the reason? I have creating the same NSProgressIndicator on an NSWindow and it works to update the progress bar with the same code. Anything that I m missing to understand here? Below is the code I m using: class AppDelegate: NSObject, NSApplicationDelegate { var progressIndicator: NSProgressIndicator! let dockTile = NSApp.dockTile func applicationWillFinishLaunching(_ notification: Notification) { // Step 1: Create a progress bar (NSProgressIndicator) progressIndicator = NSProgressIndicator(frame: NSRect(x: 10, y: 10, width: 100, height: 20)) progressIndicator.isIndeterminate = false progressIndicator.minValue = 0.0 progressIndicator.maxValue = 100.0 progressIndicator.doubleValue = 0.0 progressIndicator.style = .bar dockTile.contentView = progressIndicator dockTile.display() //// Update the progress bar for demonstration DispatchQueue.main.asyncAfter(deadline: .now() + 1) { self.updateProgress(50) } } func updateProgress(_ value: Double) { progressIndicator.doubleValue = value NSApp.dockTile.display() } }
0
0
222
Oct ’24
NSAlert accessory view
I need to add an accessory view with a text field to an NSAlert. This works as expected but as my app comes in different languages, the alert looks a bit different depending on the language used: So I wonder how I can make the text field automatically use the full width of the alert window. I tried to call the alert's layout method and then resize the text field but this seems not work in all cases. Any help would be highly appreciated. Thanks!
0
0
206
Oct ’24
Applying Custom Rounded Corners to a macOS Window
Which method allows me to apply larger-than-default rounded corners to a macOS window while maintaining the system’s dynamic, contrasting border – similar to what’s seen in the Control Center popup? (Control Center has a corner radius of 16, the default system window is 10.) While I know how to closely achieve this effect manually for my plain panel, I'd like to leverage the built-in method and get it for free from the OS. I’ve spent way more time on this problem than I'm willing to admit, and searched extensively, but haven’t found any solution. I’d really appreciate any pointers. Thank you.
4
2
421
Oct ’24
Updating a NSTableView
For a Cocoa project I have this table view controller: @interface TableViewController : NSObject <NSTableViewDataSource, NSTableViewDelegate> @property (nonatomic, strong) NSMutableArray *numbers; @property (nonatomic, strong) NSMutableArray *letters; @end #import "TableViewController.h" @implementation TableViewController - (NSMutableArray *)numbers { if (!_numbers) { _numbers = [NSMutableArray arrayWithArray:@[@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10"]]; } return _numbers; } - (NSMutableArray *)letters { if (!_letters) { _letters = [NSMutableArray arrayWithArray: @[@"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", @"j"]]; } return _letters; } // NSTableViewDataSource Protocol Method - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView { return self.numbers.count; } // NSTableViewDelegate Protocol Method -(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row { NSString *identifier = tableColumn.identifier; NSTableCellView *cell = [tableView makeViewWithIdentifier:identifier owner:self]; if ([identifier isEqualToString:@"numbers"]) { cell.textField.stringValue = [self.numbers objectAtIndex:row]; } else { cell.textField.stringValue = [self.letters objectAtIndex:row]; } return cell; } In the storyboard I created a NSTableView and set up the connection, after running it I get what I expected: okay, cool, but I have a question: why is this working even though I haven't created a NSTableView as a property for my ViewController or for my TableViewController? Also, there's another problem, now I want to add a button to this window such that, every time I click it, a new item should be added to this table(for now it can be anything) So I created a button and a method for this and linked the action to the story board: -(IBAction) addNewNumber:(id)sender { [_numbers addObject:@"1234"]; [_letters addObject:@"testing"]; } Now, every time I click this button I can see that this method is indeed being called and that indeed IT IS adding a new member to these arrays, the thing is, the table is not being updated, what gives? Any advice on how I can fix this behavior?
1
0
291
Oct ’24
NSApplicationDelegate open URLs only called after second drop
I want to make a simple droplet application. I've set the document types to include com.adobe.pdf files, and I am now receiving callbacks to the app delegate's application(_:open:) callback when I drop PDFs on the app icon… But not the first time. It doesn't matter how long I wait, or whether the app is already open—the first drop never triggers the callback. All subequent drops work as expected, and I get URLs to all the files dropped. What might be wrong? How can I debug this?
1
0
281
Oct ’24
Cocoa application duplicated view
I have a Cocoa application and I'm trying to set up an NSImageView without using story board (using storyboard works just fine tho). Also, I'm kinda new to Cocoa so any advice is appreciated. Anyway so here's the deal, I created a class to encapsulate this image: @interface MyView : NSView { @private NSImageView* imageView; } @end @implementation MyView -(id) initWithFrame:(NSRect)frameRect { self = [super initWithFrame:frameRect]; if(self) { NSRect rect = NSMakeRect(10, 10, 100, 200); imageView = [[NSImageView alloc] initWithFrame:rect]; NSImage* image = [NSImage imageNamed:@"bob.jpeg"]; [imageView setImageScaling:NSImageScaleNone]; [imageView setImage: image]; [self addSubview: imageView]; } return self; } -(id) init { return [self initWithFrame:NSMakeRect(10, 10, 100, 100)]; } - (void)drawRect:(NSRect)dirtyRect { [super drawRect:dirtyRect]; // Drawing code here. } @end Now, in the view controller file, in the viewDidLoad I tried to load add this as a subview to get it to display my image: - (void)viewDidLoad { [super viewDidLoad]; MyView *customView = [[MyView alloc] init]; [self.view addSubview:customView]; } This kinda works, except that it loads the image twice, I ended up with two images instead of just one like I intended, what gives? what am I doing wrong here?
1
0
270
Oct ’24
Setting value of NSSlider resets mouse cursor
Hi, in my macOS app I am modifying the mouse cursor image for some user interactions. I also have an NSSlider in the app which can be changed programmatically by setting its doubleValue. I've noticed that whenever the slider is set programmatically the custom mouse cursor is lost and changes back to an arrow. This doesn't happen if other controls are changed programmatically, e.g. the progress bar. Any ideas on how I can prevent this happening?
0
0
200
Sep ’24
NSRuleEditor only allow criterion row to be added once
I have an object that is an NSRuleEditorDelegate for an NSRuleEditor whose nestingMode is NSRuleEditorNestingModeList. There are 8 different possible criteria. Each criterion is optional but at least 1 is required (ruleEditor.canRemoveAllRows = NO). Each criterion should only be added once. How can I limit adding a criterion for a row if it is already in the editor at a different row? Thanks!
0
0
209
Sep ’24
NSMenuToolbarItem not showing first menu item when using NSMenuDelegate
I am using NSMenuToolbarItem to show a drop-down menu in my NSToolbar. This works as expected when creating an NSMenu beforehand and assign it to the menu property of NSMenuToolbarItem. However, my menu needs to be built dynamically when the user clicks the dropdown button. To do that, I am using NSMenuDelegate. When creating the menu in the menuNeedsUpdate of the delegate, the first menu item isn't shown to the user. Why? Menu when using delegate: Menu when pre-assigning menu: I also cannot just add a placeholder menu item at the start of the NSMenuToolbarItem as all menu items do show in the overflow menu. Example code: import Cocoa class AppDelegate: NSObject, NSApplicationDelegate, NSToolbarDelegate, NSMenuDelegate { var window: NSWindow! func applicationDidFinishLaunching(_ aNotification: Notification) { window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 400, height: 300), styleMask: [.titled, .closable, .resizable], backing: .buffered, defer: false) window.makeKeyAndOrderFront(nil) let toolbar = NSToolbar(identifier: "MainToolbar") toolbar.delegate = self window.toolbar = toolbar } func toolbarAllowedItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] { return [NSToolbarItem.Identifier("item1"), NSToolbarItem.Identifier("item2")] } func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] { return [NSToolbarItem.Identifier("item1"), NSToolbarItem.Identifier("item2")] } func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, willBeInsertedIntoToolbar: Bool) -> NSToolbarItem? { let item = NSMenuToolbarItem(itemIdentifier: itemIdentifier) if itemIdentifier == NSToolbarItem.Identifier("item1") { let menu = NSMenu() fillMenuWithItems(menu) item.menu = menu } else if itemIdentifier == NSToolbarItem.Identifier("item2") { item.menu = NSMenu() item.menu.delegate = self } return item } func menuNeedsUpdate(_ menu: NSMenu) { menu.removeAllItems() fillMenuWithItems(menu) } func fillMenuWithItems(_ menu: NSMenu) { menu.addItem(NSMenuItem(title: "Option 1", action: nil, keyEquivalent: "")) menu.addItem(NSMenuItem(title: "Option 2", action: nil, keyEquivalent: "")) } }
1
0
228
Sep ’24
ViewBridge to RemoteViewService Terminated (...)
After updating to Sonoma, the following is logged in the Xcode console when an editable text field becomes key. This doesn't occur on any text field, but it seems to happen when the text field is within an NSPopover or an NSSavePanel. ViewBridge to RemoteViewService Terminated: Error Domain=com.apple.ViewBridge Code=18 "(null)" UserInfo={com.apple.ViewBridge.error.hint=this process disconnected remote view controller -- benign unless unexpected, com.apple.ViewBridge.error.description=NSViewBridgeErrorCanceled} What does this mean?
4
1
653
Sep ’24
Failed to find localization for values: All, of the following are true
After each macOS update come new annoying log messages. So after updating to Sonoma and Xcode 16, I see the following logged in the Xcode console whenever my app shows an NSPredicateEditor. Failed to find localization for values: All, of the following are true I tried setting the "localize" attribute to "never" in the attributes inspector of the corresponding menu/text fields from interface builder, to no avail. Any suggestions ?
0
0
195
Sep ’24
Possible to have NSWindow *without* NSWindowStyleMaskTitled to make the screen its on the main screen?
I have a NSWindow subclass. The window has a custom shape, and thus has a custom contentView which overrides drawRect to draw . Since the window has a custom shape it cannot use the system provided titlebar. The problem I'm having is when there are multiple screens, if my window is on the inactive screen (not mainScreen with menu bar) and I move the mouse over to the second monitor and click the window....the menu bar doesn't travel to the screen my app is on after the window is clicked. This does not happen with any other window. In all other windows, the menu bar moves to the screen once you click a window on that screen. So it appears this is because my window is not using NSWindowStyleMaskTitled. As far as I know, I can't use the system title bar and draw my custom window shape. Abandoning the custom window shape is not an option. Without going into too many details as to why I care, the menu bar really should travel with first click on my window like other apps.. Is there a way to tell the system (other than using the NSWindowStyleMaskTitled) that clicking on my window should make that screen the "main screen" (bring the menu bar over? I tried programmatically activating the application, ordering the window to the front, etc. but none of this works. This forces the user to click outside my app window, say on the desktop, to move the menu bar over, which feels wrong. Thanks in advance if anyone has any suggestions.
4
0
316
Sep ’24