cocoa

Encoding string arguments for URLs

匆匆过客 提交于 2020-01-11 12:20:53
问题 I created a method to build URLs for me. - (NSString *)urlFor:(NSString *)path arguments:(NSDictionary *)args { NSString *format = @"http://api.example.com/%@?version=2.0.1"; NSMutableString *url = [NSMutableString stringWithFormat:format, path]; if ([args isKindOfClass:[NSDictionary class]]) { for (NSString *key in args) { [url appendString:[NSString stringWithFormat:@"&%@=%@", key, [args objectForKey:key]]]; } } return url; } When I try to build something like below, the URLs aren't encoded

Finding where a method was called from

社会主义新天地 提交于 2020-01-11 10:17:27
问题 How would I find out where a method was called from? Like you see in a stack trace. Basically, just to explain, I am observing a core data property and the method that gets called when the property changes calls another method (the IBAction) but in this IBAction it adds Core Data objects which triggers the KVO method which triggers the IBAction and so forth. That is why I was trying to figure out where the method was called from so I could stop this infinite loop. 回答1: You can't without going

Properly move an object to the Trash

a 夏天 提交于 2020-01-11 08:42:42
问题 It looks like on Cocoa there are many ways to move file/folder-directory to Trash: [[[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation] [[NSWorkspace sharedWorkspace] recycleURLs:] [NSFileManager trashItemAtURL:] [NSFileManager removeItemAtPath:] [NSFileManager removeItemAtURL:] It would be nice to understand what the difference is by either reading an explanation here or a link to the official Apple docs. Also if someone knows a universal way of moving a file/non

load nibs in cocoa

孤街醉人 提交于 2020-01-11 06:35:35
问题 How can I load a nib inside of another window? I tried initWithWindowName, if (mmController == NULL) mmController = [[mainMenu alloc] initWithWindowNibName:@"mainMenu"]; [mmController showWindow:self]; but it opens a new window. I also tried loadNibNamed [NSBundle loadNibNamed:@"mainGame" owner:self]; and it succeeded, but when I try to use the same method to get back to the main menu, [NSBundle loadNibNamed:@"mainMenu" owner:self]; it doesn't work. It does nothing at all... Any ideas? 回答1: I

Loading a Localized UIImage

。_饼干妹妹 提交于 2020-01-11 05:23:29
问题 I need to load localized images in my iOS app, but also have to take into account that they might need to be the ...@2x kind. How can I do this? 回答1: Corrected Answer [UIImage imageNamed:@"TestImage"] looks first into Resources, if nothing found, it then looks in the lproj dir folders. So you only need to put the localized images into the right folder. Old Answer NSLocalizedString should do the trick. See Localizing String Resources. UIImage *img = [UIImage imageNamed:NSLocalizedString(@

NSPredicateEditor in Xcode 4

被刻印的时光 ゝ 提交于 2020-01-11 05:17:10
问题 Bit of an issue with Xcode 4's predicate editor controls - I think I'm doing everything right, and it really seems like the IDE itself is busted. I'm having this issue in an app I'm writing, but to isolate it, I did the following: Create a new project with a window. In the XIB editor, add an NSPredicateEditor to it, and add one line. Leave it as key paths/strings and add two key paths - "title" and "writer". Make it case and diacritical insensitive. Create a subclass of NSWindowController and

How do you toggle the status item in the menubar on and off using a checkbox?

孤者浪人 提交于 2020-01-11 04:20:29
问题 I have already created a status item for the menu bar but I would like to add a checkbox to make it able to be toggled on and off. So when the check box is checked the status item is displayed and when the checkbox is not checked it is not displayed. What code would i need to do this? 回答1: First in your controller class create an instance variable to hold the reference to this item: NSStatusItem *item; Then create a method to create this status item, when the box is checked: - (BOOL

How to dim os x desktop using Cocoa/Core Animation

跟風遠走 提交于 2020-01-10 20:01:14
问题 Is there a way to control the brightness of the OS X desktop programmatically using Cocoa or Core Animation? 回答1: In case you were actually interested in doing a fade-out or fade-in of the whole screen, here is a link describing how to do that. The linked document contains examples for how to fade all displays (or a single display) to black (you can change the color), capture the display (where you can display what you'd like), and then fade back. This uses CGAcquireDisplayFadeReservation()

Change NSNumberFormatter's negative format from (xxx.xx) to -xxx.xx

不问归期 提交于 2020-01-10 19:26:55
问题 I want to change my NSNumberformatter from displaying negative numbers with parenthesis around them to putting the minus sign in front (or whatever the localized standard is). I would assume I could do this with setNegativeFormat: but reading Apple's oh so thorough docs I am left scratching my head: setNegativeFormat: Sets the format the receiver uses to display negative values. - (void)setNegativeFormat:(NSString *)aFormat Parameters aFormat A string that specifies the format for negative

NSOutlineView example

我是研究僧i 提交于 2020-01-10 13:37:19
问题 Please, could you give a simple complete example of Cocoa application with NSOutlineView with hierarchical data representation not so ambiguous like NSOutlineView and NSTreeController example . Thanks! 回答1: This example uses a simple two layer hierarchy of two parents Foo and Bar who each have two kids, Foox, Fooz and Barx, Barz respectively, the outline view is the default one with just two columns and the identifier of the second column is set to "children". NSDictionary *firstParent = @{@