Handling "OK" button from UIAlertView - objective-c

I have an alert view set up for two names to be entered like so
UITextField *player1;
UITextField *player2;
UIAlertView *prompt = [[UIAlertView alloc] initWithTitle:#"Enter 2 player names"
message:#"\n\n\n" // IMPORTANT
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
player1 = [[UITextField alloc] initWithFrame:CGRectMake(12, 50, 260, 25)];
[player1 setBackgroundColor:[UIColor whiteColor]];
[player1 setPlaceholder:#"player1"];
[prompt addSubview:player1];
player2 = [[UITextField alloc] initWithFrame:CGRectMake(12, 85, 260, 25)];
[player2 setBackgroundColor:[UIColor whiteColor]];
[player2 setPlaceholder:#"player2"];
[prompt addSubview:player2];
// set place
[prompt setTransform:CGAffineTransformMakeTranslation(0, 110)];
[prompt show];
//[prompt release];
// set cursor and show keyboard
[player1 becomeFirstResponder];
Now I would like to handle the "OK" button click. I'm attempting to do something like this with no luck..
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0)
{
NSLog(#"cancel");
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"OK works" message:#"no error" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
From what I've read this should work. However when I press the 'ok' button nothing happens.

You need to set self for delegate for the UIAlertView.
UIAlertView *prompt = [[UIAlertView alloc] initWithTitle:#"Enter 2 player names"
message:#"\n\n\n" // IMPORTANT
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];

Note: Not related original question but as per comment "Could you give me a few more details as to what you mean to assign tag to textfield and then get the textfield & its value? " I am posting answer here.
Assign tags to textfields
UIAlertView *prompt = [[UIAlertView alloc] initWithTitle:#"Enter 2 player names"
message:#"\n\n\n" // IMPORTANT
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
player1 = [[UITextField alloc] initWithFrame:CGRectMake(12, 50, 260, 25)];
[player1 setBackgroundColor:[UIColor whiteColor]];
[player1 setPlaceholder:#"player1"];
[player1 setTag:100]; // added this
[prompt addSubview:player1];
player2 = [[UITextField alloc] initWithFrame:CGRectMake(12, 85, 260, 25)];
[player2 setBackgroundColor:[UIColor whiteColor]];
[player2 setPlaceholder:#"player2"];
[player2 setTag:200]; // added this
[prompt addSubview:player2];
// set place
[prompt setTransform:CGAffineTransformMakeTranslation(0, 110)];
[prompt show];
//[prompt release];
// set cursor and show keyboard
[player1 becomeFirstResponder];
Retrieving value
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
NSLog(#"cancel");
}
else
{
UITextField *txtPlayer1 = (UITextField*)[alertView viewWithTag:100];
NSLog(#"Value for player1: %#",txtPlayer1.text);
UITextField *txtPlayer2 = (UITextField*)[alertView viewWithTag:200];
NSLog(#"Value for player2: %#",txtPlayer2.text);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"OK works" message:#"no error" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}

I use the following code to check the button index of the alertview.
as stated setting delegate to self too when showing the UIAlertview, then...
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
NSLog(#"index 0 pressed ie cancel button");
// do something when cancel pressed
}
else NSLog(#"index 1 pressed ie ok button");
// do something when ok button pressed
}

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

NSIndexSet not set when user press enter after NSAlert

I have a very simple Mac app, that at one particular time, a button brings up a NSAlert with an input field. After that I take some actions on a NSTableView.
It works fine if the user presses the OK button, but if the user presses ENTER, it does not define the selectedrows
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:#"Damage"];
[alert addButtonWithTitle:#"Ok"];
[alert addButtonWithTitle:#"Cancel"];
NSInteger damage = 0;
NSTextField *input = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 24)];
[input setStringValue:#""];
[[alert window]setInitialFirstResponder:input];
[alert setAccessoryView:input];
NSInteger button = [alert runModal];
if (button == NSAlertFirstButtonReturn) {
damage = [input integerValue];
} else if (button == NSAlertSecondButtonReturn) {
}
//data
NSIndexSet *set = [self.tableView selectedRowIndexes];
NSUInteger index = [set lastIndex];
The index has the value of NSNotFound.
Could someone help fix it?
Tks
So, I basically needed to add a key equivalent to the ENTER and it solved the issue:
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:#"Damage/Heal"];
[alert addButtonWithTitle:#"Ok"];
[alert addButtonWithTitle:#"Cancel"];
NSInteger damage = 0;
NSTextField *input = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 24)];
[input setStringValue:#""];
[[alert window]setInitialFirstResponder:input];
[alert setAccessoryView:input];
alert.buttons[0].keyEquivalent=#"\r";//new code
NSInteger button = [alert runModal];
if (button == NSAlertFirstButtonReturn) {
damage = [input integerValue];
} else if (button == NSAlertSecondButtonReturn) {
}

textField does not show in the alertView

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];

Actionsheet buttons do not respond when using showInView

I have this code on my program
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *saveMessageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[saveMessageBtn setImage:[UIImage imageNamed:#"btn_done.png"] forState:UIControlStateNormal];
[saveMessageBtn addTarget:self action:#selector(saveMessage) forControlEvents:UIControlEventTouchUpInside];
[saveMessageBtn setFrame:CGRectMake(0, 0, 49, 30)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:saveMessageBtn];
}
-(IBAction)saveMessage:(id)sender{
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:nil cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Send Now",#"Non Recurring",#"Recurring", nil];
[actionSheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *) actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0){
NSLog(#"Send Now");
}
else if (buttonIndex == 1){
[self performSegueWithIdentifier:#"modalNonRecurring" sender:self];
}
else if (buttonIndex == 2){
[self performSegueWithIdentifier:#"modalRecurring" sender:self];
}
else{
NSLog(#"Cancel Clicked");
}
}
as you can see in the code, it supposed to perform a segue or do 'NSLog' when a particular button clicked.
But when I click a button, it does not perform what I want it to do, instead it displays this message in the debug area..
Presenting action sheet clipped by its superview. Some controls might not respond to touches. On iPhone try -[UIActionSheet showFromTabBar:] or -[UIActionSheet showFromToolbar:] instead of -[UIActionSheet showInView:].
By the way, I am using a UINavigationController that is inside a UITabBarController.
Anyone that has a great idea how to fix this? your help would be much appreciated. Thanks!
Add UIActionSheetDelegate delegate in .h file and try like this then it'l work.
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:nil cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Send Now",#"Non Recurring",#"Recurring", nil];
actionSheet.delegate = self;
[actionSheet showInView:self.view];

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;