In running my program, a basic chat app which i'm writing to learn ios, i noticed different results with:
[self._inputBox resignFirstResponder];
vs
[sender resignFirstResponder];
i have this ibaction:
- (IBAction)_sendText:(id)sender
which is called on hitting the send button. its also called on enter as that ibaction calls the same ibaction as the send button.
Now on the iphone which i just supported tonight ( it's a one view now i have both a view on my ipad storyboard and my ipod and wired the ipad actions to the ipod storyboard tonight) for clicking the send button which is wired to this ibaction, self.inputBox calling resignFirstResponder works but sender resignFirstResponder did not work. I have a friend who is testing and he was the one who found it didn't work on his ipod. I have only run it in the simulator and both worked there.
Mike
If a "Send" button is connected to your _sendText: action and you click that button, then the button is the sender and not your text input field, therefore [sender resignFirstResponder] will not have the desired effect.
Related
I am using the following control in a viewcontroller i want to show the keyboard when user opened the view so I tried to call this method from the viewcontroller
[self.inputToolbar.textView becomeFirstResponder];
But the keyboard does not show when opened the view. So i called the becomefirstresponder in the place where the textview is allocated you can see the allocation page here i called like this
[internalTextView becomeFirstResponder];
Now it works in iOS6 but in iOS7 it does not works
By using sample code
I write below code inUIInputToolbarViewController.m > loadView
[self.inputToolbar.textView.internalTextView becomeFirstResponder];
And it's working fine.
Code
So in some of the apple sample code I see things like this for application testing:
[viewController press:[calcView viewWithTag: 6]]; // 6
However when I try to do that with my own viewcontrollers/views all I am getting is "No visible interface declares press"... where is the documentation for application SenTesting on iOS, and in particular how do you go about doing UI testing (programmatically press a button, etc) within iOS?
Without seeing the sample code, I would guess that the view controller has a UIAction -press:. It should expect the nib to wire button presses to this action.
Instead of simulating the event, the test directly calls the action touch event would call.
After looking a bit, I think you may want how to programmatically fake a touch event to a UIButton?.
First you need to get a reference to the UIButton. If it's already set as an outlet in the view controller, you can use that. So, I'll assume your viewController has an IBOutlet for that UIButton named 'button'
[viewController.button sendActionsForControlEvents:UIControlEventTouchUpInside];
For the Swift 2.0 solution
viewController.button.sendActionsForControlEvents(.TouchUpInside)
I want Button action done automatically when a view load. Is it possible?
The other answers are correct in that setting the action that your button is tied to, then in your viewDidLoad:, call that function will work. I will just chime in with another method for others info.
You can send it a control event telling it that the button should act as if it has been pressed:
[button sendActionsForControlEvents:UIControlEventTouchUpInside];
This is useful when you do not have an outlet to the button. For instance, I created an app where the user can press on a web view and launch a youtube video. It was also required that if the user presses a "video" button, then the same youtube video would launch. Basically, I had to fire a press event on the web view. So i searched through its views and found the button, from there I called the above line, and the webview pushes a video view controller for the youtube video.
Certainly Yes. Call your method as
assuming your method declaration as
-(IBAction)yourButtonTapEvent:(id)sender;
[self yourButtonTapEvent:nil];
Yes, in your viewDidAppear method, just call the action you are providing for that specific button.
- (void)viewDidAppear:(BOOL)animated
{
[self yourButtonAction:nil];
[super viewDidAppear:animated];
}
Ok
On first run I ask for the mobile phone number in an alertview. This is using - (void)textFieldDidEndEditing:(UITextField *)textField to validate the phone number.
Now I also have push notifications.
The trouble is when the app is first installed the alertview shows and the keyboard shows but the "Do you want to enable push notifications" alert also shows. So whats the problem? Well the alert shows then over the alert the push notification shows blocking the input box. Then the keyboard shows blocking the "Yes/No" buttons of the push notification.
Therefor I cannot dismiss or answer either alert! So my first thought is to comment out [passwordField becomeFirstResponder]; for the phone number alert and the keyboard can be dismissed but then my validation never gets called. :(
Any suggestions will be appreciated.
You have to delay your register for push notifications code until after your UI is done.
The UIAlertViewDelegate protocol defines two methods, alertView:clickedButtonAtIndex: and alertView:didDismissWithButtonIndex:, which seem to me to be identical in usefulness.
Why is there a clickedButtonAtIndex and a didDismissButtonWithIndex when they both do the same thing? I realize there is also a willDismissButtonWithIndex that happens before the alert view is dismissed, but is there any reason to use clickedButtonAtIndex instead of didDismissButtonWithIndex?
I found a more useful difference between the two:
When showing a UIAlertView, and the device goes to sleep, alertView:didDismissWithButtonAtIndex: gets called, even though the alert view is not actually dismissed. It is shown again once the device wakes up. alertView:clickedButtonAtIndex: is only called when the user clicks one of the buttons.
The alertView:clickedButtonAtIndex: is called when the user clicks a button on an alert view whereas the alertView:didDismissWithButtonIndex: is called after an alert view is dismissed from the screen. (See the UIAlertViewDelegate Protocol Reference.)
The difference is minimal but it allows you to do something before or after animation.
If the alert view disappears for any reason (including being covered by another UIAlertView, going to sleep, etc.), didDismissWithButtonAtIndex: is called. This can mean that the method can be called even without the user clicking on anything. This can lead to unexpected behaviour if you depend on this delegate callback to be called in response to the user actually clicking on a button. In this case clickedButtonAtIndex: is more useful.
I couldn't reproduce Ed's behaviour by locking my device with the alert view present on iOS 7.
However, the most important difference between alertView:clickedButtonAtIndex:, alertView:didDismissWithButtonIndex: and alertView:willDismissWithButtonIndex: is that the first method (clickedButtonAtIndex:) is only called when the user explicitly taps on a button on your alert view (hence 'clicked').
Is it possible that an alert view is dismissed without clicking on a button? Yes, you could programmatically hide an alert view using the UIAlertView method dismissWithClickedButtonIndex:animated:.
So, if you need some behavior to be always triggered upon the dismissal of the alert view—whether it was triggered by the user tapping on a button or triggered programmatically—then using the didDismissWithButtonIndex: and willDismissWithButtonIndex: makes more sense.