Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @property (nonatomic, weak) UITextField *inputField;
- @property (nonatomic, strong) NSString *value;
- -(void)promptEditCount {
- UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"Please enter a number" message:nil preferredStyle:UIAlertControllerStyleAlert];
- [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
- textField.textColor = [UIColor blueColor];
- textField.clearButtonMode = UITextFieldViewModeWhileEditing;
- textField.borderStyle = UITextBorderStyleNone;
- textField.keyboardType = UIKeyboardTypeNumberPad;
- textField.inputAccessoryView = [self accessoryViewForTextField:textField];
- _inputField = textField;
- }];
- UIAlertAction *okAction = [UIAlertAction actionWithTitle:LocalizedString(@"ok") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
- NSArray * textfields = alertController.textFields;
- UITextField * field = textfields[0];
- value = field.text;
- }];
- [alertController addAction:okAction];
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:LocalizedString(@"cancel") style:UIAlertActionStyleCancel handler:nil];
- [alertController addAction:cancelAction];
- [self presentViewController:alertController animated:YES completion:nil];
- }
- -(UIView *)accessoryViewForTextField:(UITextField *)textField {
- UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
- view.backgroundColor = [UIColor lightGrayColor];
- UIButton *minusButton = [UIButton buttonWithType:UIButtonTypeCustom];
- UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
- [minusButton setTitle:@"-" forState:UIControlStateNormal];
- [doneButton setTitle:LocalizedString(@"done") forState:UIControlStateNormal];
- NSInteger buttonWidth = view.frame.size.width/4;
- minusButton.frame = CGRectMake(0, 0, buttonWidth, 44);
- doneButton.frame = CGRectMake(view.frame.size.width - buttonWidth, 0, buttonWidth, 44);
- [minusButton addTarget:self action:@selector(minusTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
- [doneButton addTarget:self action:@selector(doneTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
- [view addSubview:minusButton];
- [view addSubview:doneButton];
- return view;
- }
- -(void)minusTouchUpInside:(id)sender {
- UITextField *valueView = (UITextField *)_inputField;
- NSString *value = valueView.text;
- if (value.length == 0) {
- valueView.text = @"-";
- } else if (value.length > 0) {
- NSString *firstCharacter = [value substringToIndex:1];
- if ([firstCharacter isEqualToString:@"-"]){
- valueView.text = [value substringFromIndex:1];
- } else {
- valueView.text = [NSString stringWithFormat:@"-%@", value];
- }
- }
- }
- -(void)doneTouchUpInside:(id)sender {
- [_inputField resignFirstResponder];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement