NSIndexSet not set when user press enter after NSAlert - objective-c

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) {
}

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

The added NSTextField on NSAlert doesn't allow to type text

I have used the following code to add a NSTextField onto the NSAlert successfully:
NSAlert *alert = [NSAlert alertWithMessageText: #"Password"
defaultButton:#"OK"
alternateButton:#"Cancel"
otherButton:nil
informativeTextWithFormat:#""];
NSTextField *input = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 24)];
[input setStringValue:#""];
[input autorelease];
[alert setAccessoryView:input];
NSInteger button = [alert runModal];
However the displayed NSTextField on NSAlert doesn't allow to type text, whatever I enter it does not show on the NSTextField. I also tried the set Enabled and Editable to YES but nothing changed.

set font size to UIActionSheet title

I need to set size UIActionSheet title "Select option to Copy"
with this code :
[[UIActionSheet alloc] initWithTitle:#"Select option to copy:" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil
otherButtonTitles:#"copy all ",nil];
You could take advantage of Runtime Properties to set the font. By using NSAttributedString you can achieve this.
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil
otherButtonTitles:#"copy all ",nil];
//Creating Attributed String with System Font size of 30.0f
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:#"Select option to copy:" attributes:#{NSFontAttributeName: [UIFont systemFontOfSize:30.0f]}];
//Accessing the Alert View Controller property from Action Sheet
UIAlertController *alertController = [actionSheet valueForKey:#"_alertController"];
//Setting Attributed Title property of Alert View Controller
[alertController setValue:attrString forKey:#"_attributedTitle"];
//Displaying the Action Sheet
[actionSheet showInView:self.view];
You can change the title font of uiactionsheet by using this delgate
-(void)willPresentActionSheet:(UIActionSheet *)actionSheet {
for (UIView *_currentView in actionSheet.subviews) {
if ([_currentView isKindOfClass:[UILabel class]]) {
UILabel *l = [[UILabel alloc] initWithFrame:_currentView.frame];
l.text = [(UILabel *)_currentView text];
[l setFont:[UIFont fontWithName:#"Arial-BoldMT" size:20]];
l.textColor = [UIColor darkGrayColor];
l.backgroundColor = [UIColor clearColor];
[l sizeToFit];
[l setCenter:CGPointMake(actionSheet.center.x, 25)];
[l setFrame:CGRectIntegral(l.frame)];
[actionSheet addSubview:l];
_currentView.hidden = YES;
break;
}
}
}

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

Handling "OK" button from UIAlertView

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
}