http://iphonedevelopment.blogspot.com/2009/02/alert-view-with-prompt.html
I'm using that code to get my iPhone app to display an alertView with an included UITextField and matching CANCEL and OK buttons.
The user can enter text, hit CANCEL or OK... and I see the text the user entered... using my code inside the clickedButtonAtIndex method.
It all works except for 1 thing: Instead of OK, the user might hit DONE on the keyboard.
I'm using dismissWithClickedButtonIndex to simulate an OK click... but then clickedButtonAtIndex never gets called.
Shouldn't dismissWithClickedButtonIndex also call clickedButtonAtIndex?
I'm trying to get clickedButtonAtIndex to get called if CANCEL, or OK, or even DONE is hit.
Since this is an old question you've probably figured it out by now, but I was incredibly frustrated with this and couldn't find any help from Google, so for anyone who comes across this in the future: when you call dismissWithClickedbuttonIndex, the method that gets called is alertView:didDismissWithButtonIndex, not alertView:clickedButtonAtIndex. It behaves exactly the same way, and I can't figure out why they'd be two separate methods for what seems to be the same action, but there it is.
clickedButtonAtIndex is called when user actually clicks the button. Clicking the button may or may not dismiss the alert. the standard OK/Cancel buttons have the effect of dismissing the alert on click. If a click translates to dismiss or a dismissWithClickedbuttonIndex method is called, then did DismissWithButtonIndex is called.
I'm not sure whether I fully understand your question, but here is my try:
Firstly, you need to remove the if (buttonIndex != [alertView cancelButtonIndex]) from the example. This should work for the OK and CANCEL buttons.
To recognize the DONE key of the keyboard, use - (BOOL)textFieldShouldReturn:(UITextField *)textField of the UITextFieldDelegate. Here you could call [textField resignFirstResponder] to dismiss the keyboard.
Also, what to you do in clickedButtonAtIndex? Couldn't you create your own method and the call it when you need it?
I had the same problem with UIAlertView(Blocks) categoty
UIAlertView-Blocks
My solution:UIAlertView(Blocks) -> MYAlertView : UIAlertView
Sometimes method:- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex called. Sometimes it didn't call.
So... I tried to find solution in many sources. Then I'he created subclass of UIAlertView with absolutely same code. And my problem was solved. In fact it is little bit more difficult to use. But no more this intrusive bugs.
Related
I just started to suspect that calling resignFirstResponder directly is not actually allowed. Unlike NSResponder, it's allowed to call becomeFirstResponder directly in UIKit. So far I was making assumption that calling resignFirstResponder would be okay too. But in fact, the manual says resignFirstResponder method is there to get notified, and mentions nothing about direct calling.
Notifies this object that it has been asked to relinquish its status
as first responder in its window.
If it's designed in same way of how NSResponder works, direct calling to resignFirstResponder wouldn't be allowed though there's no obvious way to figure out validity of the call in the manual...
If it's been designed not to be called directly, directly calling to the method would be harmful or make code harder to maintain.
Is it okay to call UIResponder.resignFirstResponder method directly?
It seems to be okay because on another part of the manual, it says
...
To dismiss the keyboard, you call the resignFirstResponder method of
the text-based view that is currently the first responder. When a text
view resigns its first responder status, it ends its current editing
session, notifies its delegate of that fact, and dismisses the
keyboard. In other words, if you have a variable called myTextField
that points to the UITextField object that is currently the first
responder, dismissing the keyboard is as simple as doing the
following:
[myTextField resignFirstResponder];
Everything from that point on is handled for you automatically by the text object.
When connecting a button in Xcode as #IBAction func, we can choose between Arguments: Sender or None.
When do we choose Sender and when None?
Well, if the action method needs to know which button triggered it, or can make use of any of the button's properties, then you would need to pass the sender (the button) to the method.
A contrived example for this would be if you have a calculator, which has a '+' and a '-' button. They both could share the action method performCalculation(sender: NSButton) and the method could use the sender's symbol (this would be the button's title) to perform the correct calculation.
On the other hand, if you have a button, that will for example clear all input on a textfield, then you would wire this button up to a method clearInput() which would not need to have any information about the button that triggered it (in fact, having no argument here would make it easier to reuse this method elsewhere in your code and trigger clearing the textfield programmatically).
I hope this makes sense to you. There are no clear rules afaik. It's mostly a matter of taste I guess.
I'm adding sharing to my app (targeting Mavericks, 10.9), which I want to work like this:
User clicks Share button
Cursor changes to crosshair
User drags selection of what he'd like to share
NSSharingServicePicker displays, allowing the user to pick the service to share with
I'm accomplishing this using the -mouseDown:, -mouseDragged:, and -mouseUp: events. mouseDown begins the selection, mouseDragged provides feedback as to the area being selected, and then mouseUp completes the drag, showing the picker. Each time, though, I get this written to the console:
2014-06-25 00:13:45.111 App[31401:303] Warning: -[NSSharingServicePicker showRelativeToRect: ofView: preferredEdge:] should not be called on mouseUp
Please configure the sender with -[NSControl sendActionOn:NSLeftMouseDownMask];
I don't understand why that would be a problem, unless you were showing it from a button click on mouse up. Should I ignore the message? I've tried showing it using dispatch_async and dispatch_after to try and get it to run outside the event's invocation, but they didn't work. I suppose I could ignore it, but does that leave the door to deprecation open?
I know this is a year late but,
I had the same problem. After some research, I cam back with this answer. Before I implemented this code, my button would spin for a while, and then return with the same error you had. When I click my share button now, it no longer lags, and does not return any error. Insert this into your app's awakeFromNib method:[yourShareButtonName sendActionOn:NSLeftMouseDownMask];.
This is what your code should look like:
- (void)awakeFromNib {
[yourShareButtonName sendActionOn:NSLeftMouseDownMask];
}
I hope this helps!
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.
I have a password field of type NSSecureTextField say passwordField and a NSButton. I have assigned return key as key equivalent for the button action triggering. After pressing the button I get user entered text as [passwordField stringValue] this will give the correct value only, when user press return key after entering his password but not when user clicks on the button through mouse. In the latter case it will give nil value. I have tried hard to find the problem but no use. If anybody knows what is going wrong, please help me.
Regards
ypk
EDIT: This is observed only in 10.5 and works fine on 10.6.
Try checking Continuous on the text field in Interface Builder.
Also make sure Action is set as Sent On End Editing.
I've had a similar problem. For me, it turned out that the focus was not correctly set on the text field. Calling [window makeFirstResponder: textfield] after a timeout of 0.5 seconds solved it.
I usually explicitly call [window makeFirstResponder:nil as one of the first lines in my button click events to ensure the Sent On End Editing event is called.