iphone - 如何在 UIToolbar 内的 UIBarButtonItem Action 中发送发件人

标签 iphone ios objective-c selector

看起来很简单。工具栏内的按钮(在键盘顶部)应该将发送者发送到一个函数。使用下面的代码,我在调试器中收到“发送到实例的无法识别的选择器”。

我的目标是访问自定义单元格的特定 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/

相关文章:

iphone - 如果你有很多静态数据,如何遵循 MVC?

ios - 为 iOS 应用程序中的所有 UITableViewCells 设置背景颜色

objective-c - Cocos2d v2 + 花栗鼠 : Fails when adding shapes to a CCLayer

ios - 在 NSXMLParser 方法 didFinishElement 中为 UITableView 调用 reloadData 无效

ios - Apple 拒绝在 iPhone 应用程序的 iPad 屏幕顶部出现黑条

iphone - 在横向模式下报告不正确的边界

ios - 快速切换与数组匹配的模式

ios - 如何在 Swift 3 中使用带有指数和模数的 RSA 来加密密码?

ios - NSCFConstantString objectAtIndex 无法识别的选择器发送到实例

iphone - 有人可以解释推送通知代码中的对象部分吗?