I tried many ways but it seems I just cannot get it working.
// AppDelegate
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
true // false does not work either
}
// MainWC
func windowShouldClose(_ sender: NSWindow) -> Bool {
print(#function)
if sender === NSApp.mainWindow {
print("is main window")
sender.setIsVisible(false)
return false
}
return true
}
Once I hide the main window (using setIsVisible or orderOut) the app quits; even if I return false from applicationShouldTerminateAfterLastWindowClosed, NSApp.mainWindow will become nil.
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
I have a custom Objective-C ObjectCache class that utilized NSMutableDictionary to store named objects (specifically large NSImage objects).
The class has a maxItems property and clear method, which worked fine.
- (void)clear
{
@synchronized(_cache)
{
[_cache removeAllObjects];
}
}
But yesterday I received a customer feedback complaining one of my app consumes up to 24GB memory.
I verified that on my M1 Mac mini, and it's true. Even after calling clear method, memory usage shown in both Xcode debugger panel and Activity Monitor remains the same.
It did work before. Once I close a picture gallery window by calling clear method, memory usage dropped immediately as shown in Activity Monitor.
I suspect that this is a bug in ARM architecture. Does anyone else have same problem?
What unit does NSView.bounds/frame use? Pixel or point? How to convert between them?
I am no expert at coordinate systems. I am kind of aware I can use its enclosingScrollView but don't know the details.
Any suggestions would be appreciated.
I have an uncommon scenario here.
outer tableview
+--------------------------+
| column 1| inner tableview|
+--------------------------+
Now most often the out tableview has many rows and vertical scrollbar visible.
When user try to scroll vertically in the inner tableview but it has no vertical scrollbar (because it has only a few items), I want the scroll event sink into its parent view or better outer tableview, so that user does not have to move cursor to first column in outer tableview and scrolls.
Is this possible?
I have a tableview with a column which has an inner tableview. I want to change height of the outer tableview to match the height of the inner tableview.
outerTableView.reloadData()
let range = outerTableView.rows(in: summaryTableView.superview!.visibleRect)
outerTableView.noteHeightOfRows(withIndexesChanged: IndexSet(integersIn: range.lowerBound..<range.upperBound))
The above code will cause an exception:
WARNING: NSTableView detected a rowView was requested from inside of the -heightOfRow delegate method. That is not supported!
(
0 CoreFoundation 0x000000019093eccc __exceptionPreprocess + 176
1 libobjc.A.dylib 0x0000000190426788 objc_exception_throw + 60
2 AppKit 0x000000019420be98 -[NSTableRowData _availableRowViewWhileUpdatingAtRow:] + 0
3 AppKit 0x000000019425a470 -[NSTableView viewAtColumn:row:makeIfNecessary:] + 32
I accidentally removed the Fixed Width preset item in the font panel (by dragging it out).
Is there any way to get it back?
Does "natural scrolling" system preference affect NSEvent.scrollingDeltaY?
BTW, I find that "natural scrolling" on my Sonoma (Mac mini m1) stops working; turning on/off has no effect when I scroll in Finder app.
Is there any way to keep an NSWindow always on top of other windows (inside an app)?
I want to create a "preview" window for pictures and videos with it not taking up space in the main window.
Does Swift/objc provide builtin support for reading/writing xcstrings, like PropertyListEncoder/PropertyListDecoder?
Today the Connect page asks me to confirm if I am a trader or not under DSA.
I am an independent developer and have several apps in App Store. I have no idea what a trader is.
The DSA defines a trader as “any natural person, or any legal person irrespective of whether privately or publicly owned, who is acting, including through any person acting in his or her name or on his or her behalf, for purposes relating to his or her trade, business, craft or profession.” If you have questions about your status as a trader, consult with your legal advisor.
I emailed Connect support but their reply is very "official" which confuses me more.
Instruments reveals that the following simple code leaks memory on each delegate invoke:
- (void)start {
[_urlSession dataTaskWithURL:_url];
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
[_data appendData:data];
}
I don't have any clue on this leak, it should not happen. What should I do to avoid this?
I need to create timers in another thread (not on main):
@implementation AppDelegate {
NSThread* _worker;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
_worker = [[NSThread alloc] initWithTarget:self selector:@selector(main) object:nil];
[_worker start];
}
- (void)main {
[NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(timer_tick:)
userInfo:nil
repeats:YES];
[NSThread sleepForTimeInterval:INFINITY];
NSLog(@"**********************");
}
- (void)timer_tick:(NSTimer*)timer {
NSLog(@"%@", NSThread.currentThread);
}
The timer never gets fired. What's wrong with my code?
I have the following code:
+ (BOOL)activateWindow:(NSWindow*)window
{
if (NSApp.activationPolicy != NSApplicationActivationPolicyRegular)
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
if (window)
{
[NSApp activate];
//if (window.isMiniaturized) [window deminiaturize:nil];
[window makeKeyAndOrderFront:nil];
}
return YES;
}
+ (BOOL)hideWindowFromDock:(NSWindow*)window
{
if (NSApp.activationPolicy != NSApplicationActivationPolicyProhibited)
[NSApp setActivationPolicy:NSApplicationActivationPolicyProhibited];
window.isVisible = NO;
return YES;
}
I hide app main window by setting NSApplicationActivationPolicyProhibited if it is minimized or being closed.
The code worked most of the time. But since upgrading to Sonoma, sometimes it won't correctly activate main window. The window icon reappears in the dock, but the window won't show up. I have to click on the window icon again to let it 'order front'.
Sometimes, I observe a very weird behavior of the window being activated. It 'order front' and then disappears and re-appears.
Is there any way to tell if the user clicked X button in windowWillClose delegate method?
- (void)windowWillClose:(NSNotification *)notification