如何在UIToolbar内发送UIBarButtonItem内部的发件人

Pet*_*ter 3 iphone objective-c selector ios

看似简单的任务.工具栏内的按钮(位于键盘顶部)应将发件人发送到某个功能.使用下面的代码,我在调试器中收到"无法识别的选择器发送到实例".

我的目标是访问自定义单元格的特定TextField.该代码非常适用于识别例如交换机

ToolBar声明:

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];
Run Code Online (Sandbox Code Playgroud)

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"]);
}
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助

Anu*_*das 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];
Run Code Online (Sandbox Code Playgroud)

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"]);
}
Run Code Online (Sandbox Code Playgroud)