Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. @property (nonatomic, weak) UITextField *inputField;
  2. @property (nonatomic, strong) NSString *value;
  3.  
  4. -(void)promptEditCount {
  5. UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"Please enter a number" message:nil preferredStyle:UIAlertControllerStyleAlert];
  6. [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
  7. textField.textColor = [UIColor blueColor];
  8. textField.clearButtonMode = UITextFieldViewModeWhileEditing;
  9. textField.borderStyle = UITextBorderStyleNone;
  10. textField.keyboardType = UIKeyboardTypeNumberPad;
  11. textField.inputAccessoryView = [self accessoryViewForTextField:textField];
  12. _inputField = textField;
  13. }];
  14.  
  15. UIAlertAction *okAction = [UIAlertAction actionWithTitle:LocalizedString(@"ok") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  16. NSArray * textfields = alertController.textFields;
  17. UITextField * field = textfields[0];
  18. value = field.text;
  19. }];
  20. [alertController addAction:okAction];
  21. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:LocalizedString(@"cancel") style:UIAlertActionStyleCancel handler:nil];
  22. [alertController addAction:cancelAction];
  23.  
  24. [self presentViewController:alertController animated:YES completion:nil];
  25. }
  26.  
  27. -(UIView *)accessoryViewForTextField:(UITextField *)textField {
  28. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
  29. view.backgroundColor = [UIColor lightGrayColor];
  30.  
  31. UIButton *minusButton = [UIButton buttonWithType:UIButtonTypeCustom];
  32. UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
  33. [minusButton setTitle:@"-" forState:UIControlStateNormal];
  34. [doneButton setTitle:LocalizedString(@"done") forState:UIControlStateNormal];
  35. NSInteger buttonWidth = view.frame.size.width/4;
  36. minusButton.frame = CGRectMake(0, 0, buttonWidth, 44);
  37. doneButton.frame = CGRectMake(view.frame.size.width - buttonWidth, 0, buttonWidth, 44);
  38.  
  39. [minusButton addTarget:self action:@selector(minusTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
  40. [doneButton addTarget:self action:@selector(doneTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
  41.  
  42. [view addSubview:minusButton];
  43. [view addSubview:doneButton];
  44.  
  45. return view;
  46. }
  47.  
  48. -(void)minusTouchUpInside:(id)sender {
  49. UITextField *valueView = (UITextField *)_inputField;
  50. NSString *value = valueView.text;
  51. if (value.length == 0) {
  52. valueView.text = @"-";
  53. } else if (value.length > 0) {
  54. NSString *firstCharacter = [value substringToIndex:1];
  55. if ([firstCharacter isEqualToString:@"-"]){
  56. valueView.text = [value substringFromIndex:1];
  57. } else {
  58. valueView.text = [NSString stringWithFormat:@"-%@", value];
  59. }
  60. }
  61. }
  62.  
  63. -(void)doneTouchUpInside:(id)sender {
  64. [_inputField resignFirstResponder];
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement