My question is simple and easy; How do i get NSPopover to be first responder from the NSStatusbar?
I'm asking for a short code, not a link to a big XCode-project. Thanks.
Though you've probably solved your question by now, you can do this:
[yourPopover becomeFirstResponder];
If you want more control, just setup your containing controller for a NSPopoverDelegate.
[yourPopOver setDelegate:self]; /* Don't forget including the <NSPopoverDelegate> in your headerfile */
Then you can use 5 functions to gain more control over your popover.
– popoverShouldClose:
– popoverWillShow:
– popoverDidShow:
– popoverWillClose:
– popoverDidClose:
Related
Apple makes NSTextView respond to page up, page down, arrow keys, etc. automatically, but the home and end keys are not automatically handled by NSTextView out of the box. There's no apparent reason for this; I just logged a Radar on it. Until they fix that Radar, the question is: how do I make my NSTextView handle those keys correctly?
I just spent a little while googling around about this, and didn't find a good modern answer on either SO or elsewhere, so I'm posting my own answer here just for other's reference.
The wrong way to do this is to implement keyDown: and check for the particular keys having been pressed. This circumvents Apple's key-binding mechanism, which as it happens does supply the needed selectors for the concepts of "scroll to beginning" and "scroll to end"; NSTextView just doesn't respond to those selectors.
All you need to do is to add, in your NSTextView subclass, the following:
- (void)scrollToBeginningOfDocument:(id)sender
{
[self scrollRangeToVisible:NSMakeRange(0, 0)];
}
- (void)scrollToEndOfDocument:(id)sender
{
[self scrollRangeToVisible:NSMakeRange([[self string] length], 0)];
}
This hooks up the NSResponder methods for the relevant key bindings to appropriate actions in your NSTextView. These methods on NSResponder appear to have been public since 10.6 or so, and may have actually existed for a while before that, so this solution should be good on all modern systems.
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How to know when the device is charging?
I am kind of a noob at Xcode so sorry if I make I fool out of my self. I am trying to make an app and one of the functions is to check if the iphone/ipod/ipad is charging or not. If not charging I want it to play a sound perhaps and flash LEDs. Also if it is charging, I want to make it display text such as "Device Charging :) " . By the way I am using the method with the FlipsideViewController, but these features will be in the MainView. I have looked at various examples and have seen the one below as well as many test applications, but I don't know how to use it. Thank you in advance!!
Code:
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
if ([[UIDevice currentDevice] batteryState] != UIDeviceBatteryStateUnplugged) {
//Device is connected (charging or fully charged)
}
It sounds like you might want to start with some tutorials on writing iPhone apps because the code above looks ok (though I haven't tried this specifically). If you don't understand how to do some of the things I describe below (or some of the terms are unfamiliar), I'd recommend getting a good book or checking out some tutorials:
Book: iOS 6 SDK Development (Amazon)
Tutorials: Ray Wenderlich has many good tutorials on his site
Apple Documentation is quite good
There's no point in just writing the code for you because you presumably want to learn how to program and you won't learn much if someone just does it for you.
Here are some pointers on how to think about this, though the behavior you desire isn't completely obvious from what you've said.
If I understand you you want two different actions to happen when the main view is displayed.
To start with, why not get it to set the text string in a field to match the state of charging / not charging?
So you'd add a UILabel to the main view .xib file in Xcode and add an outlet to it in the view controller so that you can set it's text at runtime.
Then you'll want to call the first line above somewhere once to start monitoring (parameter is: YES) and again with NO to stop monitoring once the view goes away. If you look at the documentation for UIViewController at developer.apple.com near the top you'll see a list of methods that UIViewController implements. In this case you probably want to override (create your own version of) viewWillAppear and viewWillDisappear and put the setBatteryMonitoringEnabled call with YES and NO into these two methods respectively.
So that takes care of enabling / disabling battery status monitoring. Now you need to test the state and take action (this is call to batteryState in your code above).
One question about design which isn't obvious from your description is if you want to do this once when the view appears or if you want it to continually monitor the state.
If you want to do it once, you could put the call to the above in viewDidAppear, say, and then use the outlet to the UILabel to set the message in the label to "Charging" or "Not Charging" based on the result from the batteryState method.
If you want something that watches for changes in the state of charging then you need to subscribe to the notification and put your code to change the UILabel (or whatever you finally do in response to a change) in the handler for that notification.
You figure this out by reading the documentation for UIDevice at developer.apple.com or in Xcode's Organizer window's Documentation section. If you read the documentation for the UIDevice method batteryMonitoringEnabled you can see that the "See Also" section includes two entries for the notifications you can subscribe to to find out when the Level or the State changes: UIDeviceBatteryLevelDidChangeNotification and UIDeviceBatteryStateDidChangeNotification. To learn how to subscribe to notifications you'll want to look at NSNotificationCenter documentation.
Then once you get this working you can add your fancier alerts (be careful about putting them up repeatedly!) and sounds (though playing a sound when the battery isn't charging will use battery which is somewhat questionable.. just make sure not to do it repeatedly perhaps).
good luck!
Add this to the appDelegate.m
- (void)applicationDidBecomeActive:(UIApplication *)application{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(batteryStateDidChange:) name:UIDeviceBatteryStateDidChangeNotification object:nil];
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
}
- (void)applicationWillResignActive:(UIApplication *)application {
[[UIDevice currentDevice] setBatteryMonitoringEnabled:NO];
}
- (void)batteryStateDidChange(NSNotification*)notif {
// check the status here.
// See if it is charging, or not and respond to the change.
}
(edited to make code more explicit)
I'm writing a simple cocoa program that should use the swipe gesture.
I've implemented in my NSView subclass the method swipeWithEvent: but when i try the program the method is never called. rotateWithEvent: method works instead.
I'm using a Xcode 4.1 on Mac OS 10.7 Lion.
Is there a difference between rotateWithEvent: and swipeWithEvent: ?? Why the first is called when I'm under the view and do a rotate gesture and the second in the same condition is never called if i do the swipe gesture?
Update :
I built also a simple project only to check the swipeWithEvent: and rotateWithEvent: methods but the behavior is the same.
Take a look at this sample code I wrote https://github.com/oscardelben/CocoaNavigationGestures
I think it would be helpful if you posted your code, reduced down to the bare essentials if possible.
One thing to look at is to make sure the method signature exactly matches the definition. In this case it should be:
- (void) swipeWithEvent: (NSEvent*) event
{
NSLog( #"A swipe happened" );
}
Make sure your definition matches this. Since you have a rotateWithEvent: that is working correctly this is probably unlikely but sometimes a typo can creep in.
Another thing you can do is to make a sample project that does nothing but respond to a swipe by logging (or whatever). This can help identify if there is something else ii your code or view hierarchy that is getting in the way.
In Cocoa/Objective-C if I have created a button programmatically, what do I put in for my control event?
[btnMakeChar addTarget:self action:#selector(makeChar:) forControlEvents:WHAT GOES HERE?];
In iOS you can write it like so forControlEvents: UIControlEventTouchUpInside
I can't find anything to show what I would use for just cocoa, not cocoa touch
I'm not sure if I understand you correctly, but if you're programming a Mac,
[theHappyButton setTarget:self];
[theHappyButton setAction:#selector(doStuff)];
it is two separate lines, rather than the one combined line of code on an iPhone.
I hope that is what you were after??
To find it in the doco: choose on the 10.6 doco (not iOS) and search on "setAction:". You'll see it in NSControl Class Reference. NSButton is of course a subclass of NSControl.
The method you're asking about does not exist in Cocoa, so nothing goes there. Cocoa controls have a single target with a single action, and either use a different addTarget:-type method for each kind of action or expect a delegate object that will handle all the events they generate.
I have a list view class that just like NSCollectionView requires an additional prototype item and a prototype view to be of any use.
When dropping an NSCollectionView from the library in Interface Builder those two helper items are automatically created. However I couldn't find a single official Apple document dealing with this use case (describing how its done).
Digging thru the Apple Dev Guides I could however find "ibDidAddToDesignableDocument:".
With the following code I managed to get my auxiliary items created on drop from library:
- (void)ibDidAddToDesignableDocument:(IBDocument *)document {
[super ibDidAddToDesignableDocument:document];
NSView *prototypeView = [[[NSView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 300, 65.0)] autorelease];
DLListViewItem *prototypeViewItem = [[[DLListViewItem alloc] initWithNibName:nil bundle:nil] autorelease];
[document addObject:prototypeViewItem toParent:nil];
[document addObject:prototypeView toParent:nil];
[document connectOutlet:#"view" ofSourceObject:prototypeViewItem toDestinationObject:prototypeView];
[document connectOutlet:#"listView" ofSourceObject:prototypeViewItem toDestinationObject:self];
[document connectOutlet:#"prototypeItem" ofSourceObject:self toDestinationObject:prototypeViewItem];
}
However…
…IB adds those aux items for NSCollectionView only on the actual initial drag from the library, not on any other call of "ibDidAddToDesignableDocument:", such as when embedding, copying or duplicating the item. (while my method would, and on all)
This makes me wonder whether Apple actually uses "ibDidAddToDesignableDocument:" for this and if I'm on the right track with this at all.
How does one imitate this properly? I'm having a hard time trying to distinguish between different contexts for "ibDidAddToDesignableDocument:". Anybody successfully done this?
Unfortunately none of Google, Google Code, GitHub, or the documentation revealed anything helpful, so I'm in desperate need of help here. :(
Thanks in advance!
Edit: Oh great, this question just brought me the tumbleweed badge, yay! Not.
I'm more into useful answers actually, but thanks anyway ;)
I struggled with this on a plugin I did myself a while ago. In my case I was able to check a property of the object to see if it had been initialized already and skip adding the auxilliary objects in that case. I believe BWToolkit uses some internal checking that is similar. Couldn't you check your object's 'prototypeItem' property to see if you need to skip creating your aux objects?