iPhone keyboard popup problem in UIAlertView - cocoa-touch

I want to accept password using an alert view. Following is the code I am using. But I am unable to figure out why does the keyboard pop out two times instead of once? Any ideas?
UIAlertView *passwordAlert = [[UIAlertView alloc]
initWithTitle:#"Enter Password" message:#""
delegate:self cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Submit",nil];
[passwordAlert addTextFieldWithValue:#"" label:#"Password"];
UITextField *textfield = [passwordAlert textFieldAtIndex:0];
textfield.secureTextEntry = YES;
[passwordAlert setTag:10];
[passwordAlert show];

Not entirely sure where addTextFieldWithValue is defined but you may want to check your .xib file to make sure you didn't place double views on the "stage" as in this post:
http://www.iphonedevsdk.com/forum/iphone-sdk-development/1479-uialertview-popping-up-twice.html
Also, check out this post. Looks like you may have to "tell the text field to become first responder before showing the alert view, you'll wind up with two keyboards":
http://www.iphonedevsdk.com/forum/iphone-sdk-development/2753-new-info-adding-text-fields-alerts.html#post14701

Related

How to validate login using UIAlertView in IOS application?

I have created login for my application. I have written alert view in iOS. Here the code:
alertView = [[UIAlertView alloc] initWithTitle:#"Login"
message:#"Enter username and password"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"Login", nil];
alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[alertView show];
I want to check the user name and password to allow to view next form. If the user name and password are wrong, the alert view need to pop up always.
How to pop up the alert always when the user name and password are wrong?
Any idea or reference should be appreciated.
Thanks in Advance.
In your view controller make sure you implement the UIAlertViewDelegate protocol
#interface MyCarsViewController () <UIAlertViewDelegate>
and then add this method:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
to the view controller. This is where you can get a reference to the text fields in the alert and do your db check etc. To get a reference to the text fields use
textFieldAtIndex:
https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html#//apple_ref/occ/instm/UIAlertView/textFieldAtIndex:
After you check for the correct login you can present other view controllers or display another alert depending on the login results.
Hoep this helps

how to display a multiple choice picker

i'm trying to find the correct control to display a choice selector, like the one which appears in Safari when you long press on image.
It should be some kind of UIPickerView, but it is not the same design.
Any idea is welcome.
This looks like UIActionSheet, which can be presented for example from the UITabBarController likes this:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"This will delete all your records. Are you sure you want to continue?" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"Yes, go ahead." otherButtonTitles: nil];
[actionSheet showFromTabBar:self.tabBarController.tabBar];

How to prevent a UISwitch from changing state?

I have a UISwitch that is defined in an .xib file. The event that I'm connected to is "Value Changed".
I want the following behavior (essentially warning the user that this function is available in the Full Vesion of the software):
allow user to click on switch
prevent the switch from sliding to "on" (I want the switch to stay in the "off" position)
show an alert
So far, I can't get 2 to work. Right now I have a kludge. I force the switch to go back to the OFF position:
[self.switchButton setOn:NO animated:NO];
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Feature unlocked in Full Version" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil] autorelease];
alert.tag = ALERT_TAG;
[alert show];
The problem is that you see the switch slide to the ON position, then it jumps to the OFF position, and then you see the alert box.
Is there a way to intercept the behavior so that the switch doesn't slide to the ON position?
UPDATE
I tried to link up to the "TouchUpInside" event and have moved my alert code there. It's still not early enough to intercept the visual change in the state of the switch.
I've had the same problem like you. In your valueChanged action method you have to invoke the setOn method with animated set to true. So in swift that would be:
#IBAction func switchValueChanged(sender: UISwitch) {
sender.setOn(false, animated: true)
}
It might seem counterintuitive since this method is called after switch value changed. But somehow it seems to work.
One unsophisticated solution is just putting a button with the same size and transparent background color in front of the UISwitch control. While it is not the direct answer to your question, it is nice workaround and I always do that with UIImageView.
Why dont u simply set userInteraction to NO
[self.switchButton setUserInteractionEnabled:NO];
add a clear color subview to that UISwitch view, cover it and add UITapGestureReconizer to this subview, and all action operations can be triggered in tap action including change the UISwitch view status. Hope it help you!
Try to listen to the TouchDown event instead of TouchUpInside.
This is the way you do it, set UISwitch to .touchUpInside
switchBtn.addTarget(self, action: #selector(unlockEvent), for: .touchUpInside)
if you set to .valueChanged, will trigger the event every time, regardless having user interaction or not.
set the state of UISwitch to off in xib and inside valueChange action method
-(IBAction) switchValueChanged{
if (toggleSwitch.on) {
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Feature unlocked in Full Version" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil] autorelease];
alert.tag = ALERT_TAG;
[alert show];
}
}
its pretty lame that there doesn't seem to be a delegate method for UISwitch to check whether to allow UISwitch to change.
I added a button over the UISwitch and handled the switching on the button's IBAction event, seems to be the best method.

UIAlertView "non-blocking" show message does retain the view?

I have a simple question for you... I was reading Beginning IOS 4 Development book and there is the following code example:
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#”Hello”
message:#”This is an alert view”
delegate:self
cancelButtonTitle:#”OK”
otherButtonTitles:nil];
[alert show];
[alert release];
Then it points out that the show message shows the alert view but the code does not stop it's execution until the user dismisses the dialog, it goes on and executes the following code... since next to the show message there is a release message, does the show method retain the view until it is dismissed? Otherwise I should not release it after the show message has been sent.. I'm sorry but I did not find this information on the reference pages, so I hope this is not a (too much) stupid question.
OT: how do I activate colors on code snippets?
The window that displays the alert view retains its reference, so you don't have to.
I've wondered this and concluded that something in the show method, likely a call to addSubview: increments the retain count on the UIAlertView preventing it from being dealloc-ed.

UIAlertView with 19 buttons

I am creating a pop up dialog box (UIAlertView). It works great, except I need to choose from 19 items, and the buttons do not automatically scroll and only five fit on the screen.
So, can I make them scroll? If not, can I put a UIPickerView in an alert view? Or, is there a better way?
Code so far:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Provider"
message:#"Please choose your provider:"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"AT&T",#"Verizon",#"Sprint",#"Nextel",#"T-Mobile",#"Alltel",#"US Cellular",#"Virgin Mobile",#"Metro PCS",#"Boost Mobile",#"Cricket",#"Rogers(Can)",#"Telus(Can)",#"Bell Mobility(Can)",#"Fido",#"Virgin(Can)",#"O2",#"Vodaphone",#"Meteor", nil];
[alert show];
[alert release];
You might consider using a UIActionSheet instead. It will automatically scroll when you have a lot of items.
Don't create 19 buttons. Instead, use UIPickerView.
In general, use UIActionSheet or a modal view of some sort.
In this particular case, you may be able to use the CoreTelephony framework. CTCarrier has a property carrierName that is supposed to return the something similar to what you are asking for (as long as the device is connected to a cellular network).