How to disable UITextField editing but still accept touch?

后端 未结 15 2340
误落风尘
误落风尘 2020-11-29 19:55

I\'m making a UITextField that has a UIPickerView as inputView. Its all good, except that I can edit by copy, paste, cut and select te

15条回答
  •  半阙折子戏
    2020-11-29 20:10

    For an alternative that handles the UIPickerView and Action Sheets, checkout ActionSheetPicker

    https://github.com/TimCinel/ActionSheetPicker

    It's cocoapods enabled. It handles all of the cancel and done buttons on the Action Sheet. The examples within the sample project are great. I choose the ActionSheetStringPicker, which handles easily just String based options, but the API can handle most anything that I can think of.

    I originally started a solution much like the checkmarked answer, but stumbled onto this project and took me roughly 20 minutes to get things integrated into my app for usage including using cocopods: ActionSheetPicker (~> 0.0)

    Hope this helps.

    Download the git project and look at the following classes:

    • ActionSheetPickerViewController.m
    • ActionSheetPickerCustomPickerDelegate.h

    Here is roughly most of the code that I added, plus the *.h imports.

    - (IBAction)gymTouched:(id)sender {
          NSLog(@"gym touched");
    
          [ActionSheetStringPicker showPickerWithTitle:@"Select a Gym" rows:self.locations initialSelection:self.selectedIndex target:self successAction:@selector(gymWasSelected:element:) cancelAction:@selector(actionPickerCancelled:) origin:sender];
     }
    
    
    - (void)actionPickerCancelled:(id)sender {
        NSLog(@"Delegate has been informed that ActionSheetPicker was cancelled");
    }
    
    
    - (void)gymWasSelected:(NSNumber *)selectedIndex element:(id)element {
        self.selectedIndex = [selectedIndex intValue];
    
        //may have originated from textField or barButtonItem, use an IBOutlet instead of element
        self.txtGym.text = [self.locations objectAtIndex:self.selectedIndex];
    }
    
    
    -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
        return NO;  // Hide both keyboard and blinking cursor.
    }
    

提交回复
热议问题