【问题标题】:UIButton with pushViewController that is declared in another class带有在另一个类中声明的 pushViewController 的 UIButton
【发布时间】:2015-06-16 13:06:31
【问题描述】:

大家好,我正在尝试添加一个自定义导航栏,该导航栏在不同的类中声明,并且有一个按钮可以进行推送操作。 我最初的问题是,当应用程序由于 uinavicgationconreoller 崩溃而推送视图时,我以某种方式修复了该问题,现在它也崩溃了。这是我的代码:

#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];
}

这是我的类,它持有我调用的方法

-(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 时

【问题讨论】:

    标签: ios objective-c uiviewcontroller uibutton


    【解决方案1】:

    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];
    

    【讨论】:

      【解决方案2】:

      您的操作方法格式错误。它应该是以下形式:

      - (void) productSheetsButtonFunction:(id)sender;
      

      然后你想通过@selector(productSheetsButtonFunction:)引用它

      您似乎还在viewDidLoad 中创建了一个TopBar 的实例,除了作为目标外,它从未被使用过。但是,返回时对此的唯一引用是来自按钮,因为当viewDidLoad 返回时,您的原始引用会出现。将addTopBarToScreen 设为类方法并将目标和选择器作为参数传入可能会更好。

      + (void)addTopBarToScreen:(UIView *)screen target:(id)target action:(SEL)action;
      

      然后从你的主要代码调用:

      [TopBar addTopBarToScreen:self.view target:self action:@selector(productSheetsButtonFunction:))];
      

      您需要将productSheetsButtonFunction: 移动为家庭控制器的方法,而不是TopBar

      【讨论】:

        猜你喜欢
        • 2010-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-18
        • 2020-09-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多