textField does not show in the alertView - objective-c

Here is my code:
UIAlertView *theAlert = [[UIAlertView alloc] initWithTitle:#"Alert" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
UITextField *textField = [[UITextField alloc] initWithFrame: CGRectMake(12, 45, 260, 30)];
[textField setBackgroundColor: [UIColor clearColor]];
textField.borderStyle = UITextBorderStyleRoundedRect;
[theAlert addSubview:textField];
[theAlert show];
[textField release];
[theAlert release];
There is no textFiled in alertView, only a title "Alert" and a button "OK"

I will refer you the Apple Documentation in regards to UIAlertView Class Reference. Specifically
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
So what this means is you can't add subviews to an instance of UIAlertView the recent UIAlertView still has the addSubview: method is because UIAlertView is a subclass of UIView which has this method, but as of iOS7 the method addSubview: for UIAlertView no longer calls the super method on UIView it just does nothing.
So basically what you are after is not possible.
There are however alertViewStyles that you can use like UIAlertViewStylePlainTextInput which will add a single UITextField to the UIAlertView which you can then access using the method textFieldAtIndex:. However please be aware that you can't really do anything with this again because it is part of the UIAlertViews hierarchy so again it is made to be used as is.
Messing with the UIAlertView hierarchy will get your app rejected from the Apple Review process under
2.5 - Apps that use non-public APIs will be rejected

your background color is clear so It's not showing
[textField setBackgroundColor: [UIColor redColor]];.
and also show default text field
theAlert.alertViewStyle = UIAlertViewStylePlainTextInput;
//or for username and password
theAlert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
// or for password
theAlert.alertViewStyle = UIAlertViewStyleSecureTextInput;
Try this

You can use the default alertView with textfield.For that you need to set the alertViewStyle
[message setAlertViewStyle:UIAlertViewStylePlainTextInput];
Here message is object of alertView.
You can get the textfield value using the delegate method as shown below
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Login"]) {
UITextField *username = [alertView textFieldAtIndex:0];
UITextField *password = [alertView textFieldAtIndex:1];
NSLog(#"Username: %#\nPassword: %#", username.text, password.text);
}
}

Or you can 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];

