I'm trying to add a new cell to a tableview, and pop up an alert with a UITextField to allow the user to input the title they wish to give the new cell. I have code to pop up an alert with a UITextField when the "+" button is pressed, and the code to add a new cell, however I don't know how to get the text from the UITextField to insert it into the cell's title.
This is my code to pop up the alert:
UIAlertView* alertPopUp = [[UIAlertView alloc] init];
[alertPopUp setDelegate:self];
[alertPopUp setTitle:#"Enter event title"];
[alertPopUp setMessage:#" "];
[alertPopUp addButtonWithTitle:#"Cancel"];
[alertPopUp addButtonWithTitle:#"OK"];
UITextField * eventNameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[eventNameField setBackgroundColor:[UIColor whiteColor]];
[alertPopUp addSubview:eventNameField];
[alertPopUp show];
and my alertView action is:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if([buttonTitle isEqualToString:#"Cancel"]) {
return;
}
else if([buttonTitle isEqualToString:#"Ok"]) {
}
}
What can I do to get the text from eventNameField when "Ok" is pressed and add it to a mutablearray named eventList? Thanks!
Set the tag on eventNameField to something meaningful
eventNameField.tag = 1001;
Then inside of the alertViewDelegate you can get the TextField by using - [UIView viewWithTag:]
UITextField* textField = (UITextField*)[alertView viewWithTag:1001];
eventNameField.text should give you the value
//declare the array
NSMutableArray* eventList = [NSMutableArray array];
//set its value
[eventList addObject:eventNameField.text];
DHamrick probably has the better solution, but found one more that could work.
You can get all the subviews inside alertView with [alertView subviews] (returns a NSCFArray), then you just have to find the one that is of class UITextField, in your case you could get it with:
[[[alertView subviews] objectAtIndex:4] text]
The old answers don't take advantage of changes in UIAlertView.
In iOS 5 and beyond, there is an easier way of using UITextfield on an alert view:
UIAlertView *alertPopUp = [[UIAlertView alloc] initWithTitle:#"Enter event title" message:#"" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
alertPopUp.alertViewStyle = UIAlertViewStylePlainTextInput;
self.alertTextField = [message textFieldAtIndex:0];
self.alertTextField.keyboardType = UIKeyboardTypeAlphabet;
alertPopUp.delegate = self;
[alertPopUp show];
[self.alertTextField becomeFirstResponder];
where alertTextField was set up like this:
#propery (nonatomic, strong) UITextField *alertTextField;
Then you can access alertTextField in your delegate response:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if([buttonTitle isEqualToString:#"Cancel"]) {
return;
}
else if([buttonTitle isEqualToString:#"Ok"]) {
NSLog(#"your text string is %#", self.alertTextField.text);
}
}
You could also just give the alertView a unique tag, and compare tag numbers, rather than save a reference to it using #property.
Related
I've added UITextView as a sub view of UIAlertView, I've added UIToolBar with a Done button (using UIBarButtonItem) as inputAccessoryView to allow user to resign keyboard`. It's just working fine in iOS6.0. And not in iOS5.0.
I've break point and checked in all the way I can, for reconfirming my self, I've made a sample project and checked the same in both the iOS versions, and the problem is same.
Here's the code, that's messing with me,
-(UIToolbar *)accessoryView
{
if (!accessoryView) {
accessoryView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44.0)];
UIBarButtonItem *btn = [[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(hideKeyBoard)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
accessoryView.items = [NSArray arrayWithObjects:flexibleSpace,btn, nil];
[accessoryView setTintColor:[[UIColor blackColor]colorWithAlphaComponent:0.5]];
}
return accessoryView;
}
-(IBAction) showAlertWithTextView: (id) sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithFrame:CGRectMake(0, 50, 320, 200)];
alert.title = nil;
alert.message = nil;
alert.delegate = self;
//textview I've added in .h file
textView = [[UITextView alloc] initWithFrame:alert.bounds];
textView.text = #"This is a UITextView";
textView.keyboardAppearance = UIKeyboardAppearanceAlert;
textView.editable = YES;
textView.inputAccessoryView = [self accessoryView];
[alert addSubview:textView];
[alert show];
}
- (void)hideKeyBoard {
[textview resignFirstResponder];
//[self.view endEditing:YES]; //this is also not worked
}
Here's list of steps I'm doing,
Showing Alert with Textview
Focus to Textview
Tap on Done button to resign TextView
Its not resigning in iOS5 but resigning in iOS6
Any idea? What's going wrong?
Actually your textview is not the first responder. The first responder is your alertView.
So you should try this....
[textField resignFirstResponder];
[alertView resignFirstResponder];
To use this code you must declare your alertView object in your.h file
I've solved it, as solution given by #iOS Developer, but as I've so many UIAlertView objects to handle, I've added a property of UIAlertView in delegate, and assigning it with the object of my alertview like this,
-(void)willPresentAlertView:(UIAlertView *)alertView
{
//set current alertview
[[AppDelegate sharedDelegate] setCurrentAlertView:alertView];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//will remove current alertview reference
[[AppDelegate sharedDelegate] setNilCurrentAlertView];
}
- (void)hideKeyBoard {
[textView resignFirstResponder];
//when need to resign UITextView, get current alertview object
UIAlertView *alertView = [[AppDelegate sharedDelegate] getCurrentAlertView];
if(alertView) {
[alertView resignFirstResponder];
}
}
In AppDelegate.m file,
#pragma mark - UIAlertView Resign
- (void) setCurrentAlertView:(UIAlertView *)alert{
self.alertObj = alert;
}
- (void) setNilCurrentAlertView {
self.alertObj = nil;
}
- (UIAlertView *)getCurrentAlertView {
return (UIAlertView *)self.alertObj;
}
As it is somewhat late but I am wondering that your code will not work in iOS7 or not as from iOS7 onwards UIAlertView is not allowing to add any subview (As you are adding UITextView as subview of UIAlertView).
If your requirement is not to compulsory using the UITextView then you can use UIAlertView with following configuration with default textfield in alert.
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Title"
message:#"Message"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
[message setAlertViewStyle:UIAlertViewStylePlainTextInput];
[message show];
And you can get value of this textfield in delegate of UIAlertView like this,
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
[alertView textFieldAtIndex:0].text;
}
I have 3 buttons in my UIActionsheet.
I want one cancel button at the bottom of it.
I also want that all of the buttons should be able to dismiss UIActionsheet.
Currently, none of them does the job for me.
Here is how I display it:
UIActionSheet *actionSheet = nil;
NSArray *otherButtons = nil;
otherButtons = [NSArray arrayWithObjects:#"Button1", #"Button2", #"Button3",
nil];
actionSheet = [[UIActionSheet alloc] initWithTitle:title
delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil, nil];
for( NSString *btntitle in otherButtons)
{
[actionSheet addButtonWithTitle:btntitle];
}
[actionSheet addButtonWithTitle:#"Cancel"];
actionSheet.cancelButtonIndex = actionSheet.numberOfButtons - 1;
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:self.view];
In my delegate, I do this:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
//some code
[actionSheet dismissWithClickedButtonIndex:buttonIndex animated:YES];
}
But it does not dismiss. I do not want to change my design and position of any buttons.
What should I do?
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
[actionSheet dismissWithClickedButtonIndex:buttonIndex animated:YES];
[self performSelector:#selector(OtherOperations) withObject:nil afterDelay:0.0];
}
-(void)OtherOperations{ //some code
}
Be careful when opening an action sheet from a lpgr. Only open it on "Began", not again on "Ended".
- (void) handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
CGPoint p = [gestureRecognizer locationInView:self.tblYourVids];
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
NSIndexPath *indexPath = [self.aTable indexPathForRowAtPoint:p];
if (indexPath == nil)
NSLog(#"long press on table view but not on a row");
else {
NSLog(#"long press on table view at row %d", indexPath.row);
self.selectedCell = [self.aTable cellForRowAtIndexPath:indexPath];
DLog(#"open action sheet only once");
[self showActionSheet];
}
} else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
// eat this one
}
}
you have to add this to yours .h file.
<UIActionSheetDelegate>
Also add this line to yours .m file.
actionSheet.delegate = self;
Let me know, this is working or not. If works, accept it as right answer
just try to use the below code.it will automatically dismiss.
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:#""
delegate:self
cancelButtonTitle:#"cancel"
destructiveButtonTitle:#"Printer"
otherButtonTitles: nil];
[sheet showInView:self.view];
[sheet release];
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
}
let me know whether it is working or not
Happy Coding!!!!!
This is not the right way to cancel UIActionSheet firstly you have to disable to default UIActionSheet by javascipt
and go through this web
http://www.icab.de/blog/2010/07/11/customize-the-contextual-menu-of-uiwebview/
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
warning: 'UIAlertView' may not respond to '-addTextFieldWithValue:label:'
I am using below code for put textfield in AlertView, it gives a warning like "UIAlertView may not respond - addTextFieldWithValue: label:" & "UIAlertView may not respond - textFieldAtIndex"...
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Email" message:#""
delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Submit", nil];
[alert addTextFieldWithValue:#"" label:#"Email"];
// Username
textfieldName = [alert textFieldAtIndex:0];
textfieldName.keyboardType = UIKeyboardTypeEmailAddress;
textfieldName.keyboardAppearance = UIKeyboardAppearanceAlert;
textfieldName.autocorrectionType = UITextAutocorrectionTypeNo;
[alert show];
please help!
there is no method "addTextFieldWithValue" for uialertview
if you need to modify your alert view have a look at my blog here - http://www.makebetterthings.com/blogs/iphone/add-uitextfield-in-uialertview/
These is no such methods like addTextFieldWithValue in UIAlertView.
[alert addTextFieldWithValue:#"" label:#"Email"]; is a private api. My app got rejected because of using this.
Use this
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:#"Twitter"
message:#"\n\n\n\n"
delegate:self
cancelButtonTitle:#""
otherButtonTitles:#"", nil];
myAlert.frame = CGRectMake( 0, 0, 300, 260);
textfieldName = [[UITextField alloc] initWithFrame:CGRectMake(13, 95, 260, 25.0)];
[textfieldName setBackgroundColor:[UIColor whiteColor]];
textfieldName.placeholder = #"xyz";
textfieldName.keyboardType = UIKeyboardTypeAlphabet;
textfieldName.keyboardAppearance = UIKeyboardAppearanceAlert;
textfieldName.autocorrectionType = UITextAutocorrectionTypeNo;
textfieldName.delegate = self;
textfieldName.tag = 1;
[myAlert addSubview:textfieldName];
**I use in my project < and use and implement
use in .h file**
#protocol InputAlertViewDelegate
-(void)handleEnteredValue:(NSString*)_value;
-(void)handleCancellation;
#end
#interface InputAlertView : UIAlertView <UIAlertViewDelegate> {
UITextField *textField;
id<InputAlertViewDelegate> caller;
} #property(nonatomic, retain) UITextField *textField;
#property(nonatomic, retain) id<InputAlertViewDelegate> caller;
-(NSString*) theText;
-(void) prepare;
-(void) makePassword;
-(void) makeTelephone;
+(InputAlertView*) inputAlertViewWithTitle:(NSString*)_title initialFieldText:(NSString*)_text caller:(id<InputAlertViewDelegate>)_caller;
**use in .m file**
+(InputAlertView*) inputAlertViewWithTitle:(NSString*)_title initialFieldText:(NSString*)_text
caller:(id<InputAlertViewDelegate>)_caller
{
InputAlertView *_alert =
[[[self alloc] initWithTitle:_title message:#"\n" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil] autorelease];
_alert.delegate = _alert;
_alert.caller = _caller;
[_alert prepare];
_alert.textField.text = _text;
return _alert;
}
-(void)prepare
{
self.textField = [[[UITextField alloc] initWithFrame:CGRectMake(12.0, 45, 260.0, 30.0)] autorelease];
[textField setBackgroundColor:[UIColor clearColor]];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
[self addSubview:textField];
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, 130.0);
[self setTransform:myTransform];
-(void) show{
[textField becomeFirstResponder];
[super show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 1){ [self.caller handleCancellation];
} else{
[self.caller handleEnteredValue:[self theText]];
}
}
Here is the code I have to create an UIAlertView with a textbox.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Enter A Username Here" message:#"this gets covered!"
delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:#"OK!", nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0, 60);
[alert setTransform:myTransform];
alert.tag = kAlertSaveScore;
[myTextField setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:myTextField];
[alert show];
[alert release];
[myTextField release];
My question is, how do I get the value from the textfield in:
- (void) alertView:(UIAlertView *) actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
}
I know I can get the standard stuff for the alertview such as actionSheet.tag and such, but how would I get the above created textfield?
#interface MyClass {
UITextField *alertTextField;
}
#end
And instead of declaring it locally, just use that.
//...
alertTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
//...
- (void) alertView:(UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *text = alertTextField.text;
alertTextField = nil;
}
Just give it a tag, and find it using the tag later. So, using your code:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Enter A Username Here" message:#"this gets covered!"
delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:#"OK!", nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0, 60);
[alert setTransform:myTransform];
alert.tag = kAlertSaveScore;
// Give the text field some unique tag
[myTextField setTag:10250];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:myTextField];
[alert show];
[alert release];
[myTextField release];
Then, in the call-back, wherever that happens to be and without having to worry about memory management or state management of the text field:
- (void) alertView:(UIAlertView *) actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
// Get the field you added to the alert view earlier (you should also
// probably validate that this field is there and that it is a UITextField but...)
UITextField* myField = (UITextField*)[actionSheet viewWithTag:10250];
NSLog(#"Entered text: %#", [myField text]);
}
Hey guys, I'm having trouble obtaining the value/text from a AlertView TextField. Maybe someone can look at my code and see what I'm doing wrong. Every time I press OK, the label get's a (null) value. I must be missing something here.
TextFieldINAlertViewController.h
#import <UIKit/UIKit.h>
#interface TextFieldInAlertViewController : UIViewController {
UITextField *myTextField;
IBOutlet UILabel *labelView;
NSString *FieldStr1;
}
#property (nonatomic, copy) NSString *FieldStr1;
#end
TextFieldInAlertViewController.m
#import "TextFieldInAlertViewController.h"
#implementation TextFieldInAlertViewController
#synthesize FieldStr1;
- (void)viewDidLoad {
[super viewDidLoad];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Enter Name Here" message:#"blank" delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:#"OK!", nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0, 60);
[alert setTransform:myTransform];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:myTextField];
[alert show];
[alert release];
}
-(void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)index
{
if (index == 0){
return;
}
if (index == 1){
FieldStr1 = myTextField.text;
labelView.text = [NSString stringWithFormat:#"%#" , FieldStr1 ];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[super dealloc];
}
#end
myTextField is a local variable in viewDidLoad, but it's also a member variable in your class. Get rid of the local declaration, and you should be fine. Also, I'd move this code to viewDidAppear:, rather than viewDidLoad.
Since iOS5 you can use property on the alertView which is a much simplier way to add a textField.
If you want to add a TextField to an UIAlertView, you can use this property (alertViewStyle) for UIAlertView:
- (void)showAlert{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"Done" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
((UITextField *) [alertView textFieldAtIndex:0]).text = #"Default Value";
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(#"%#", [alertView textFieldAtIndex:0].text);
}
Check UIAlertView reference: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html
Hugues