gpt4 book ai didi

objective-c - 如何获取UIBarButtonItem的目标

转载 作者:行者123 更新时间:2023-12-01 17:45:02 53 4
gpt4 key购买 nike

我在UIViewController子类中有以下代码:

MyViewController *viewController = [[MyViewController alloc] init];
viewController.title = title;

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"image"]
style:UIBarButtonItemStyleDone
target:self
action:@selector(mySelector)];
viewController.navigationItem.leftBarButtonItem = doneButton;


NSLog(@"Class name:%@",[viewController.navigationItem.leftBarButtonItem.target className]);

代码在NSLog语句上崩溃,并且警告说找不到className方法。目标属性不是对象,因此我应该可以在其上调用className?

我最终实际上想做的是从viewController调用mySelector而不按下按钮。有什么好方法吗?

最佳答案

要直接执行选择器,请尝试调用performSelector:

    [[doneButton target] performSelector:[doneButton action]];

关于objective-c - 如何获取UIBarButtonItem的目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8668995/

53 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com