I have got an alertview which has a text field
UIAlertView* dialog = [[UIAlertView alloc] init];
dialog.tag = 5;
[dialog setDelegate:self];
dialog.delegate = self;
[dialog setTitle:#"Please set the quantity:"];
[dialog setMessage:#"Quantity"];
[dialog addButtonWithTitle:#"Cancel"];
[dialog addButtonWithTitle:#"SET"];
UITextField *nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setBackgroundColor:[UIColor whiteColor]];
//nameField.keyboardType = UIKeyboardTypeDecimalPad;
NSString *str = nameField.text;
NSLog(#"%#",str);
[dialog addSubview:nameField];
[dialog show];
[dialog release];
[nameField release];
I'm trying to store the values of the textfield after the alertviews dismissal as
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.tag ==5) {
if(buttonIndex == 1)
{
UITextField* textField = (UITextField*)[alertView.subviews objectAtIndex:2];
NSLog(#"%#",textField.text);
}
}
}
But the text field is returning null, what is the value that im supposed to give or objectatindex?
Thanks.
Try this
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.tag ==5) {
if(buttonIndex == 1)
{
//UITextField* textField = (UITextField*)[alertView.subviews objectAtIndex:2];
//NSLog(#"%#",textField.text);
for (UIView *subview in [alertView subviews])
{
if ([subview isKindOfClass:[UITextField class]])
{
UITextField * textField =(UITextField *)subview;
NSLog(#"%#",textField.text);
}
}
}
}
}
Use the method instance method textFieldAtIndex: of UIAlertView.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.tag ==5) {
if(buttonIndex == 1)
{
UITextField* textField = [alertView.subviews textFieldAtIndex:2]; //make sure the index of the text field is correct.
NSLog(#"%#",textField.text);
}
}
}
NOTE: This method is available in iOS 5+
Related
I am opening an alert view when the view will appear. My alertview style is UIAlertViewStylePlainTextInput.I am saving the textfield text in NSUserDefaults. I want when the textfield have text in their textfield alert is not open But if the textfield is empty only then the alert is pop up on the screen.I am using the below code.enter code here
- (void)viewDidLoad {
[super viewDidLoad];
proAlert = [[UIAlertView alloc]initWithTitle:#"Pro-Tracking Number" message:#"Firstly enter the protracking number here" delegate:self cancelButtonTitle:#"Done" otherButtonTitles:nil];
proAlert.alertViewStyle = UIAlertViewStylePlainTextInput;
[proAlert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex==0)
{
proTextField.text = [[proAlert textFieldAtIndex:0]text];
}
}
proTextField = [[UITextField alloc]initWithFrame:CGRectMake(170, 35, 150, 40)];
proTextField.textColor=[UIColor blackColor];
//proTextField.placeholder = #"Pro/Tracking no";
NSUserDefaults *proNum = [NSUserDefaults standardUserDefaults];
proTextField.text = [proNum valueForKey:#"proTracking"];
[view2 addSubview:proTextField];
-(void)viewWillAppear:(BOOL)animated
{
[activity stopAnimating];
NSString *textString =[[proAlert textFieldAtIndex:0]text];
[proTextField.text length];
myText =textString;
NSLog(#"Textfield text - %#",myText);
NSUInteger length = [myText length];
NSLog(#"LENGTH of string %lu",(unsigned long)length);
if(myText<0)
{
proAlert.hidden = NO;
}
else
{
proAlert.hidden = YES;
}
}
-(void)viewWillAppear:(BOOL)animated {
if (txtField.text.length<=0) {
proAlert = [[UIAlertView alloc]initWithTitle:#"Pro-Tracking Number" message:#"Firstly enter the protracking number here" delegate:self cancelButtonTitle:#"Done" otherButtonTitles:nil];
proAlert.alertViewStyle = UIAlertViewStylePlainTextInput;
[proAlert show];
}
}
I'm attempting to create a simple webpage browser-esque application, where whenever the user wants to use a back, home, or forward function they are asked through an alert view to confirm.
The console window confirms that it has no problem calling the IBAction but comes up blank whenever I expect it to call Alert View.
Any help would be appreciated.
- (IBAction)controlPanelActions:(id)sender{
if (_controlPanel.selectedSegmentIndex == 0)
{
if ([_viewWeb canGoBack])
{
[self askToGoHome:nil];
}
}
- (IBAction)askToGoHome:(id)sender {
UIAlertView *alertDialog;
alertDialog.tag = 2;
alertDialog = [[UIAlertView alloc]
initWithTitle: #"Confirm"
message:#"Continue with Home Action?"
delegate: self
cancelButtonTitle: #"Yes"
otherButtonTitles: #"No", nil];
[alertDialog show];
}
- (void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if (alertView.tag == 1)
{
if ([buttonTitle isEqualToString:#"Yes"])
{
NSLog(#"back - yes");
[_viewWeb goBack];
}
}
if (alertView.tag == 2)
{
NSLog(#"home - pre-yes");
if ([buttonTitle isEqualToString:#"Yes"])
{
NSLog(#"home - yes");
HOMEPAGEURL = [[NSURL alloc] initWithString:HOMEPAGE];
[self.viewWeb loadRequest:[NSURLRequest requestWithURL:HOMEPAGEURL]];
}
}
if (alertView.tag == 3)
{
if ([buttonTitle isEqualToString:#"Yes"])
{
[_viewWeb goForward];
}
}
}
You set alertDialog.tag = 2;before you call init.
So,everytime you set tag,you set tag to a nil.It will not work.
Alertview alloc and init method sets its tag to 0.
So alertDialog.tag = 2;
will not work.
add this line after alloc and init method.
When I click on a button in one row, the button in a different row disappears. Why might this be happening?
I looked at the following question and all the other questions within it, but nothing really answers my issue.
Custom, Imageless UIButton title disappears
I used the Debug Color Blended Layers to see if it's just a color thing, but my button just appears to disappear completely. I suspected this was a button.hidden property thing so I hardcoded button.hidden = NO; but nothing has changed.
What went wrong here?
Table Control Code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([locationObjectsArray count] > 0)
{
return [locationObjectsArray count]+1;
}
else
return 1;
}
// Populate the Table View with member names
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier= #"Cell";
UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the Cell...
UIButton *selectButton = (UIButton *)[cell viewWithTag:1];
UILabel *cityNamesText = (UILabel *)[cell viewWithTag:2];
UIButton *editButton = (UIButton *)[cell viewWithTag:3];
//NSLog(#"[locationObjectsArray count]: %lu", (unsigned long)[locationObjectsArray count]);
if (indexPath.row >= [locationObjectsArray count]) {
// locationObjectsArray count == 0; Empty Array
cityNamesText.text = #"Add New Location";
NSLog(#"%ld: %#", (long)indexPath.row, #"Add New Location");
editButton.hidden = NO;
[editButton setTitle:#"Add" forState:UIControlStateNormal];
//[editButton setTitle:#"Add" forState:UIControlStateApplication];
selectButton.hidden = YES;
}
else if ([locationObjectsArray count] > 0) {
LocationObject *locObject = [locationObjectsArray objectAtIndex:indexPath.row];
NSLog(#"%ld: %#", (long)indexPath.row, [locObject getLocationName]);
cityNamesText.text = [locObject getLocationName];
selectButton.hidden = NO;
editButton.hidden = NO;
}
// Assign button tags
selectButton.tag = indexPath.row;
editButton.tag = indexPath.row;
[selectButton addTarget:self action:#selector(selectButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[editButton addTarget:self action:#selector(editButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
LocationObject *selectedLocationObject = [self loadLocationObjectWithKey:#"locObject"];
// Set Selected Cell to different Color
if ([cityNamesText.text isEqualToString:[selectedLocationObject getCityName]]) {
// Change to lightBlue color
UIColor * lightBlue = [UIColor colorWithRed:242/255.0f green:255/255.0f blue:254/255.0f alpha:1.0f];
[cell setBackgroundColor:lightBlue];
}
else
{
// All non-selected cells are white
//[cell setBackgroundColor:[UIColor whiteColor]];
//editButton.hidden = NO;
}
return cell;
}
// Select Button Clicked method
-(void)selectButtonClicked:(UIButton*)sender
{
if ([locationObjectsArray count] == 0)
{
NSLog(#"locObject count == 0");
// locationObjectsArray count == 0; Empty Array
// City name input is invalid
UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:#"No Locations Set"
message:#"Please add a new location."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
}
else
{
NSLog(#"locObject count > 0");
if (sender.tag >= locationObjectsArray.count) {
// Create local isntance of the selected locationObject
LocationObject *locObject = [locationObjectsArray objectAtIndex:sender.tag];
// Set locObject as current default locObject
[self saveLocationObject:locObject key:#"locObject"];
}
[mainTableView reloadData];
}
}
// Edit Button Clicked method
-(void)editButtonClicked:(UIButton*)sender
{
if ([locationObjectsArray count] == 0) {
// locationObjectsArray count == 0; Empty Array
UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:#"Add Location"
message:#"Input City Name"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles: nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert addButtonWithTitle:#"Save"];
[alert show];
}
else
{
selectedObjectInArray = sender.tag;
UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:#"Edit Location"
message:#"Input City Name"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles: nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert addButtonWithTitle:#"Save"];
[alert show];
}
}
// Handle alertView
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if ([alertView.title isEqualToString:#"Add Location"]) {
// Add Location Alert View
if (buttonIndex == 0)
{
NSLog(#"You have clicked Cancel");
}
else if(buttonIndex == 1)
{
NSLog(#"You have clicked Save");
UITextField *cityNameTextField = [alertView textFieldAtIndex:0];
NSString *saveLocationName = cityNameTextField.text;
NSLog(#"saveLocationName: %#", saveLocationName);
if ([self isLocationValid:saveLocationName] == YES) {
NSLog(#"location is valid. locationObjectsArray.count = %lu", locationObjectsArray.count);
if (locationObjectsArray.count == 0) {
locationObjectsArray = [NSMutableArray array];
}
// City name input is valid
LocationObject *locObject = [[LocationObject alloc] init];
[locObject setCityName:saveLocationName];
locObject.byCityName = YES;
[locationObjectsArray addObject:locObject];
NSLog(#"After addObject: locationObjectsArray.count = %lu", locationObjectsArray.count);
[self saveLocationArrayObject:locationObjectsArray key:#"locationObjectsArray"];
[mainTableView reloadData];
}
else
{
// City name input is invalid
UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:#"City Name Invalid"
message:#"Unable to locate input city."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
}
}
}
else if ([alertView.title isEqualToString:#"Edit Location"])
{
// Edit Location Alert View
if (buttonIndex == 0)
{
NSLog(#"You have clicked Cancel");
}
else if(buttonIndex == 1)
{
NSLog(#"You have clicked Save");
UITextField *cityNameTextField = [alertView textFieldAtIndex:0];
NSString *saveLocationName = cityNameTextField.text;
if ([self isLocationValid:saveLocationName]) {
// City name input is valid
int selectedIndex = (int)selectedObjectInArray;
LocationObject *locObject = [locationObjectsArray objectAtIndex:selectedIndex];
[locObject setCityName:saveLocationName];
[locObject setByCityName:(Boolean *)TRUE];
[locationObjectsArray setObject:locObject atIndexedSubscript:selectedIndex];
[self saveLocationArrayObject:locationObjectsArray key:#"locationObjectsArray"];
[mainTableView reloadData];
}
else
{
// City name input is invalid
UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:#"City Name Invalid"
message:#"Unable to locate input city."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
}
}
}
}
Before:
After check button is selected:
Your issue is in these lines in cellForRowAtIndexPath:
// Assign button tags
selectButton.tag = indexPath.row;
editButton.tag = indexPath.row;
The tags will get mixed up as the cells are reused, I would recommend trying to omit using tags in this situation and use e.g. IBOutlets as #rdelmar pointed out.
I have created textfields dynamically in UITableView I don't want to pass cursor without entering value in first textfield
[self.playerTable.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx,BOOL*stop)
{
UITableViewCell *cell = obj;
if([cell isKindOfClass:[UITableViewCell class]])
{
for(UITextField *textField in cell.contentView.subviews)
{
if([textField isKindOfClass:[UITextField class]])
{
if ([textField isFirstResponder])
{
[textField resignFirstResponder];
isEditMode = NO;
if(!isEditMode && [playerstr length] > 0)
{
NSMutableArray *playerinfoArry = [dbWrapper getPlayerInfo];
for (Playerinfo *player in playerinfoArry)
{
if ([player.playername isEqualToString:playerstr])
{
isPlayerExist = YES;
isEditMode = !isEditMode;
CustomAlert *alert = [[CustomAlert alloc] initWithTitle:#"" message:#"Please choose a different name" delegate:nil cancelButtonTitle:nil otherButtonTitle:#""];
[_nameField resignFirstResponder];
[alert showInView:self.view];
NSIndexPath *indexPath1=[NSIndexPath indexPathForRow:selectedRow inSection:0];
[_playerTable selectRowAtIndexPath:indexPath1 animated:YES scrollPosition:UITableViewScrollPositionTop];
return;
}
}
}
}
}
}
}
}];
instead of looking for the first responder while trying to edit another UITextField, have you tried the other approach: not permitting the UITextField to resign as first responder. This could be something like:
- (BOOL) textFieldShouldEndEditing:(UITextField *)textField{
for(UITextField *otherTextField in self.view)
{
if ([otherTextField isKindOfClass:[UITextField class]] && [textField.text isEqualToString:otherTextField.text]){
CustomAlert *alert = [[CustomAlert alloc] initWithTitle:#"" message:#"Please choose a different name" delegate:nil cancelButtonTitle:nil otherButtonTitle:#""];
[alert showInView:self.view];
return NO;
}
}
return YES;
}
I hope this helps.
i have been trying to copy text input from alertview textfield to a NSMutableArray that i will use later, alertview pops up and i enter the input to text field but when i press OK alert view disappears but doesnt copy text to my mutable array
here is my code
-(IBAction)add:(UIButton *)sender
{
addCustomStand = [[NSMutableArray alloc] init];
UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:#"Enter a Stand Location"
message:#" "
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
UITextField *nameField = [[UITextField alloc]
initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setBackgroundColor:[UIColor whiteColor]];
nameField.text = #"";
[dialog addSubview:nameField];
if ([nameField text]){
NSLog(#"Name Field %# ",nameField.text);
[addCustomStand addObject:nameField.text];
}
[nameField release];
[dialog show];
[dialog release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"OK"])
{
NSLog(#"Button 1 was selected.");
NSLog(#"StandLocations %# ",addCustomStand);
}
}
here is my output on log screen
2012-02-07 20:26:57.315 Avicii[1399:b603] Name Field
2012-02-07 20:26:59.720 Avicii[1399:b603] Button 1 was selected.
2012-02-07 20:26:59.721 Avicii[1399:b603] StandLocations (
""
)
anyone can help whats wrong with that code?
That's because [nameField text] doesn't have user entered value yet when you added it in your [addCustomStand addObject:nameField.text];
so change your adding into array in UIAlertView delegate method.
-(IBAction)add:(UIButton *)sender
{
addCustomStand = [[NSMutableArray alloc] init];
UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:#"Enter a Stand Location"
message:#" "
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
UITextField *nameField = [[UITextField alloc]
initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
[nameField setBackgroundColor:[UIColor whiteColor]];
nameField.text = #"";
// Note at this line
nameField.tag = 100;
//
[dialog addSubview:nameField];
[nameField release];
[dialog show];
[dialog release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"OK"])
{
// Note at this line
UITextField* nameField = (UITextField *)[alertView viewWithTag:100];
[addCustomStand addObject:nameField.text];
//
NSLog(#"Button 1 was selected.");
NSLog(#"StandLocations %# ",addCustomStand);
}
}
You are adding nameField.text to your addCustomStand array before you even show the alert dialog. At the time you add it to the array the value is an empty string.
Instead you need to copy the value into your array during your clickedButtonAtIndex: method, by doing something like this:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"OK"])
{
NSString *location;
UIView *view;
for (view in [alertView subviews]) {
if ([view isKindOfClass:[UITextField class]]) {
location = [(UITextField*)view text];
}
}
if (location) {
[addCustomStand addObject:location];
}
}
}