大家好我正在尝试添加一个在不同类中声明的自定义导航栏,并且有一个按钮可以执行推送操作。 我最初的问题是,当应用程序推动视图因为uinavicgationconreoller它崩溃时,我以某种方式修复了它现在它也崩溃了。这是我的代码:
self.toolbar = Toolbar()
self.addToolBar(self.toolbar)
这是我的类,它包含我调用的方法
#import "HomeViewController.h"
@implementation HomeViewController
-(void)viewDidLoad
{
[super viewDidLoad];
TopBar *navBar = [TopBar new];
[navBar addTopBarToScreen:self.view];
}
-(void)productSheetsButtonFunction
{
MainViewController* productSheetsView = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
[self.navigationController pushViewController:productSheetsView animated:YES];
}
我知道问题是当我将按钮的目标添加到自我
时答案 0 :(得分:3)
addTopBarToScreen
可能属于另一个类,与HomeViewController
无关,并且未定义productSheetsButtonFunction
。您可以通过多种方式执行此操作,但最简单的方法是重用您的结构,即使用addTopBarToScreen
方法传递目标,如下所示:
- (void)addTopBarToScreen:(UIView *)screen target:(id)target
然后,在addTarget
电话中,您传递了该目标。
[productSheetsButton addTarget:target
action:@selector(productSheetsButtonFunction)
forControlEvents:UIControlEventTouchUpInside];
最后,在您的HomeViewController
中,您可以这样称呼它:
[navBar addTopBarToScreen:self.view target:self];
答案 1 :(得分:1)
您的操作方法格式错误。它应该是以下形式:
- (void) productSheetsButtonFunction:(id)sender;
然后,您想通过@selector(productSheetsButtonFunction:)
您似乎也在TopBar
中创建viewDidLoad
的实例,除了作为目标之外,它永远不会被使用。但是,返回时对此的唯一引用是在viewDidLoad
返回时作为原始引用的按钮。将addTopBarToScreen
作为类方法并将目标和选择器作为参数传递可能会更好。
+ (void)addTopBarToScreen:(UIView *)screen target:(id)target action:(SEL)action;
然后从你的主要代码电话:
[TopBar addTopBarToScreen:self.view target:self action:@selector(productSheetsButtonFunction:))];
您需要将productSheetsButtonFunction:
移动为家庭控制器的方法,而不是TopBar
。