3

似乎是一件容易的事。工具栏内的按钮(在键盘顶部)应该将发送者发送到函数。使用下面的代码,我在调试器中收到“发送到实例的无法识别的选择器”。

我的目标是访问自定义单元格的特定 TextField。该代码非常适合识别例如开关

工具栏声明:

UIToolbar* itemToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
itemToolbar.barStyle = UIBarStyleBlackTranslucent;
itemToolbar.items = [NSArray arrayWithObjects:
                     [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)],
                     [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                     [[UIBarButtonItem alloc]initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)],
                     nil];
[itemToolbar sizeToFit];

doneWithNumberPad 的功能:

- (void)doneWithNumberPad:(id)sender {
    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
    NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
    NSLog(@"%@", [object valueForKey:@"item"]);
}

谢谢你的帮助

4

1 回答 1

5

您只需在选择器方法中包含冒号即可保存按钮

itemToolbar.items = [NSArray arrayWithObjects:
                     [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)],
                     [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                     [[UIBarButtonItem alloc]initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad:)],
                     nil];

sender类型为NSObjectUIBarButtonItem在转换点之前将其转换为。

- (void)doneWithNumberPad:(id)sender {
    UIBarButtonItem *button = (UIBarButtonItem *)sender;
    CGPoint buttonPosition = [button convertPoint:CGPointZero toView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
    NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
    NSLog(@"%@", [object valueForKey:@"item"]);
}
于 2013-06-15T13:23:40.113 回答