If your using ios 7 then add below code to your set of code
theAlert.alertViewStyle=UIAlertViewStylePlainTextInput;
Full code
UIAlertView *theAlert = [[UIAlertView alloc] initWithTitle:#"Alert" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
UITextField *textField = [[UITextField alloc] initWithFrame: CGRectMake(12, 45, 260, 30)];
[textField setBackgroundColor: [UIColor greenColor]];
textField.borderStyle = UITextBorderStyleRoundedRect;
theAlert.alertViewStyle=UIAlertViewStylePlainTextInput;
[theAlert show];

Related

Extracting input from a UIAlertView text box Objective-C

I have gotten too frustrated from trying to find an answer to this, so I will as a question...
How can I get the raw text from a UIAlertView text box in Objective-C?
Here is my code:
UIButton *AppCrashButton = [UIButton buttonWithType: UIButtonTypeCustom];
AppCrashButton.frame = CGRectMake(0, (self.view.frame.size.height - 44) / 6 * 1, self.view.frame.size.width, (self.view.frame.size.height - 44) / 6);
[AppCrashButton setTitle: #"Ping" forState: UIControlStateNormal];
[AppCrashButton addTarget: self action: #selector(Click) forControlEvents: UIControlEventTouchUpInside];
AppCrashButton.backgroundColor = [UIColor colorWithRed:0.19 green:0.19 blue:0.19 alpha:1.0];
AppCrashButton.layer.borderColor = [UIColor colorWithRed:0.96 green:0.26 blue:0.21 alpha:1.0].CGColor;
AppCrashButton.layer.borderWidth = 0.5f;
[self.view addSubview: AppCrashButton];
-(void) Click {
UIAlertView *AppCrashAlert = [[UIAlertView alloc] initWithTitle: #"IP Ping" message: #"Please enter the IP address" delegate: self cancelButtonTitle: #"Cancel" otherButtonTitles: #"OK", nil];
AppCrashAlert.alertViewStyle = UIAlertViewStylePlainTextInput;
[AppCrashAlert show];
[AppCrashAlert release];
}
It shows a custom button, that once clicked, executes the "Click" method, which pings a host.
I was wondering how I could get the text from the text input.
Thanks
Set UIAlertViewDelegate delegate to Your controller
#interface YourViewController ()<UIAlertViewDelegate>
After that when you create your UIAlertView set it's delegate to Self In your case here
UIAlertView *AppCrashAlert = [[UIAlertView alloc] initWithTitle: #"IP Ping" message: #"Please enter the IP address" delegate: self cancelButtonTitle: #"Cancel" otherButtonTitles: #"OK", nil];
AppCrashAlert.alertViewStyle = UIAlertViewStylePlainTextInput;
// Set delegate
AppCrashAlert.delegate = Self;
[AppCrashAlert show];
[AppCrashAlert release];
Then,
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex(NSInteger)buttonIndex{
NSLog(#"Text : %#",[[alertView textFieldAtIndex:0] text]);
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(#"Entered: %#",[[alertView textFieldAtIndex:0] text]);
}
To get entered text in UIAlertView text field

UIAlertView does not work with UIBarButton

I have a strange problem:
Trying to call an action which contains NSLog and UIAlert using a UIBarButtonItem from a NavigationController. NSLog prints and UIAlert doesn't appear…
-(void)addNewItem
{
UIAlertView *alertAddnewItem = [[UIAlertView alloc]initWithTitle:#"Hello!" message:#"hello!" delegate:self cancelButtonTitle:#"NO" otherButtonTitles:#"YES", nil];
[self.view addSubview:alertAddnewItem];
NSLog(#"Hello!");
}
I guess the problems is with [self.view addSubview:alertAddnewItem];
Thank you!
call:
[alertAddnewItem show];
not
[self.view addSubview:alertAddnewItem];

how can an UIAlertView button calls another function?

-(void)buPressed{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Game Over"
message:#"YOU LOST! ALL YOUR BASE ARE BELONG TO US!"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Publish", nil];
[alertView show];
[alertView release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex==0){
NSLog(#"%d",buttonIndex);
}
else{
[self bPressed];
}
}
-(void)bPressed{
ModalViewConroller *yeniSayfa=[[ModalViewConroller alloc] init];
yeniSayfa.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:yeniSayfa animated:YES];
[yeniSayfa release];
//Restore to Defaults
[button_1 setSelected:NO];
[button_2 setSelected:NO];
[button_3 setSelected:NO];
[button_4 setSelected:NO];
[button_5 setSelected:NO];
[button_6 setSelected:NO];
slider.value=50.00;
UIImage *image = [UIImage imageNamed:#"Smiley_00025.png"];
imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(81, 43, image.size.width, image.size.height);
[self.view addSubview:imageView];
}
This is my code i want to make the publish button to call bPressed function but it is giving a warning and the program crashes when i touch the publish button i want to open a modalview when i push the publish button can anybody help me?
You need to declare the function in your header file so that other objects (in this case an instance of UIAlertView, since its delegate is set to your class) know that this method exists.
So, in your whatever_class.h file, add the following line below the #interface{ }:
-(void)bPressed;

UIAlertView causes crash in Release mode

If the Help option is activated in my app when the user goes to the camera I show a UIAlertView first with tips on how to take a picture:
-(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex != [actionSheet cancelButtonIndex]) {
NSString *selectedButtonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
if ([selectedButtonTitle isEqualToString:#"Camera"]) {
// If Help is activated display camera tips
if (helpEnabled == YES) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Camera Tips" message:#"\n\n\n\n\n\n\n\n\n\n\n\n" delegate:self cancelButtonTitle:nil otherButtonTitles:#"Go To Camera"];
UIImageView *cameraHelpView = [[UIImageView alloc] initWithFrame:CGRectMake(17, 40, 250, 255)];
UIImage *cameraTutorial = [UIImage imageNamed:#"Camera_Tips.png"];
cameraHelpView.image = cameraTutorial;
[alert addSubview:cameraHelpView];
[cameraHelpView release];
[alert show];
[alert release];
}
}
}
This works in Debug mode but causes an "EXC BAD ACCESS" error in Release mode. I can present a new view controller modally from this point just fine, however the UIAlertView will always crash the app. Why?
I don't know why it works in debug mode, but it looks like you are releasing your cameraHelpView while it is still being used. In the subviews of alert is a pointer to cameraHelpView; when you release that, it can no longer be accessed. I would suggest replacing all of your -[NSObject release] calls in this context with -[NSObject autorelease]. Thus:
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Camera Tips" message:#"\n\n\n\n\n\n\n\n\n\n\n\n" delegate:self cancelButtonTitle:nil otherButtonTitles:#"Go To Camera"] autorelease];
UIImage *cameraTutorial = [UIImage imageNamed:#"Camera_Tips.png"];
UIImageView *cameraHelpView = [[[UIImageView alloc] initWithFrame:CGRectMake(17, 40, 250, 255)] autorelease];
cameraHelpView.image = cameraTutorial;
[alert addSubview:cameraHelpView];
[alert show];
Go ahead and try that and let me know if it works. Good luck!
I found my mistake. I wasn't passing nil as the final argument to otherButtonTitles! Debug mode must see and fix this error for you. Hope this helps someone.

Fitting a UIDatePicker into a UIActionSheet

I'm trying to get a UIDatePicker with a UIButton to show up in a UIActionSheet. Unfortunately it gets cropped off and the entire Date Picker is not visible. I have not even attempted to add the UIButton yet. Can anyone suggest on getting the entire view to fit properly? I'm not sure how to add the proper dimensions as UIActionSheet seems to lack an -initWithFrame: type constructor.
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"Date Picker"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:nil];
// Add the picker
UIDatePicker *pickerView = [[UIDatePicker alloc] init];
pickerView.datePickerMode = UIDatePickerModeDate;
[menu addSubview:pickerView];
[menu showInView:self.view];
[pickerView release];
[menu release];
I've also tried with something similar to:
UIActionSheet *menu = [[UIActionSheet alloc] initWithFrame:CGRectMake(200.0, 200.0, 100.0f, 100.0f)];
The coords are ofcourse not realistic, but they don't seem to affect the position/size of the UIActionSheet.
You can use something like this (adjust the coordinates):
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"Date Picker"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:nil];
// Add the picker
UIDatePicker *pickerView = [[UIDatePicker alloc] init];
pickerView.datePickerMode = UIDatePickerModeDate;
[menu addSubview:pickerView];
[menu showInView:self.view];
[menu setBounds:CGRectMake(0,0,320, 500)];
CGRect pickerRect = pickerView.bounds;
pickerRect.origin.y = -100;
pickerView.bounds = pickerRect;
[pickerView release];
[menu release];
But you better create a fullscreen view with a UIDatePicker and a navigation bar. For an example see UICatalog -> Pickers sample from the iPhone DevCenter.
Try adding following line to resolve disabled buttons issue
[menu sendSubviewToBack:pickerView];
Dmitry's answer is almost correct. However, the frame of the actionsheet is too small, and therefore touches near the bottom of the picker are ignored. I have corrected these problems:
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"Date Picker"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"OK",nil];
// Add the picker
UIDatePicker *pickerView = [[UIDatePicker alloc] init];
pickerView.datePickerMode = UIDatePickerModeTime;
[menu addSubview:pickerView];
[menu showInView:_mainView];
CGRect menuRect = menu.frame;
CGFloat orgHeight = menuRect.size.height;
menuRect.origin.y -= 214; //height of picker
menuRect.size.height = orgHeight+214;
menu.frame = menuRect;
CGRect pickerRect = pickerView.frame;
pickerRect.origin.y = orgHeight;
pickerView.frame = pickerRect;
[pickerView release];
[menu release];
This works for me
NSString *title = UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation) ? #"\n\n\n\n\n\n\n\n\n" : #"\n\n\n\n\n\n\n\n\n\n\n\n" ;
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:[NSString stringWithFormat:#"%#%#", title, NSLocalizedString(#"SelectADateKey", #"")]
delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:#"Ok", nil];
[actionSheet showInView:self.view];
UIDatePicker *datePicker = [[[UIDatePicker alloc] init] autorelease];
datePicker.datePickerMode = UIDatePickerModeDate;
[actionSheet addSubview:datePicker];
a little tricky but it works!
Buttons at top, picker at bottom:
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"Date Picker"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"OK",nil];
// Add the picker
UIDatePicker *pickerView = [[UIDatePicker alloc] init];
pickerView.datePickerMode = UIDatePickerModeTime;
[menu addSubview:pickerView];
[menu showInView:_mainView];
CGRect menuRect = menu.frame;
menuRect.origin.y -= 214;
menuRect.size.height = 300;
menu.frame = menuRect;
CGRect pickerRect = pickerView.frame;
pickerRect.origin.y = 174;
pickerView.frame = pickerRect;
[pickerView release];
[menu release];
Make sure you show the UIActionSheet in the right view when inside a UITabBarController to avoid interactivity problems at the bottom:
[actionSheet showInView:self.parentViewController.tabBarController.view];
Possibly not directly answering the specific question (how to get the UIDatePicker to fit), but for an implementation of UIActionSheet which presents a UIDatePicker (also others) please see ActionSheetPicker. From it's Readme:
Easily present an ActionSheet with a PickerView, allowing user to select from a number of immutable options. Based on the HTML drop-down alternative found in mobilesafari.
Also:
Some code derived from marcio's post on Stack Overflow
[ Add UIPickerView & a Button in Action sheet - How? ]
Even if one doesn't use the code, it's good reading before you roll your own.
Update:
This version is deprecated: use ActionSheetPicker-3.0 instead.