TextAlign not working in UIAlertView? - ios

I want to Aligned my message text in UIAlertView and here is my code but when i running this code the text appear as before and nothing change after for loop. i also want to increase my text? I am using this code Align text in a UIAlertView that has a scrollbar right now.
UIAlertView *alret = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:#"%#",[title objectAtIndex:indexPath.row]]
message:[NSString stringWithFormat:#"%#",[description objectAtIndex:indexPath.row]]
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
for(id subview in alret.subviews) {
if([subview isKindOfClass:[UITextView class]]) {
[(UITextView *)subview setTextAlignment:NSTextAlignmentJustified];
}
}
[alret show];

Try this
for (UIView *view in alert.subviews) {
if([[view class] isSubclassOfClass:[UILabel class]]) {
((UILabel*)view).textAlignment = NSTextAlignmentLeft;
}
}
Hope it helps.

Related

AlertView triggering keyboard after dismissal

So this is happening.
I have one view controller and it handles all of the alert views in my app. I have another view controller that has a UITextView that the user can edit, and a save button.
When they hit the save button, if the text is already saved, it triggers an alert that asks them if they're sure they want to update, and if they confirm, it updates the file and gives them a second alert that says it was a success.
What's happening is that the keyboard keeps popping up when the second alert appears. I've tried resigning the keyboard and turning off the user interaction enabled flag on the text field as soon as the save button is hit.
self.storyEditorTextView.userInteractionEnabled=NO;
[self.storyEditorTextView resignFirstResponder];
I've also tried to turn it off when the alert is responded to (since some alerts can have a textfield).
To make matters worse, when I comment out the resign and userInteractionsEnabled lines, including the one in the alert, the keyboard still appears after the first alert is dismissed, disappears when the second alert is dismissed (if you can tap it because the keyboard covers it), and you can't tap into the UITextView and bring up the keyboard without going back to the parent view.
Here's the alert code.
- (void)addPromptToFavorites
{
// throw up an alert to confirm and get a name
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Name Your Favorite!"
message:#"Would you like to add a name to your prompt?"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK",nil];
// add a text field
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *textField = [alert textFieldAtIndex:0];
textField.text = #"My Great Prompt";
// set the tag
[alert setTag:SAVE_FAVE];
// Display the alert to the user
[alert show];
}
- (void)updateFave: (NSNumber *) theFaveId
{
NSLog(#"UPDATE FAVE\n\nself.sharedFaveMan.tempFave %#",self.sharedFaveMan.tempFave);
// NSMutableDictionary *faveDict =[[NSMutableDictionary alloc] init];
// loop through the favoritePrompts array until you find a match to the faveID
for (id element in self.sharedFaveMan.favoritePrompts) {
NSNumber *valueForID = [element valueForKey:#"id"];
if([valueForID isEqualToNumber:theFaveId])
{
self.sharedFaveMan.tempFave=element;
}
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Update Your Favorite!"
message:#"The story will be saved with the currently selected Favorite."
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK",nil];
//set the tag
[alert setTag:UPDATE_FAVE];
// Display the alert to the user
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
UITextField *textField = [alertView textFieldAtIndex:0];
[textField resignFirstResponder];
if (buttonIndex !=0)
{
if(alertView.tag==UPDATE_FAVE)
{
NSLog(#"Updating Fave");
// loop through the favoritePrompts array until you find a match to the faveID
int counter=0;
for (id element in self.sharedFaveMan.favoritePrompts) {
NSNumber *valueForID = [element valueForKey:#"id"];
if([valueForID isEqualToNumber:self.sharedFaveMan.theFaveID]){
break;
}
counter ++;
}
// update the pieces of the prompt
[[self.sharedFaveMan.favoritePrompts objectAtIndex:counter] setObject:self.sharedFaveMan.faveStoryText forKey:#"storyText"];
// save it
[self saveFavorites];
[[NSNotificationCenter defaultCenter]
postNotificationName:#"updateTheTable"
object:self];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Success!"
message:#"Favorite Updated!"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"OK",nil];
//play a sound
[self createSoundID: #"ticktock.aiff"];
//set the tag
[alert setTag:UPDATE_COMPLETE];
// Display the alert to the user
[alert show];
}
}
else if (buttonIndex == 0)
{
NSLog(#"%ld",(long)alertView.tag);
if(alertView.tag==SAVE_FAVE)
{
// they canceled the save
}
else if (alertView.tag==UPDATE_COMPLETE)
{
NSLog(#"Hit that");
[[NSNotificationCenter defaultCenter]
postNotificationName:#"dismissedDialogNotification"
object:self];
}
}
}
-(BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
UITextField *textField = [alertView textFieldAtIndex:0];
if (textField && [textField.text length] == 0)
{
return NO;
}
return YES;
}
Any ideas?
Here is the code
- (void)textViewDidEndEditing:(UITextView *)textView {
[textView resignFirstResponder];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
// Return FALSE so that the final '\n' character doesn't get added
return NO;
}
// For any other character return TRUE so that the text gets added to the view
return YES;
}
-(BOOL)textViewShouldEndEditing:(UITextView *)textView {
[textView resignFirstResponder];
return true;
}
and in alert method at the begining add this line of code
[self.view endEditing:YES];

UITextField In UIAlert Input Not Working iOS 7

I am currently using the following to use a popup alert which allows the user to set a delay in seconds;
The input itself works fine, but the number is not remembered, so if I put 5, or 10, when I press 'set' it just ignores that value and goes to instant recording again. here is the IBAction to call the setDelay, and below it, I have posted the Alertview
-(IBAction)setDelay:(id)sender
{
// open a alert with text field, OK and cancel button
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Set delay in seconds" message:#" "
delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Set", nil];
CGRect frame = CGRectMake(14, 45, 255, 23);
if(!delayTextField) {
delayTextField = [[UITextField alloc] initWithFrame:frame];
delayTextField.borderStyle = UITextBorderStyleBezel;
delayTextField.textColor = [UIColor blackColor];
delayTextField.textAlignment = UITextAlignmentLeft;
delayTextField.font = [UIFont systemFontOfSize:14.0];
delayTextField.backgroundColor = [UIColor redColor];
delayTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
delayTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation; // use the default type input method (entire keyboard)
delayTextField.returnKeyType = UIReturnKeyDefault;
delayTextField.delegate = self;
delayTextField.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
}
else
{
delayTextField.text=#"";
}
alert.delegate=self;
[alert addSubview:delayTextField];
[alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
[alert show];
[alert release];
}
//
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView.tag==100)
{
if (buttonIndex!=alertView.cancelButtonIndex)
{
[self featureButtonPressed];
}
}
else
{
if (buttonIndex==alertView.cancelButtonIndex)
{
self.delayTextField.text=#"";
}
}
}
From iOS 7 you can't add subviews to UIAlertView anymore -
UIAlertView addSubview in iOS7.
But if you need just simple UITextField in your UIAlertView you can just set UIAlertView style to UIAlertViewStylePlainTextInput and retrieve value from that field later. Eg.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Set delay in seconds" message:#" "
delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Set", nil];
alert.delegate=self;
[alert addSubview:delayTextField];
[alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
[alert show];
then in your delegate method:
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView.tag==100)
{
if (buttonIndex!=alertView.cancelButtonIndex)
{
[self featureButtonPressed];
}
}
else
{
if (buttonIndex==alertView.cancelButtonIndex)
{
UITextField *alertViewTextField = [alertView textFieldAtIndex:0];
//Do something with alertViewTextField.text value
}
}
}
#Guferos answer is absolutetly correct. It doesn't work because iOS 7 doesnt support this.
But in case you need more than a simple TextView, you can use a framework such as https://github.com/jdg/MBProgressHUD
:)

Check UITextFields for text

There is a view controller on my app in which the user enters personal information. There is a cancel option that pulls up an alert notifying them they will lose the data if it is not saved. I only want to display this alert if any text field in this view controller has [.text.length > 0] (I have about 20 text fields, if any have even 1 character it should pull up the alert). I could manually name every text field in an if statement but was hoping there was some way to check all the text fields in my view controller?
Here's what I have so far:
for (UIView *view in [self.view subviews]) {
if ([view isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField *)view;
if(textField.text.length >= 1){
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Cancel?"
message:#"If you leave before saving, the athlete will be lost. Are you sure you want to cancel?"
delegate:self
cancelButtonTitle:#"No"
otherButtonTitles:#"Yes", nil];
[alertView show];
}
if(textField.text.length == 0){
[[self navigationController] popViewControllerAnimated:YES];
}
}
}
I want to check if there are any text fields with value but this causes errors because its checking if textField.text.length == 0 before it finishes the for loop.
SOLUTION:
BOOL areAnyTextFieldsFilled = NO;
for (UIView *view in [self.view subviews]) {
if ([view isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField *)view;
if(textField.text.length >= 1){
areAnyTextFieldsFilled = YES;
}
}
}
if(areAnyTextFieldsFilled == YES){
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Cancel?"
message:#"If you leave before saving, the athlete will be lost. Are you sure you want to cancel?"
delegate:self
cancelButtonTitle:#"No"
otherButtonTitles:#"Yes", nil];
[alertView show];
}
else{
[[self navigationController] popViewControllerAnimated:YES];
}
You can achieve something very nice with tags. Set each one to a certain tag 1, 2, ... , 20. Then, iterate through with something like this:
for (int x = 1; x < 21; x++)
{
UITextField *aTextField = (UITextField *) [self.view viewWithTag:x];
//check here if text
}
Also, for additional efficiency, you could set a flag that starts as zero and if a user edits a textField, it will change to one. Then it would only loop if it is one.
However, for your current method what you could do is have a flag (yes, again, I love them :) ) And set it to zero before hand (probably in viewDidLoad). If you run into a text field with words, set it to 1. Erase the current if(textField.text.length == 0) statement. Then, afterwards just check if the flag still == 0 after the loop.

how to get value of textview from alertview [duplicate]

This question already has answers here:
Showing a alertview with a textbox in iPhone
(4 answers)
Closed 9 years ago.
i can't get the value of textView from my alertView
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(12, 50, 260, 100)];
[textView setText:#""];
UIAlertView *av = [[UIAlertView alloc]initWithTitle:#"put text\n\n\n\n\n\n" message:#"" delegate:self cancelButtonTitle:#"Annuler" otherButtonTitles:#"Envoyer",nil];
av.alertViewStyle = UIAlertViewStyleDefault;
[av addSubview:textView];
[av show];
it display alertview view with textview inside and its fine, i want to get the value of textview i my
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
}
can you help me please
Declare textView for use throughout your class
#implementation objectTables
{
UITextView *textView;
}
then in your
textView = [[UITextView alloc]initWithFrame:CGRectMake(12, 50, 260, 100)];
[textView setText:#""];
UIAlertView *av = [[UIAlertView alloc]initWithTitle:#"Push Notification\n\n\n\n\n\n" message:#"" delegate:self cancelButtonTitle:#"Annuler" otherButtonTitles:#"Envoyer",nil];
av.alertViewStyle = UIAlertViewStyleDefault;
[av addSubview:textView];
[av show];
and
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *textViewText = [textView text];
}
you can use - (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex which will return object of UITextField.
and from UITextField get text from it.
Or another way is
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
UITextField *TextField = (UITextField *)[alertView viewWithTag:YOURTAG];
}
You can try this :
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(12, 50, 260, 100)];
[textView setText:#""];
textView.tag = 10;
UIAlertView *av = [[UIAlertView alloc]initWithTitle:#"put text\n\n\n\n\n\n" message:#"" delegate:self cancelButtonTitle:#"Annuler" otherButtonTitles:#"Envoyer",nil];
av.alertViewStyle = UIAlertViewStyleDefault;
[av addSubview:textView];
[av show];
In Delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
UITextView *textView = (UITextView *) [alertView viewWithTag:10];
NSString *yourText = textView.text;
}
Just Declare Your UITextView in .h File
UITextView *textView;
and in .m file
textView = [[UITextView alloc]initWithFrame:CGRectMake(12, 50, 260, 100)];
[textView setText:#""];
UIAlertView *av = [[UIAlertView alloc]initWithTitle:#"put text\n\n\n\n\n\n" message:#"" delegate:self cancelButtonTitle:#"Annuler" otherButtonTitles:#"Envoyer",nil];
av.alertViewStyle = UIAlertViewStyleDefault;
[av addSubview:textView];
[av show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//Gives you the Value of UITextView
NSLog(#"%#",[textView text]);
}
Or you can define it as the property in Extended class(.m file) and access it.
#Interface yourINterfaceName()
{
#property(nonatomic , retain ) UITextView *textView;
}
Try this
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(12, 50, 260, 100)];
[textView setText:#""];
textView.tag =10;
UIAlertView *av = [[UIAlertView alloc]initWithTitle:#"put text\n\n\n\n\n\n" message:#"" delegate:self cancelButtonTitle:#"Annuler" otherButtonTitles:#"Envoyer",nil];
av.alertViewStyle = UIAlertViewStyleDefault;
[av addSubview:textView];
[av show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
UITextView *textView = (UITextView *)[alertView viewWithTag:10];
NSLog(#"%#",[textView text]);
}

adding action to alertView Button

I everybody,
I have 3 Buttons each one calls an AlertView with "Cancel" and "OK" and each "OK" Button goes to an different view.
for now I solved this with this
- (UIButton *)1_BTN
{
if (1_BTN == nil)
{
UIImage *buttonBackground = [UIImage imageNamed:#"1_btn.png"];
UIImage *buttonBackgroundPressed = [UIImage imageNamed:#"1_btn.png"];
CGRect frame = CGRectMake(655, 985, 107, 30);
1_BTN = [_IPadAppDelegate buttonWithTitle:#""
target:self
selector:#selector(1_BTN:)
frame:frame
image:buttonBackground
imagePressed:buttonBackgroundPressed];
[1_BTN setTag:1];
}
return 1_BTN;
}
......
- (void)1_BTN:(NSInteger *)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"some fancy text" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"ok", nil];
[alert setTag:[sender valueForKey:#"tag"]];
[alert show];
[alert release];
}
.......
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
if ([[alertView tag] isEqualToNumber:[NSNumber numberWithInt:1]]) {
something should happen.....
}
for all three Buttons and it works fine but for
[alert setTag:[sender valueForKey:#"tag"]];
and
if ([[alertView tag] isEqualToNumber:[NSNumber numberWithInt:1]]) {
i get this warning "Invalid receiver Type "NSInteger""
why is that so and how can I solve this better?
For one thing, you're doing this entirely wrong. Implement the UIAlertViewDelegate, specifically alertView:clickedButtonAtIndex: and check which button index (from 0 .. n where n is the last button). Act accordingly based on your known fixed indices.
Secondly, NSInteger is a scalar non-object type, and cannot receive messages. You would want an equality comparison instead, i.e., alertview.tag == 1. But as I said previously, don't do it that way.
The UIView property "tag" isn't an object, it is just a simple NSInteger. It's almost the same as "int" you probably know from C/C++. I corrected a few line in your code. Now it should work.
Another thing: The allocation of the UIButton seems a bit strange to me. Maybe you should check your memory management.
- (UIButton *)1_BTN
{
if (1_BTN == nil) {
UIImage *buttonBackground = [UIImage imageNamed:#"1_btn.png"];
UIImage *buttonBackgroundPressed = [UIImage imageNamed:#"1_btn.png"];
CGRect frame = CGRectMake(655, 985, 107, 30);
1_BTN = [_IPadAppDelegate buttonWithTitle:#"" target:self selector:#selector(1_BTNAction:) frame:frame image:buttonBackground imagePressed:buttonBackgroundPressed];
[1_BTN setTag:1];
}
return 1_BTN;
}
- (void)1_BTNAction:(UIButton *)sender {
NSInteger tagNumber = [sender tag];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"sone fancy text" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"ok", nil];
[alert setTag:tagNumber];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
if ([alertView tag] == 1) {
//something should happen
}
}
}

Resources