UIAlertView displays textboxes and buttons - uialertview

I used the following code to create a UIAlertView and add some components on it but the result is at the image :(( (image is here : http://i.stack.imgur.com/DTg02.png)
-(IBAction)login:(id)sender
{
UIAlertView *login = [[UIAlertView alloc]initWithTitle: #"Login first"
message:#"enter username and password please first" delegate:self cancelButtonTitle:#"cancel"otherButtonTitles:#"OK", nil];
k = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 70.0, 200.0, 25.0)]; //<< it also displays wrong without this line
k.text = #"";
k.backgroundColor = [UIColor whiteColor];
k.clearButtonMode= UITextFieldViewModeWhileEditing;
k.keyboardType = UIKeyboardTypeAlphabet;
k.keyboardAppearance = UIKeyboardAppearanceAlert;
p = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 100.0, 200.0, 25.0)];
p.text = #"";
p.backgroundColor = [UIColor whiteColor];
p.clearButtonMode = UITextFieldViewModeWhileEditing;
p.keyboardType = UIKeyboardTypeAlphabet;
p.keyboardAppearance = UIKeyboardAppearanceAlert;
[login addSubview:k];
[login addSubview:p];
[login show];
}

if you want to add multiple textField and Multiple Button on UIAlertView then follow the step :
step 1 :
#interface ViewController ()<UIAlertViewDelegate>
{
UITextField *textFieldOne,*textFieldTwo,*textFieldThird;
}
step 2 :
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Multiple TextField" delegate:nil cancelButtonTitle:#"NO" otherButtonTitles:#"YES", nil];
[alert addButtonWithTitle:#"CANCEL"];
alert.delegate=self;
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
textFieldOne=[[UITextField alloc]initWithFrame:CGRectMake(5, 0, 150, 20)];
[textFieldOne setPlaceholder:#"First Name"];
[myView addSubview:textFieldOne];
textFieldTwo=[[UITextField alloc]initWithFrame:CGRectMake(5,25, 150, 20)];
[textFieldTwo setPlaceholder:#"Middle Name"];
[myView addSubview:textFieldTwo];
textFieldThird=[[UITextField alloc]initWithFrame:CGRectMake(5,50, 150, 20)];
[textFieldThird setPlaceholder:#"Last Name"];
[myView addSubview:textFieldThird];
[alert setValue:myView forKey:#"accessoryView"];
[alert show];
step 3 :
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"YES"])
{
NSLog(#"Button YES");
NSLog(#"TextFiledOne Text = %#",textFieldOne.text);
NSLog(#"TextFiledTwo Text = %#",textFieldTwo.text);
NSLog(#"TextFiledThree Text = %#",textFieldThird.text);
}
else if([title isEqualToString:#"NO"])
{
NSLog(#"Button NO");
}
else
{
NSLog(#"Cancel is click");
}
}

A UIAlertView is no thought for additional text input. You could use instead a UIActionSheet as it is explained here.

There is no point adding to the code that has already been added on here as it is good enough already and there are some good alternatives given. What I am going to do is tell you why you shouldn't be adding your own UITextFields to a UIAlertView using addSubview:.
Basically Apple have made the UIAlertView class to be used as is and the view hierarchy for this class is private.
Subclassing Notes
The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.
Subclassing note taken from UIAlertView Apple documentation.
In essence what this means is that if you use the addSubview: method on a UIAlertView then you are in essence altering something that Apple as indicated as being private and your app would get rejected from the Apple App Review Process for rule 2.5.
2.5 Apps that use non-public APIs will be rejected
You might be asking yourself why does that method even exist then for UIAlertView surely because its there we can use it. Well NO the reason it is there is because the UIAlertView class is itself is a subclass of UIView which is where the addSubview: method is declared. Unfortunately there is no why to stop a instance of UIAlertView from actually calling that method. So what Apple have done is they have overridden the addSubview: method and all it does is it returns. So this method makes it do nothing and any views you pass to this method will never get added, as it never calls [super addSubview:view];.
So when it comes to UIAlertViews there are two things that you shouldn't be doing and these are:
Subclass UIAlertView like #interface MYAlertView : UIAlertView this is subclassing and is not allowed.
Alter View Hierarchy like [alertView addSubview:view];
However there is one point I should make, whilst we aren't allowed to subclass UIAlertView we are still allowed to make categories for it like #interface UIAlertView (MyCategory) as this is not classed as subclassing it is known as a class category or class extension (I've also heard it called a class provider before).
It should also be noted that if you are developing for iOS8 and above you should be using UIAlertController as the UIAlertView has been deprecated.
Important:
UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated.) To create and manage alerts in iOS 8 and later, instead use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert.
So I'd either use one of the custom classes mentioned in the other answers or move to use UIAlertController or if you have the time make your own custom alert view.
I hope this explanation helps you understand UIAlertViews more.

Don't go for all the bluffs. Just use the following code:
[login setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
And to handle the inputs of the user, use this:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if([[alertView textFieldAtIndex:0].text isEqual:#"UserName"]&&[[alertView textFieldAtIndex:1].text isEqual:#"PassWord"])
{
//do your stuff here
[adminLoginButton setTitle:#"Logout" forState:UIControlStateNormal];
[adminLoginButton setImage:[UIImage imageNamed:#"Logout.png"] forState:UIControlStateNormal];
}
else
{
UIAlertView *errorMessage = [[UIAlertView alloc]initWithTitle:#"Authentication Error" message:#"Input is wrong" delegate:self cancelButtonTitle:#"Okay" otherButtonTitles: nil];
[errorMessage show];
}
}

Related

How to disable a UIAlertView button with ios 7?

I have a simple view with a button linked with "showAlert" method. When I click on this button, it displays a UIAlertView.
Before, with ios 6, I was using the following code to disable a UIAlertView button :
- (IBAction)showAlert:(id)sender
{
myAlert = [[UIAlertView alloc] initWithTitle:#"" message:#"" delegate:self cancelButtonTitle:#"Retour" otherButtonTitles:#"Button1", #"Button2", #"Button3", #"Button4", nil];
[myAlert show];
for(UIView *aView in myAlert.subviews)
{
if ([[[aView class] description] isEqualToString:#"UIAlertButton"])
{
UIButton *aButton = (UIButton *)aView;
if ([aButton.titleLabel.text isEqualToString:#"Button2"])
aButton.enabled = NO;
}
}
}
Now, with ios 7, it does not work... Why ?
Since iOS7 it's not possible to add or manipulate the subviews or a UIAlertView, you need to create your own, sorry.
Subclass UIView to create your own UIAlertView or use a 3rd party library.
Adding a subview to UIAlertView is not possible from iOS 7.
The only way is to go for a custom UIView subclass which can act as UIAlertView.
Github and this answer may get you a solution.
You can disable 1st other Button of UIAlertView using delegate method
-(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
return NO;
}
works in ios 7 also.

Textfield in alertview not working in ios7?

I have an application in which I'm using a specific design for a reason. I put a text field in an alert view above an otherbutton with a background image. Everything is working fine in ios 6 version.
UIAlertView *av=[[UIAlertView alloc] initWithTitle:#"fdhdj" message:#" hdfjkhfjkhdk" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:#" ",#"cancel",nil];
av.alertViewStyle = UIAlertViewStylePlainTextInput;
namefield = [[UITextField alloc] initWithFrame:CGRectMake(10.0,43.0, 264.0, 44.0)];
namefield.borderStyle = UITextBorderStyleNone;
namefield.background = [UIImage imageNamed:#"text_field_default.png"];
namefield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
namefield.textAlignment = UITextAlignmentCenter;
//[namefield setBackgroundColor:[UIColor whiteColor]];
[av addSubview:namefield];
[namefield release];
av.tag=12;
av.delegate=self;
[av show];
[av release];
But now in ios 7, I heard you can't easily alter the view hierarchy of a UIAlertView.
One alternative for this case is to set
alert.alertViewStyle = UIAlertViewStylePlainTextInput
But can we add that text field in wherever we want? As in my case above the first otherbutton.can anybody help me?
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Enter Student Name" message:#"" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Save",nil];
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
[alertView show];
i used to do like this ,and its working very fine
This is my component to support addSubview with alertView in iOS7.
CXAlertView - Custom alert-view which allow you to add view as main content.
The simple answer to your question is NO, you can't change anything in this testField for the UIAlertViewStylePlainTextInput and you shouldn't.
This is from Apple:
The UIAlertView class is intended to be used as-is and does not
support subclassing. The view hierarchy for this class is private and
must not be modified.
And unfortunately what you heard I heard you can't easily alter the view hierarchy of a UIAlertView is wrong, you cannot alter the view hierarchy of a UIAlertView in iOS7 at all.
There are a good alternative on the web, you can check in cocoacontrols.com
You can't easily alter the view hierarchy of a UIAlertView in iOS 7. (Nor should you; the documentation specifically tells you not to.) Head over to the developer forums to see a long discussion about it.
One alternative in your case is to set alert.alertViewStyle = UIAlertViewStylePlainTextInput; This will add a text field for you. You can access it in the UIAlertView delegate callback by using UITextField *textField = [alertView textFieldAtIndex:0];.

How to get user input using cocos2d

I am looking to prompt the user to enter his/her name at the beginning of a game I am building.
What is the best way to get input from the user in cocos2d?
Thank you,
Joey
Cocos2d doesn't have any text input controls but you can easily add UIKit controls to the scene in Cocos2d 2.0
[[CCDirector sharedDirector] view] addSubview:myTextField];
You can use a UIAlertView with a text field embedded.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"Done" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
//[alert release]; If not using ARC
To receive the events back from the UIAlertView you implement the UIAlertViewDelegate. In your header file add the delegate protocol to your interface
#interface BTMyScene : CCLayer <UIAlertViewDelegate>
Then in your implementation file add any of the methods from the delegate protocol you want to receive notifications for. You probably want this one
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
UITextField *textField = [alertView textFieldAtIndex:0];
NSString *name = textField.text;
}
I recommend reading the documentation for UIAlertView and UIAlertViewDelegate. You will see all the available methods that you can use.

Add Text in TweetSheet when an UIAlert is pressed

Can anyone tell me how i can add text in TweetSheet when an UIAlert Title button is pressed?
Currently i am using this code to achieve it, but failed so far.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
TWTweetComposeViewController *tweetSheet = [[TWTweetComposeViewController alloc] init];
if ([title isEqualToString:#"Add custom text"]) {
UIAlertView *alert= [[UIAlertView alloc] initWithTitle:#"Add a template" message:nil delegate:self cancelButtonTitle:#"Hi it was nice meeting you" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
if ([title isEqualToString:#"Hi it was nice meeting you"]) {
[tweetSheet setInitialText:#"hey it was nice meeting you"];
}
}
Can anyone tell me what's wrong here?
You're not presenting your TWTweetComposeViewController you can do this simply by adding :
[self presentModalViewController:tweetSheet animated:YES];
I guess this code is only for a simple test tweet.
For a serious project you should always:
Check if the user can send tweets by calling +(BOOL)canSendTweet before trying to show the TWTweetComposeViewController
If not sure about the properties you'll be setting (text, images, urls), you need to check if adding them was successful : -(BOOL)addImage:(UIImage *)image, -(BOOL)addURL:(NSURL *)url and -(BOOL)setInitialText:(NSString *)text all return YES if it was successful and NO if not.
You can set a completion handler to perform different actions depending on what the user decided (send tweet/cancel)
The WWDC session about Twitter integration from 2011 is very simple and detailed, you should watch it and read the documentation if you need more information.

Hiding Keyboard in iphone resingFirstResponder Not working

I have create UITextField with alert view. After typing on UITextField I want to hide keyboard when user click "done" button.
This is my code. When I run it it's hitting the hideKeyBoard method (I found out using NSLog and the debugging). But this code not hiding the keyboard. Please Help me.
- (IBAction)doAlertInput:(id)sender
{
UIAlertView *alertDialog;
alertDialog = [[UIAlertView alloc]
initWithTitle: #"Please Enter Your Email Address!" message:#"You won’t see me"
delegate: self cancelButtonTitle: #"Ok" otherButtonTitles: nil];
userInput=[[UITextField alloc] initWithFrame: CGRectMake(12.0, 70.0, 260.0, 25.0)];
userInput.returnKeyType = UIReturnKeyDone;
[userInput addTarget:self action:#selector(hideKeyBoard) forControlEvents:UIControlEventEditingDidEndOnExit];
[userInput setBackgroundColor:[UIColor whiteColor]];
[alertDialog addSubview:userInput];
[alertDialog show];
[alertDialog release];
}
-(void)hideKeyBoard
{
[userInput resignFirstResponder];
NSLog(#"Key Board Hid");
}
I think you're missing the delegate for your UITextField
userInput.delegate = self;
also implement the protocol and that should be it. Cheers.
After appering the key board its covering the alert view. So what i am doing is shift up my alert view little higher. Then it will be not a problem because key board doesn't cover the alert view any more.
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, 130.0);
[myAlertView setTransform:myTransform];
The first parameter of CGAffineTransformMakeTranslation() is the X coordinate to move the origin of the view to. The second parameter is the Y location to move the origin of the view to. 130.0 doesn't exactly center the alert view between the status bar and the keyboard.
You'll need to add these two lines somewhere between the UIAlertView instantiation and the [UIAlertView show] line. I put them directly before the show line.
Also add the CoreGraphics framework to your project.
I think this will help you. Thank you :)
You can also use this method.
First set delegate method in your .h file
UITextfieldDelegate using <>
and then in your .m file.
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Make sure do not forget to add textfield delegate,otherwise method -(BOOL) will not appear.