看起来很简单。工具栏内的按钮(在键盘顶部)应该将发送者发送到一个函数。使用下面的代码,我在调试器中收到“发送到实例的无法识别的选择器”。
我的目标是访问自定义单元格的特定 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"]);
}
感谢您的帮助
最佳答案
您只需要在保存按钮的选择器方法中包含冒号
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
的类型为 NSObject
,在转换点之前将其转换为 UIBarButtonItem
。
- (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"]);
}
关于iphone - 如何在 UIToolbar 内的 UIBarButtonItem Action 中发送发件人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17124108/