I have an alertview that is being created with a textfield inside of it. when i close the alertview via a submit or cancel button, i am getting the wait_fences error in the console. it doesnt crash, or i havent been able to make it crash but id really like to figure out what is going on.
alert = [[UIAlertView alloc]
initWithTitle:#"Lookup"
message:#"\n\n\n"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Submit", nil];
label = [[UILabel alloc] initWithFrame:CGRectMake(12, 40, 260, 25)];
label.font = [UIFont systemFontOfSize:16];
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
label.shadowColor = [UIColor blackColor];
label.shadowOffset = CGSizeMake(0, -1);
label.textAlignment = UITextAlignmentCenter;
label.text = #"Enter 10-Digit ISBN Number";
[alert addSubview:label];
field = [[UITextField alloc] initWithFrame:CGRectMake(16, 83, 252, 25)];
field.font = [UIFont systemFontOfSize:18];
field.backgroundColor = [UIColor whiteColor];
field.keyboardAppearance = UIKeyboardAppearanceAlert;
field.keyboardType = UIKeyboardTypeNumberPad;
field.borderStyle = UITextBorderStyleBezel;
field.delegate = self;
[field becomeFirstResponder];
[alert addSubview:field];
[alert show];
I looked around online to try and figure out what the problem was and some people had mentioned resigningFirst responder. I added that to - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex but it didnt do anything.
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
[field resignFirstResponder]
if (buttonIndex == 1) {
NSLog(#"Submit");
} else {
NSLog(#"Cancel");
}
}
I then added an if statement to try and find out if field was the first responder and i got nothing.
if([field isFirstResponder]) {
NSLog(#"field isFirstResponder");
}
does anyone have any suggestions of what i could have done wrong?
so i think ive got it figured out.
it seems that when i was calling [field resignFirstResponder] from alertView:didDismissWithButtonIndex, firstResponder wouldnt really resign and the keyboard would be forced out a second or two after the alertView had been removed. but when i call [field resignFirstResponder] from alertView:clickedButtonAtIndex it would resign firstResponder like it is supposed to.
I also ran into a problem were the alertview would move down before being removed, to compensate for the keyboard not being there, but to fix this i created a function thats sole purpose is to call [field resignFirstResponder] and called that from alertView:clickedButtonAtIndex and delayed it being run by .2 seconds.
Did you try removing textfield like:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
[[alertView.subviews objectAtIndex:0] removeFromSuperview];
}
Related
I have three text fields on an alertview, I set keyboard as decimalType ,
- (IBAction)heightMethod:(id)sender
{
self.utextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)]; utextfield.placeholder = #" Centimeters";
self.utextfield.delegate=self;
self.utextfield.tag=3;
[ self.utextfield setBackgroundColor:[UIColor whiteColor]];
[self.alertHeight addSubview: self.utextfield];
// Adds a password Field
self.ptextfield = [[UITextField alloc] initWithFrame:CGRectMake(40, 80.0, 80, 25.0)]; ptextfield.placeholder = #" Feet";
self.ptextfield.delegate=self;
self.ptextfield.tag=4;
[self.ptextfield setBackgroundColor:[UIColor whiteColor]];
[self.alertHeight addSubview:self.ptextfield];
self.ptextfieldInches = [[UITextField alloc] initWithFrame:CGRectMake(140, 80.0, 80, 25.0)]; ptextfieldInches.placeholder = #" Inches";
self.ptextfieldInches.delegate=self;
self.ptextfieldInches.tag=5;
[ptextfieldInches setBackgroundColor:[UIColor whiteColor]];
[self.alertHeight addSubview:ptextfieldInches];
[self.utextfield setKeyboardType:UIKeyboardTypeDecimalPad];
[self.ptextfieldInches setKeyboardType:UIKeyboardTypeDecimalPad];
[self.ptextfield setKeyboardType:UIKeyboardTypeDecimalPad];
[self.alertHeight show];
}
As I tap any textfield, keyboard resign only two times, but on third time its not resigning . I added resignfirst responder method inside the delgate method of alertview , look here
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
Create iVar of UITextField and assign in side UITextFieldDelegate method textFieldShouldBeginEditing. Hopefully it should work.
Like Below:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
textField = iVar;
}
Try
- (void) alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
[self.view endEditing: YES];
}
When you Press OK button on alert view the keypad dismisses automatically.I have tested it out so remove resignfirstresponder from the above mentioned method and try
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
UIAlertView not showing message text
I am trying to create a simple UIAlertView for an app in landscape mode but all I see is the title and buttons but no actual message.
It has something to do with the space available for the UIAlertView on the screen as if I remove a couple of the buttons then the message displays fine.
Is there a way to force the UIAlertView to resize itself to fit?
Relevant code is here
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Free Update",nil)
message:NSLocalizedString(#"There is a new version of this app available in the App Store. Download it now.",nil)
delegate:self
cancelButtonTitle:NSLocalizedString(#"Don't remind me",nil)
otherButtonTitles:NSLocalizedString(#"Download",nil), NSLocalizedString(#"Remind me later", nil), nil];
[alert show];
[alert release];
And this is the result:
a bit dirty but it works :o) just add a UILabel to the UIAlertView
UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake( alert.frame.origin.x + 10, 16, 260, 70)];
messageLabel.backgroundColor = [UIColor clearColor];
messageLabel.text = #"Your message";
messageLabel.textColor = [UIColor whiteColor];
messageLabel.lineBreakMode = UILineBreakModeWordWrap;
messageLabel.numberOfLines = 2;
messageLabel.font = [UIFont systemFontOfSize:12];
messageLabel.textAlignment = UITextAlignmentCenter;
[alert addSubview:messageLabel];
another dirty hack to get more space between title and buttons.
- (void)willPresentAlertView:(UIAlertView *)alertView {
float moreSpace = 20.0f;
alertView.frame = CGRectMake(alertView.frame.origin.x, alertView.frame.origin.y - moreSpace
,alertView.frame.size.width, alertView.frame.size.height + moreSpace*2 + 10.0f);
for ( UIView *view in [alertView subviews] ) {
if ( (view.class != UILabel.class ) && ( view.class != UIImageView.class ) ) {
[view setTransform:CGAffineTransformMakeTranslation(0, moreSpace * 2)];
}
}
}
In the end we had to drop one of the buttons from the alert view to make it work in landscape mode.
OK I am trying to retrieve a variable from this text field from this example http://junecloud.com/journal/code/displaying-a-password-or-text-entry-prompt-on-the-iphone.html. He says to do this :
"To get the text entered you just need to set a delegate for the text field and the alert, as shown in the example code above. Then you can use textFieldDidEndEditing: to get the value and store it somewhere temporary. When alertView:didDismissWithButtonIndex: is called, you can look up the saved value, and either use it or discard it depending on what button was pressed."
Thing is I'm so new to iOS and objective c that this means nothing to me. To me the text field delegate is set to self-- passwordField.delegate = self; Does anyone have an example to show? So I can see how to retrieve the entered text.
UIAlertView *passwordAlert = [[UIAlertView alloc] initWithTitle:#"Phone Number" message:#"\n\n\n"
delegate:self cancelButtonTitle:NSLocalizedString(#"Cancel",nil) otherButtonTitles:NSLocalizedString(#"OK",nil), nil];
UILabel *passwordLabel = [[UILabel alloc] initWithFrame:CGRectMake(12,40,260,25)];
passwordLabel.font = [UIFont systemFontOfSize:16];
passwordLabel.textColor = [UIColor whiteColor];
passwordLabel.backgroundColor = [UIColor clearColor];
passwordLabel.shadowColor = [UIColor blackColor];
passwordLabel.shadowOffset = CGSizeMake(0,-1);
passwordLabel.textAlignment = UITextAlignmentCenter;
passwordLabel.text =#"Cell Phone Number xxx-xxx-xxxx";
[passwordAlert addSubview:passwordLabel];
UIImageView *passwordImage = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"passwordfield" ofType:#"png"]]];
passwordImage.frame = CGRectMake(11,79,262,31);
[passwordAlert addSubview:passwordImage];
UITextField *passwordField = [[UITextField alloc] initWithFrame:CGRectMake(16,83,252,25)];
passwordField.font = [UIFont systemFontOfSize:18];
passwordField.backgroundColor = [UIColor whiteColor];
passwordField.secureTextEntry = YES;
passwordField.keyboardAppearance = UIKeyboardAppearanceAlert;
passwordField.delegate = self;
[passwordField becomeFirstResponder];
[passwordAlert addSubview:passwordField];
[passwordAlert setTransform:CGAffineTransformMakeTranslation(0,9)];
[passwordAlert show];
[passwordAlert release];
[passwordField release];
[passwordImage release];
[passwordLabel release];
Create the textFieldDidEndEditing: delegate method in the same class:
- (void)textFieldDidEndEditing:(UITextField *)textField {
NSString *textValue = textField.text;
NSLog(#"Value: %#", textValue);
}
//---------Searchoing----- //
- (IBAction) Search_hadit
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Search"
message:#" "
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Search",
nil];
CGRect frame = CGRectMake(14, 35, 255, 23);
myTextField = [[UITextField alloc] initWithFrame:frame];
myTextField.borderStyle = UITextBorderStyleBezel;
myTextField.textColor = [UIColor blackColor];
myTextField.textAlignment = UITextAlignmentCenter;
myTextField.font = [UIFont systemFontOfSize:14.0];
myTextField.placeholder = #"Enter Query";
myTextField.backgroundColor = [UIColor whiteColor];
myTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
myTextField.keyboardType = UIKeyboardTypeDefault;// use the default type input method (entire keyboard)
myTextField.returnKeyType = UIReturnKeyDone;
myTextField.delegate = self;
myTextField.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
[alert addSubview:myTextField];
[alert show];
[alert release];
}
When I click search button in my app alert view show...
I want when user click search button I get the text which he enter and send this data to another function on as he click search button where I can perform search. Any idea?
I guess (acording to the title) you'll need an UIAlertViewDelegate or at least the function:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString* buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle equalToString:#"Search"]) {
[do something];
}
}
But the text in your description describes a different problem than your title. I can't provide a solution to that, though.
I am attempting to compare two strings using "isEqualToString" and i cant seem to get it to work. Once it begins to run the if statement it quits the app. I'm quite sure that the strings are in fact equal to each other and despite my best attempts I'm out of ideas. Any help would be appreciated.
-(void)createTextfields{
name = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 140, 25)];
name.borderStyle = UITextBorderStyleRoundedRect;
name.textColor = [UIColor blackColor];
name.placeholder = #"Password";
name.textAlignment = UITextAlignmentCenter;
[self addSubview:name];
entry = [[UITextField alloc] initWithFrame:CGRectMake(170, 0, 140, 25)];
entry.borderStyle = UITextBorderStyleLine;
entry.textColor = [UIColor blueColor];
entry.textAlignment = UITextAlignmentCenter;
[self addSubview:entry];
}
-(void)createSubmitButton{
UIButton *submit = [UIButton buttonWithType:UIButtonTypeRoundedRect];
submit.titleLabel.textAlignment = UITextAlignmentCenter;
[submit setTitle:#"Submit" forState:UIControlStateNormal];
submit.frame = CGRectMake(90, 50, 60, 30);
[submit addTarget:self action:#selector(runSubmit) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:submit];
UIButton *savePass = [UIButton buttonWithType:UIButtonTypeRoundedRect];
savePass.titleLabel.textAlignment = UITextAlignmentCenter;
[savePass setTitle:#"Save" forState:UIControlStateNormal];
savePass.frame = CGRectMake(20, 50, 60, 30);
[savePass addTarget:self action:#selector(save) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:savePass];
}
-(void)save{
tempPass = name.text;
name.text = #"";
entry.text = tempPass;
}
-(void)runSubmit{
// password = name.text;
if ([tempPass isEqualToString:name.text]) {
//[viewController displayAlertFromViewControl];
}else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Correct" message:#"Alert" delegate: self cancelButtonTitle:#"Close" otherButtonTitles: nil];
[alert show];
[alert release];
}
}
Probably one of your strings (pretty sure that it is tempPass) is autoreleased
Where is tempPass being declared? Is it possible that it isn't being retained long enough to be referenced in the if?
If you run your program with this menu item: Run -> Run with Performance Tool -> Zombies
then Instruments will do all the hard work of tracking down the problem for you. It will show you which object was released early and where it was allocated.
It is probably tempPass. Once you determine if that is the case, it would be best to make tempPass a property with a copy attribute and then use self.tempPass=name.text instead of just tempPass=name.text.