Ios 如何测试巴布通电是否连接到右侧IBAction?
我在一个普通的UIButton中看到了这一点:Ios 如何测试巴布通电是否连接到右侧IBAction?,ios,objective-c,uibutton,xctest,uibuttonbaritem,Ios,Objective C,Uibutton,Xctest,Uibuttonbaritem,我在一个普通的UIButton中看到了这一点: NSArray *actions = [viewController.addButton actionsForTarget:viewController forControlEvent:UIControlEventTouchUpInside]; XCTAssertTrue([actions containsObject:@”addNumbers:”], @””); 但现在我想为一个右翼巴布托教徒做同样的事情!我已经在VC上测试过这个按钮,但是没有
NSArray *actions = [viewController.addButton actionsForTarget:viewController forControlEvent:UIControlEventTouchUpInside];
XCTAssertTrue([actions containsObject:@”addNumbers:”], @””);
但现在我想为一个右翼巴布托教徒做同样的事情!我已经在VC上测试过这个按钮,但是没有针对目标的界面操作
我也试过了,但没有成功:
NSArray *actions = [self.navigationItem.rightBarButtonItem actionsForTarget:self forControlEvent:UIControlEventTouchUpInside];
或
它们中没有一个起作用。是否有人编写了UIBarButton的测试代码来检查按钮是否连接到正确的IBAction?您可以循环浏览导航栏的子视图,并找到包含RightBarButton项的整个UIControl allTargets方法 一旦有了它,就可以在该UIControl上调用actionForTarget:forControlEvents:操作,其中RightBarButtonim是目标,而UIControl的allControlEvents是forControlEvents参数:
//get the underlying control
for(UIControl * control in self.navigationController.navigationBar.subviews)
{
if([control isKindOfClass:UIControl.class])
{
//we found the right one
if([control.allTargets containsObject:self.navigationItem.rightBarButtonItem])
{
NSArray <NSString*> * actions = [control actionsForTarget:self.navigationItem.rightBarButtonItem forControlEvent:[control allControlEvents]];
//do something with actions.firstObject;
return;
}
}
}
//获取基础控件
用于(self.navigationController.navigationBar.Subview中的UIControl*控件)
{
如果([控件是类的种类:UIControl.class])
{
//我们找到了合适的
if([control.allTargets包含对象:self.navigationItem.RightBarButtonim])
{
NSArray*actions=[control actions ForTarget:self.navigationItem.RightBarButtonim forControlEvent:[control allControlEvents]];
//用动作做点什么;
返回;
}
}
}
但是,我相信UINavigationBar的子视图没有公开定义的布局,因此这是一个脆弱的解决方案。您可以循环浏览导航栏的子视图,并找到包含RightBarButtonim的整个allTargets方法 一旦有了它,就可以在该UIControl上调用actionForTarget:forControlEvents:操作,其中RightBarButtonim是目标,而UIControl的allControlEvents是forControlEvents参数:
//get the underlying control
for(UIControl * control in self.navigationController.navigationBar.subviews)
{
if([control isKindOfClass:UIControl.class])
{
//we found the right one
if([control.allTargets containsObject:self.navigationItem.rightBarButtonItem])
{
NSArray <NSString*> * actions = [control actionsForTarget:self.navigationItem.rightBarButtonItem forControlEvent:[control allControlEvents]];
//do something with actions.firstObject;
return;
}
}
}
//获取基础控件
用于(self.navigationController.navigationBar.Subview中的UIControl*控件)
{
如果([控件是类的种类:UIControl.class])
{
//我们找到了合适的
if([control.allTargets包含对象:self.navigationItem.RightBarButtonim])
{
NSArray*actions=[control actions ForTarget:self.navigationItem.RightBarButtonim forControlEvent:[control allControlEvents]];
//用动作做点什么;
返回;
}
}
}
但是,我相信UINavigationBar的子视图没有公开定义的布局,因此这是一个脆弱的解决方案