带有在另一个 class 中声明的 pushViewController 的 UIButton
UIButton with pushViewController that is declared in another class
大家好,我正在尝试添加一个自定义导航栏,该导航栏在另一个 class 中声明,并且有一个可以执行推送操作的按钮。
我最初的问题是,当应用程序由于 uinavigationconreoller 崩溃而推送视图时,我以某种方式修复了它,现在它也崩溃了。这是我的代码:
#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];
}
这是我的 class 持有我调用的方法
-(void)addTopBarToScreen:(UIView *)screen
{
//create the top bar
UIView *topBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64.f)];
topBar.backgroundColor = [UIColor whiteColor];
[screen addSubview:topBar]
//add the productSheetsButton
UIButton *productSheetsButton = [[UIButton alloc] initWithFrame:CGRectMake(96.f, 14.f, 36.f, 36.f)];
productSheetsButton.backgroundColor = [UIColor clearColor];
[productSheetsButton setBackgroundImage:[UIImage imageNamed:@"ingredients.png"] forState:UIControlStateNormal];
[productSheetsButton setBackgroundImage:[UIImage imageNamed:@"ingredients_active.png"] forState:UIControlStateHighlighted];
[productSheetsButton addTarget:self action:@selector(productSheetsButtonFunction) forControlEvents:UIControlEventTouchUpInside];
[topBar addSubview:productSheetsButton];
}
我知道问题出在我将按钮的目标添加到 self
可能 addTopBarToScreen
在另一个 class 中,它与 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];
您的操作方法格式错误。它应该是这样的形式:
- (void) productSheetsButtonFunction:(id)sender;
然后您想要通过 @selector(productSheetsButtonFunction:)
引用它
您似乎还在 viewDidLoad
中创建了 TopBar
的实例,除了作为目标外从未使用过。然而,在 return 上对此的唯一引用来自按钮,因为您在 viewDidLoad
return 时的原始引用。将 addTopBarToScreen
设为 class 方法并将目标和选择器作为参数传递可能会更好。
+ (void)addTopBarToScreen:(UIView *)screen target:(id)target action:(SEL)action;
然后从你的主代码调用:
[TopBar addTopBarToScreen:self.view target:self action:@selector(productSheetsButtonFunction:))];
您需要移动 productSheetsButtonFunction:
成为家庭控制器的方法而不是 TopBar
。
大家好,我正在尝试添加一个自定义导航栏,该导航栏在另一个 class 中声明,并且有一个可以执行推送操作的按钮。 我最初的问题是,当应用程序由于 uinavigationconreoller 崩溃而推送视图时,我以某种方式修复了它,现在它也崩溃了。这是我的代码:
#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];
}
这是我的 class 持有我调用的方法
-(void)addTopBarToScreen:(UIView *)screen
{
//create the top bar
UIView *topBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64.f)];
topBar.backgroundColor = [UIColor whiteColor];
[screen addSubview:topBar]
//add the productSheetsButton
UIButton *productSheetsButton = [[UIButton alloc] initWithFrame:CGRectMake(96.f, 14.f, 36.f, 36.f)];
productSheetsButton.backgroundColor = [UIColor clearColor];
[productSheetsButton setBackgroundImage:[UIImage imageNamed:@"ingredients.png"] forState:UIControlStateNormal];
[productSheetsButton setBackgroundImage:[UIImage imageNamed:@"ingredients_active.png"] forState:UIControlStateHighlighted];
[productSheetsButton addTarget:self action:@selector(productSheetsButtonFunction) forControlEvents:UIControlEventTouchUpInside];
[topBar addSubview:productSheetsButton];
}
我知道问题出在我将按钮的目标添加到 self
可能 addTopBarToScreen
在另一个 class 中,它与 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];
您的操作方法格式错误。它应该是这样的形式:
- (void) productSheetsButtonFunction:(id)sender;
然后您想要通过 @selector(productSheetsButtonFunction:)
您似乎还在 viewDidLoad
中创建了 TopBar
的实例,除了作为目标外从未使用过。然而,在 return 上对此的唯一引用来自按钮,因为您在 viewDidLoad
return 时的原始引用。将 addTopBarToScreen
设为 class 方法并将目标和选择器作为参数传递可能会更好。
+ (void)addTopBarToScreen:(UIView *)screen target:(id)target action:(SEL)action;
然后从你的主代码调用:
[TopBar addTopBarToScreen:self.view target:self action:@selector(productSheetsButtonFunction:))];
您需要移动 productSheetsButtonFunction:
成为家庭控制器的方法而不是 TopBar
。