IOS目标C选择器使用了错误的作用域
我使用一个自定义类来保存一组特定UITextField设置和inputAccessoryView的启动代码IOS目标C选择器使用了错误的作用域,ios,objective-c,xcode,scope,selector,Ios,Objective C,Xcode,Scope,Selector,我使用一个自定义类来保存一组特定UITextField设置和inputAccessoryView的启动代码 @interface CustomCountKeyboard : NSObject 它具有以下仅使用UITextField初始化的方法 -(id) initWithField:(UITextField *)field { self = [super init]; self.titleField = field; self.titleLabel = nil;
@interface CustomCountKeyboard : NSObject
它具有以下仅使用UITextField初始化的方法
-(id) initWithField:(UITextField *)field {
self = [super init];
self.titleField = field;
self.titleLabel = nil;
self.titleField.inputAccessoryView = [self makeToolBarWithWidth:self.titleField.superview.frame.size.width];
return self;
}
以及下面的方法,上面的init方法使用该方法来添加附件视图。下面代码中的doneBtn指定了DoneSelectingValue的选择器,该选择器在类的其他地方定义
-(UIToolbar *) makeToolBarWithWidth:(CGFloat) width {
UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, width, 44)];
[toolBar setTintColor:[UIColor blueColor]];
NSArray *segItemsArray = [NSArray arrayWithObjects: @"", @"c", @"+", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segItemsArray];
segmentedControl.frame = CGRectMake(0, 0, 200, 30);
segmentedControl.selectedSegmentIndex = 0;
UIBarButtonItem *segmentedControlButtonItem = [[UIBarButtonItem alloc] initWithCustomView:(UIView *)segmentedControl];
self.segmentedControl = segmentedControl;
UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(DoneSelectingValue)];
UIBarButtonItem *space=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolBar setItems:[NSArray arrayWithObjects:segmentedControlButtonItem,space,doneBtn, nil]];
return toolBar;
}
当未在此类上定义此DoneSelectingValue方法时,XCode会正确地警告我。该类已在其他地方使用,它正确地调用了该类中的DoneSelectingValue回调
但是,在本例中,它未能在类中调用此方法。困惑的是,我在周围的ViewController中添加了相同的方法定义,它成功地调用了它。这表明选择器的范围是错误的
此代码工作时使用与不工作时使用此实例的唯一区别在于,这次UITextField位于UITableViewCell中,CustomCountKeyboard的实例化发生在DidSelectRowatineXpath方法中。如果需要此代码,我可以编辑以提供
如果我输入断点,XCode inspector会在实例化时为“self”显示正确的值(CustomCountKeyboard实例),因此应该正确设置目标
有什么想法吗
编辑:根据要求,下面是CustomCount键盘的初始化代码
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if( self.textField.text.length > 0 ) {
NSDictionary *species =[self.filteredTableData objectAtIndex:indexPath.row];
[self.obs addObject:species];
[self.textField setText:@""];
[self.textField resignFirstResponder];
self.filteredTableData = [self calculateFilteredResults:self.textField.text];
[self.speciesTable reloadData];
}
else {
TestSpeciesCell *cell = [tableView cellForRowAtIndexPath:indexPath];
CustomCountKeyboard *ff = [[CustomCountKeyboard alloc] initWithField:cell.countField];
[ff StartSelecting];
}
}
开始选择添加的内容
- (void)StartSelecting {
if( self.titleLabel != nil ) {
self.titleLabel.hidden = true;
}
if( self.titleField != nil ) {
self.titleField.hidden = false;
[self.titleField becomeFirstResponder];
}
else {
[self.titleView becomeFirstResponder];
}
}
所以事实证明这很简单。在选择表行时,我正在实例化CustomCountKeyboard的自定义类,并使用它将inputAccessoryView附加到我传递给它的字段。不幸的是,uiBarButtonim上的target属性很弱,因此不会停止释放类 因此,当单击
Done
按钮时,方法已经完成,实例已经被垃圾收集,因此实际上并不存在。然后,系统会将动作选择器弹出,并在视图控制器上执行
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
TestSpeciesCell *cell = [tableView cellForRowAtIndexPath:indexPath];
CustomCountKeyboard *ff = [[CustomCountKeyboard alloc] initWithField:cell.countField];
[ff makeToolBarWithWidth:self.view.frame.size.width withTarget:ff];
[ff StartSelecting];
}
改为:
@property CustomCountKeyboard*计数选择器代码>
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
TestSpeciesCell *cell = [tableView cellForRowAtIndexPath:indexPath];
self.countPicker = [[CustomCountKeyboard alloc] initWithField:cell.countField];
[self.countPicker StartSelecting];
}
感谢Kampai和trojanfoe帮助我到达那里。如果可以,我会给你rep。你在哪里创建CustomCountKeyboard类的实例?确保,1。此时Self不是nil UIBarButtonItem*doneBtn=[[UIBarButtonItem alloc]initWithTitle:@“完成”样式:uibarbuttonimStyleplain目标:Self action:@selector(DoneSelectingValue)];2.CustomCountKeyboard类的实例未解除分配请共享完整代码,您尚未共享方法DoneSelectingValue
和startselection
的代码。您是否已在CustomCountKeyboard
类的.h
文件中添加了方法签名?是否可以显示完成SelectingValue
方法的声明/定义?我会说,委托方法不接收参数是不寻常的;例如,选择值所做的工作对委托很有用。我认为问题在于,您没有使用该类的实例调用选择器。从创建CustomCountKeyboard的类调用选择器。请遵循以下答案: