I have a UIAlertView with a textField on it and two buttons: Save & Cancel. When the Save button is tapped I am checking if the text field isn't empty and after if it is I simply want to change the textFields placeholder to: #"enter a name please" and KEEP the alert view on screen. However it is automatically dismissed.
How do I override that?
Add a target to the textfield in a subclassed alertView. You can subclass the alertView and not dismiss as described in this post
[[alertView textFieldAtIndex:0] addTarget:self action:#selector(textFieldDidChange) forControlEvents:UIControlEventEditingChanged];
Then write a function called textFieldDidChange that checks the current textfield of your alertView and set a boolean value so you know whether or not to dismiss the alert.
- (void) textFieldDidChange
{
NSString *alertViewText = [[alertView textFieldAtIndex:0] text];
if ([alertViewText isEqualToString:#""]) {
[alertView setMessage:#"Enter a name please."];
} else {
[alertView setMessage:#"Default Message"];
}
}
* Alternatively, I would suggest disabling "Save" when it is empty and not have to subclass. *
- (void) textFieldDidChange
{
NSString *alertViewText = [[alertView textFieldAtIndex:0] text];
if ([alertViewText isEqualToString:#""]) {
[alertView setMessage:#"Enter a name please."];
for (UIViewController *view in alertView.subview) {
if ([view isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)view;
if ([[[button titleLabel] text] isEqualToString:#"Save"])
[button setEnabled:NO];
}
}
} else {
[alertView setMessage:#"Default Message"];
for (UIViewController *view in alertView.subview) {
if ([view isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)view;
if ([[[button titleLabel] text] isEqualToString:#"Save"])
[button setEnabled:YES];
}
}
}
}
Related
I'm trying to change the color of the button in my UIActionSheet. The problem (a really weird problem) is that if the orientation of the device is Landscape the button color doesn't change!
Some screenshot:
I can't figure out why!
Here's the code:
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
[actionSheet.subviews enumerateObjectsUsingBlock:^(id _currentView, NSUInteger idx, BOOL *stop) {
if ([_currentView isKindOfClass:[UIButton class]]) {
[((UIButton *)_currentView).titleLabel setTextColor:[[MSAppDelegate sharedInstance] defaultColor]];
}
}];
/*
for (UIView *subview in actionSheet.subviews) {
if ([subview isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)subview;
[button setTitleColor:[[MSAppDelegate sharedInstance] defaultColor] forState:UIControlStateNormal];
[button setTitleColor:[[MSAppDelegate sharedInstance] defaultColor] forState:UIControlStateSelected];
}
if ([subview isKindOfClass:[UILabel class]]) {
UILabel *label = (UILabel *)subview;
label.textColor = [[MSAppDelegate sharedInstance] defaultColor];
}
}
*/
}
I also tried the commented code, both work in Portrait but not in Landscape!
By the way either in Landscape and in Portrait the code is executed!
If you want to see all the code here's the link
Thank you in advance!
You could Subclass the UIActionSheet class and implement the tableView:willDisplayCell:forRowAtIndexPath: selector:
#interface MSActionSheet : UIActionSheet
#end
#implementation MSActionSheet
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[[cell subviews] enumerateObjectsUsingBlock:^(id v, NSUInteger idx, BOOL *stop) {
[[v subviews] enumerateObjectsUsingBlock:^(id v2, NSUInteger idx, BOOL *stop) {
if ([v2 isKindOfClass:[UILabel class]]) {
[(UILabel *)v2 setTextColor:[[MSAppDelegate sharedInstance] defaultColor]];
}
}];
}];
}
#end
use this method to set button title color then i am sure your problem will solve
[[[actionSheet valueForKey:#"_buttons"] objectAtIndex:0] setTitleColor:[[MSAppDelegate sharedInstance] defaultColor] forState:UIControlStateNormal];
We are doing a validation in a UITableViewCell. The cell has a textView and a custom button on clicking which a pop over containing a UIDatePickerView should open & the user will select the date. Here selecting the date is mandatory. So we did a validation using the below code. In the textViewBeginEditing delegate method of the textView inside the cell in the immediate next row. i.e when the user clicks on the textView in the next row the previous row values will be validated.
Here the validation seems to be working but the cursor still blinks in current cell and do not move to the previous cell that is being validated.
UITableViewCell* myCell = (UITableViewCell*)textView.superview ;
UITextView *txtviewMycelltext;//=[[UITextView alloc]init];
for (UIView *view in myCell.subviews)
{
if([view isKindOfClass:[UILabel class]])
{
UILabel *lbl=view;
lbl.text=[lbl.text stringByReplacingOccurrencesOfString:#"." withString:#""];
int tablerowindex=[lbl.text intValue];
NSLog(#"%#",lbl.text);
break;
}
if([view isKindOfClass:[UITextView class]])
{
txtviewMycelltext=view;
}
}
inttablerowindex-=1;
if(inttablerowindex>0)
{
if([[arrActionvalues objectAtIndex:inttablerowindex1]objectForKey:KEY_FOLLOWUPDATE]==EMPTYSTRING)
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: #"Alert"
message: ALERT_FOLLOWUPDATE
delegate: nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
// UITableView *mytable=(UITableView *)((textView.superview).superview).superview;
UITableView *myTable=(UITableView*)[[myCell superview]superview];
NSIndexPath *indexPath = (NSIndexPath*)[myTable indexPathForCell:
(UITableViewCell*)textView.superview.superview];
UITableViewCell *previousCell = (UITableViewCell*)[myTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row - 1 inSection:indexPath.section]];
for (UIView *view in previousCell.subviews)
{
NSLog(#"%#",view);
if([view isKindOfClass:[UIView class]]) {
UIView *subView=view;
for(UIView *view2 in subView.subviews)
{
if([view2 isKindOfClass:[UITextView class]]) {
UITextView *txt=view2;
[txtviewMycelltext resignFirstResponder];
[txt becomeFirstResponder];
break;
}
}
}
}
}
}
Please let me know what needs to be done to get the cursor position to the correct cell.
It is not very clear from you code, but, I am guessing you are performing your validations inside textViewDidEndEditing.
textViewShouldEndEditing is the appropriate delegate method to perform validation of the text view contents. You can return NO if you want to prevent the switching of focus from the current text view.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextViewDelegate_Protocol/Reference/UITextViewDelegate.html#//apple_ref/occ/intfm/UITextViewDelegate/textViewShouldEndEditing:
I'm trying to implement custom AlertView.
The idea is to have alertview with textfield and cancel button.
What i can't do is to check textfield live for entered characters. I know i can do it using – alertViewShouldEnableFirstOtherButton: but i don't want another button. I wish to do the same just without button.
In android you can add listeners to textfields onchange.
Tried to do it using this uitextfield function, but it doesn't get called live or maybe i'm using it in a wrong way.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
textField = [alert textFieldAtIndex:0];
if ([textField.text length] == 0)
{
NSLog(#"Hello");
return NO;
}
return NO;
}
So how to do this properly?
try this
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"New List Item", #"new_list_dialog")
message:#"this gets covered" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
myTextField.delegate = self;
[myTextField setBackgroundColor:[UIColor whiteColor]];
[myAlertView addSubview:myTextField];
[myAlertView show];
[myAlertView release];
and textfield method
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSLog(#" %#", [textField.text stringByReplacingCharactersInRange:range withString:string]);
return YES;
}
You can add observer for the UITextFieldTextDidChangeNotification which will be posted whenever the text changes in textfield.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(controlTextDidChange:)
name:UITextFieldTextDidChangeNotification object:[alert textField]];
selector is below:
- (void)controlTextDidChange:(NSNotification *)notification {
{
if ([notification object] == [alert textField])
{
// [alert textField] has changed
}
}
EDIT : remove Observer when finish doing
[[NSNotificationCenter defaultCenter] removeObserver:UITextFieldTextDidChangeNotification];
I am trying to figure out how I can get notified when the keyboard changes. What I am trying to do is add a DONE button to keyboard of type 4 & 5 (NumberPad and PhonePad), everything is working fine, except when I transition from a TextField using a Default KB type, The notification that the KeyboardDidAppear isn't being fired.
Here is what I got:
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
}
Then I added a Property for the current KB type and the current TextField being edited:
#pragma mark - Delegate Methods
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
self.currentKBType = textField.keyboardType;
self.curTextField = textField;
return YES;
}
I then make a decision on whether or not to add that DONE button based on the current KB type:
- (void)keyboardDidShow:(NSNotification *)note {
if (self.currentKBType == 4 || self.currentKBType == 5) {
[self addButtonToKeyboard];
}
}
Problem is that the notification fires when the keyboard is displayed, but not when it changes (transitions from one TextField to another that specifies a different KB type.
Any suggestions? Am I missing something?
Got this figured out. Took a little logic, but it works flawlessly. Here is what I did:
Added private properties for:
#property (nonatomic) UIKeyboardType currentKBType;
#property(nonatomic,strong) UITextField *curTextField;
#property(nonatomic,strong) UIButton *doneButton;
#property(nonatomic) BOOL doneButtonDisplayed;
Then added the following logic in the both the TextField delegate method:
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
self.currentKBType = textField.keyboardType;
if (textField.keyboardType == 4 || textField.keyboardType == 5) {
if (!doneButtonDisplayed) {
[self addButtonToKeyboard];
}
} else {
if (doneButtonDisplayed) {
[self removeButtonFromKeyboard];
}
}
self.curTextField = textField;
return YES;
}
And in the KeyboardDidShowNotification that I signed the VC up for in the viewDidLoad:
- (void)keyboardDidShow:(NSNotification *)note {
if (self.currentKBType == 4 || self.currentKBType == 5) {
if (!doneButtonDisplayed) {
[self addButtonToKeyboard];
}
} else {
if (doneButtonDisplayed) {
[self removeButtonFromKeyboard];
}
}
}
And the two methods referenced in these methods:
- (void)addButtonToKeyboard {
// create custom button
doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
[doneButton setImage:[UIImage imageNamed:#"DoneNormal.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:#"DoneHL.png"] forState:UIControlStateHighlighted];
[doneButton addTarget:self action:#selector(resignKeyboard) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
if ([[[UIApplication sharedApplication] windows] count] > 1) {
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard found, add the button
if([[keyboard description] hasPrefix:#"<UIPeripheralHost"] == YES) {
[keyboard addSubview:doneButton];
self.doneButtonDisplayed = YES;
}
}
}
}
- (void)removeButtonFromKeyboard {
[doneButton removeFromSuperview];
self.doneButtonDisplayed = NO;
}
And finally, the resignKeyboard method that is called whenever the Done button is touched:
-(void)resignKeyboard {
[self.curTextField resignFirstResponder];
self.doneButtonDisplayed = NO;
}
This will add that Done button whenever a NumberPad or PhonePad type keyboard is displayed and remove it (only when it has been added) from the other keyboard types.
Tested and works great.
The below code shows that when a user does a long press gesture on a Table View Cell, then a UIActionSheet launches with a UITextField inside of it. When tapping the UITextField, the keyboard launches, and textFieldShouldBeginEditing and textFieldDidBeginEditing get called, but the text field won't accept the key taps.
Hitting the return key won't trigger the delegate methods, but tapping one of the UIActionSheet buttons will trigger textFieldShouldEndEditing and then textFieldDidEndEditing.
I'm setting the textField to become the first responder, so I'm not sure why it's not accepting input from the keyboard. Any suggestions?
- (void)longPress:(UILongPressGestureRecognizer *)gesture
{
// only when gesture was recognized, not when ended
if (gesture.state == UIGestureRecognizerStateBegan)
{
// get affected cell
SinTableViewCell *cell = (SinTableViewCell *)[gesture view];
// get indexPath of cell
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
// do something with this action
NSLog(#"Long-pressed cell at row %d", indexPath);
AppDelegate_Shared *appDelegate = (AppDelegate_Shared*)[UIApplication sharedApplication].delegate;
//setup UITextField for the UIActionSheet
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 170, 320, 200)];
textField.borderStyle = UITextBorderStyleBezel;
textField.backgroundColor = UIColorFromRGB(0XFFFFFF);
textField.text = #"";
textField.delegate = self;
[textField setKeyboardType:UIKeyboardTypeAlphabet];
[textField setKeyboardAppearance:UIKeyboardAppearanceAlert];
//setup UIActionSheet
UIActionSheet *asheet = [[UIActionSheet alloc] initWithTitle:#"Add Notes"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles: #"Save", nil];
[asheet showFromTabBar:appDelegate.tabBarController.tabBar];
[asheet setFrame:CGRectMake(0, 100, 320,380)];
[asheet insertSubview:textField atIndex:0];
//[textField becomeFirstResponder];
//memory management
[textField release];
[asheet release];
}
}
#pragma mark -
#pragma mark UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex {
}
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
}
#pragma mark -
#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
NSLog(#"textFieldShouldBeginEditing");
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
NSLog(#"textFieldDidBeginEditing");
[textField becomeFirstResponder];
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
NSLog(#"textFieldShouldEndEditing");
return YES;
}
//should save the notes value here, I think
- (void)textFieldDidEndEditing:(UITextField *)textField {
NSLog(#"textFieldDidEndEditing");
[textField resignFirstResponder];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField {
NSLog(#"textFieldShouldClearEditing");
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(#"in textFieldShouldReturn");
return YES;
}
Your question is a little bit old but not marked as answered, so if it is helpful for you or other viewers I post my solution from my own SO question. I started up with the same problem as you had and ended up in the fact that UIActionSheet really eats up some important events which are necessary to get the keyboard working properly.
My linked posted code unfortunately works only in portrait orientation, anyway it does it.
Is UITextFieldDelegate and UIActionSheetDelegate in your header?
If not, there could be problems while setting the textfield.delegate to self