【发布时间】:2025-11-30 00:40:01
【问题描述】:
所以我一直试图让这个pickerview 工作,但我就是无法让它工作。当我运行应用程序时,它在没有任何堆栈跟踪的情况下崩溃,并在 [textField becomeFirstResponder] 上显示线程 1:EXC_BAD_ACCESS。 pickerArray 是正确的,所以这不是问题。
#import "TestViewController.h"
#import "FindClasses.h"
@interface TestViewController ()
@property UIPickerView *picker;
@property NSArray *pickerArray;
@property (nonatomic, strong) FindClasses *finder;
@end
@implementation TestViewController
@synthesize finder = _finder;
- (FindClasses *)finder
{
if (!_finder) _finder = [[FindClasses alloc] init];
return _finder;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.pickerArray = [self.finder findClassesInTimetable];
self.classField.delegate = self;
self.picker = [[UIPickerView alloc] init];
self.picker.delegate = self;
self.picker.dataSource = self;
self.classField.inputView = self.picker;
// Do any additional setup after loading the view.
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[textField becomeFirstResponder];
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UIPickerView method implementation
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return self.pickerArray.count;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [self.pickerArray objectAtIndex:row];
}
谢谢。
【问题讨论】:
-
只需从 -
(BOOL)textFieldShouldBeginEditing:(UITextField *)textField委托方法中删除[textField becomeFirstResponder]; -
@SweetAngel 我这样做了,它不再崩溃,但也没有显示选择器视图。任何想法为什么?
-
Bcoz,默认情况下,此委托会自动将响应者设置为相应的
UITextField。 -
你只是想在用户点击 textField 时显示 pickerView 吗?
-
@GenieWanted 是的
标签: ios uitextfield uipickerview