have the following:
// watch the fields
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(handleTextChange:)
name:UITextFieldTextDidChangeNotification
object:textField1];
and then:
-(void) handleTextChange:(NSNotification *)notification {
...
}
Have a breakpoint in -handleTextChange:, but doesn't get fired.
textField is connected in the Interface Builder.
Works on iOS6 iPhone/iPad simulator, on iOS5.1 iPad2, but not on iOS6 iPad3.
Irena is correct, UITextFieldTextDidChangeNotification does not fire when the text field is set programmatically. However I would just like to clarify that it has nothing to do with iOS6, it has to do with the iOS 6 SDK. If you compile with the iOS 5.1 SDK, the UITextFieldTextDidChangeNotification notification will fire whenever the text field is changed, programmatically or otherwise, even if run on an iOS 6 device.
so I figured it out. What changed in IOS6 SDK is that if you change the text of textfield programmatically, it doesn't send a notification. I have a custom keyboard on all of those views. when I tap on a key, it changes the text field text value by adding whatever I typed in. In ios 5 it would send a notification "textdidchange", but not in ios6.
My use case was somewhat special, I was creating HH:MM:SS duration UITextField with characters entered from the back, therefore trapping characters in - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string and then returning return (NO); to forbid auto-update of UITextField... pre-iOS6, it called the notification, post-iOS6, I'm simply calling [[NSNotificationCenter defaultCenter] postNotificationName:UITextFieldTextDidChangeNotification object:self.textField]; just before the return statement.
On my ipad3 & iOS6.0 notification UITextFieldTextDidChangeNotification work fine. put
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(handleTextChange:)
name:UITextFieldTextDidChangeNotification
object:textField1];
in your viewDidLoad
As a temporary workaround until Apple fixes this, you can use the following code example:
//view is a UITextField
NSString *temp = ((UITextField*)view).text;
((UITextField*)view).text = #"";
[((UITextField*)view) insertText:[NSString stringWithFormat:#"%#%#", #"-", temp]];
That code will continue to fire the event.
This works too:
[((UITextField*)view) sendActionsForControlEvents:UIControlEventEditingChanged];
Related
I have a Mac application where I would like to save some values when the window closes. However, I can't figure out how to do this. I have the application delegate controlling the main window (which may not be the way you are supposed to do it, and I probably shouldn't have done it this way if what I've read previously is correct) and I can't figure out for the life of me how to do this! I believe it can be done using a NSWindowController, but can you do it in the app delegate? Thanks!
Use NSWindowWillCloseNotification.
- (void)applicationWillFinishLaunching:(NSNotification *)notification {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:#selector(windowWillClose:) name:NSWindowWillCloseNotification object:window];
}
- (void)windowWillClose:(NSNotification *)notification {
// Your code for saving data
}
I am trying to make a toolbar that is always above the keyboard, pretty much like in Apple's native Messages app. If you take a closer look at that app on the iPad, try undocking the keyboard and the dragging it around by holding than button in its bottom right. You will notice that Apple's toolbar tracks the keyboard's position quite nicely.
Trying to replicate that behavior, I subscribed to both the keyboard-will-change-frame and keyboard-did-change-frame notifications. However, to my disappointment I found that the first one is sent when the user starts the dragging, and the latter is sent when the keyboard snaps to its final position, but there are no notifications I could find that are sent in between. Am I missing something? Here's what I am talking about:
This is the code I use to subscribe to notifications:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidChangeFrame:) name:UIKeyboardDidChangeFrameNotification object:nil];
These are my listener functions:
- (void)keyboardWillChangeFrame:(NSNotification *)notification{
NSLog(#"will change");
// [self alignToolbarForKeyboard:notification action:#"will_change_frame"];
}
- (void)keyboardDidChangeFrame:(NSNotification *)notification{
NSLog(#"did change");
// [self alignToolbarForKeyboard:notification action:#"did_change_frame"];
}
I commented out the call both listener functions make to make sure it's not blocking anything. Is there a notification type I do not know of? I did look at the UIWindow reference but couldn't find anything beyond what I already have. Just to make sure, I also checked whether the show/hide notifications are sent during the movement. They aren't.
Any ideas? Thanks!
I am developing an application were everything is working fine, except one i.e. when user press on home while keyboard is in active and again opens my application the view frame bounds are changing and moving out of bounds. My expected result is keyboard should get suspended or the view should stay in the same position when it is come back from background to foreground with keyboard in-active state.
I hope people understand my scenario and reply ASAP.
Thanks.
I have found the solution to my question, i hope people can use my solution. Below is the code what I have done,
Add the below line of code in your RootViewController file (i.e. which view is coming at first when you open your APP).
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(receivedNotification:) name:UIApplicationDidEnterBackgroundNotification object:nil];
And then add a private method as below
- (void) receivedNotification:(NSNotification *) notification
{
if ([username isFirstResponder])
{
[username resignFirstResponder];
}
else if ([password isFirstResponder])
{
[password resignFirstResponder];
}
}
I hope it help some body,Thank u.
Further assistance please see the mentioned link,
there is a method in the app delegate
- (void)applicationDidEnterBackground:(UIApplication *)application
this method is fired when you press the home button.
do the necessary changes(textField resignFirstResponder) in this method and it should work fine i guess.
EDIT here's the code
in the class where you have your textfield create a method
-(void)performWhenHomeBtnprssed
{
[MytextField resignFirstResponder];
}
then in
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[myClassObj performWhenHomeBtnprssed];
}
also i agree with #valexa you should find the root cause of the problem
In software development it is always better to address the root causes than to patch the effect, in your case there are problems with the positioning of your views and you should address that, foreground/background cycling should not affect the views positioning.
Hi I'm having a weird problem.
My app is based on the samplecode of "PageControl" (the Apple example).
It uses a horizontal scrollview in which most of the stuff is happening.
At he bottom I have a UIToolbar from which I call a modal viewcontroller.
On XCode 4 everything worked like a charm, after the upgrade to XCode 4.2 (with the new SDK) I get a "exc_bad_access" on dimissModalViewcontroller.
The funniest thing is that it does not happen rightaway but only after 2 or 3 times presenting and dismissing the modalViewcontroller.
To simplify things I went back to the original samplecode and tried to implement the modalVieWcontroler in that context. No luck so far.
In the original PageControl Code I changed the type of "ContentController" from NSObject to UIViewController like so:
#interface ContentController : UIViewController
{
NSArray *contentList;
}
I call presentModalViewcontroller in a sub class (from ContentController) named PhoneContentController like so:(I use a notification so I can call it from anywhere)
-(void) showExplanationsModal:(NSNotification*)notification{
ExplanationsViewController *xplViewController = [[[ExplanationsViewController alloc] initWithNibName:#"Explanations" bundle:nil]autorelease];
[self presentModalViewController:xplViewController animated:YES];
}
The dismissal of the modalViewcontroller is called from the modal view itself like so:
(the notification is used tot initiate some other stuff)
- (IBAction)onClose
{
[self dismissModalViewControllerAnimated:YES];
[[NSNotificationCenter defaultCenter]postNotificationName:#"dismissExplanationsModal" object:self];
}
This code works fine with iOS4 SDK but renders occasional excec_bad_access with iOS5 SDK.
When I compile the app with iOS4 SDK it also rus fine on iOS5 devices.
I tried using Zombies but this does not point to a specific over-released object.
I'm sort of stuck on this one for a few days already ...
I have put up a copy of a sample project that illustrates the problem here http://www.sesni.biz/pagecontrol.zip
It seems for me that problem is in the onClose method. Try first sending the message, without the object (this object will be invalidated soon).
- (IBAction)onClose
{
[[NSNotificationCenter defaultCenter]postNotificationName:#"dismissExplanationsModal" object:nil];
[self dismissModalViewControllerAnimated:YES];
}
Found the problem: I changed the type of ContententController from NSObject to UIViewcontroller. This worked fine with the iOS4 SDK but crashes with iOS5 SDK.
- (void)applicationWillResignActive:(UIApplication *)application {
NSLog(#"resigning active status...");
}
i have tried hardware-lock in iphone simulator but this isn't called. I do want to call it in another UIviewcontroller class not in the appdelegate itself.I also added in the viewController's header File.
According to the documentation, the "applicationWillResignActive" method will get called if the device is locked.
As such pressing Command-L (or "Hardware" >> "Lock"" in the menu) will cause the iPhone simulator to lock and hopefully trigger this method.
I could only get this code to work in the AppDelegate file as well.
You could trying adding your view controller as an observer using the NSNotificationCenter,
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(foo) name:#"AppResigned" object:nil];
Then in the applicationWillResign post the notification,
[[NSNotificationCenter defaultCenter] postNotificationName:#"AppResigned" object:nil];
Hope that helps!