Laur Stefan
Laur Stefan

Reputation: 1589

UIButton with pushViewController that is declared in another class

Hi everyone I am trying to add a custom nav bar that is declared in a different class and has a button that make a push action. My initial problem was that when the app was pushing the view because of the uinavicgationconreoller it was crashing, I fixed that somehow and now it also crashes. Here is my code:

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

and this is my class that hold the method that I call

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

I know that the problem is when i add the target of the button to self

Upvotes: 0

Views: 163

Answers (2)

Rory McKinnel
Rory McKinnel

Reputation: 8014

Your action method has the wrong format. It should be of the form:

- (void) productSheetsButtonFunction:(id)sender;

You then want to refer to it via @selector(productSheetsButtonFunction:)

You also seem to create an instance of TopBar in viewDidLoad which is never used except as a target. However the only reference to this on return is from the button as your original reference goes when viewDidLoad returns. It would perhaps be better to make addTopBarToScreen a class method and to pass the target and selector in as arguments.

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

Then from you main code call:

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

You need to move productSheetsButtonFunction: to be a method of the home controller rather than TopBar.

Upvotes: 1

Tiago Almeida
Tiago Almeida

Reputation: 14237

Probably the addTopBarToScreen is in another class, that is unrelated to the HomeViewController and doesn't have the productSheetsButtonFunction defined. You can do this in several ways, but the simplest one, reusing your structure, is to pass the target in the addTopBarToScreen method, like this:

- (void)addTopBarToScreen:(UIView *)screen target:(id)target 

Then, in your addTarget call you pass that target.

 [productSheetsButton addTarget:target 
                         action:@selector(productSheetsButtonFunction) 
               forControlEvents:UIControlEventTouchUpInside];

Finally, in your HomeViewController you call it like this:

[navBar addTopBarToScreen:self.view target:self];

Upvotes: 3

Related Questions