0

I`m developing a app using Storyboards. In one ViewController I have a button on navigationBar that links to the second ViewController. This transiction is defined in the storyboard (in this case I have defined a push segue to link the two ViewControllers)

I have changed the image of the button following this post in Stackoverflow.

But the problem is: That change in the View of the button breaks the push segue that I have defined in the storyboard. So the question is: How to still change the background of the BarButton without killing the segue action?

I dont want to programmatically reset the segue using performSegueWithIdentifier. This makes no sense since I already have defined it on the storyboard, so I think that must be another solution.

Community
  • 1
  • 1
Renato Lochetti
  • 4,558
  • 3
  • 32
  • 49

2 Answers2

1

You cannot use the quoted code and use the segue at the same time. The code introduced a new object, a UIButton that gets the click, so your storyboard object will not get it any more.

You could try adding a standard custom UIButton in storyboard and change the code as follows:

// instead of 
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];

// use
_someButton.frame = frameimg;

Assuming that your _someButton is the name for the IBOutlet of the button from storyboard.

If this does not work, you should go with performSegueWithIdentifier. I do not see the problem with this, either. You anyway include a @selector with the custom UIButton. Just use it to initiate the segue. You can still configure the segue, etc., in the storyboard, so nothing is lost, right? In fact, the refactoring above seems like more work.

Mundi
  • 79,884
  • 17
  • 117
  • 140
1

I think this is going to be your best solution:

In viewDidLoad:

self.navButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.navButton.frame = CGRectMake(0, 0, 30, 30);
[self.navButton setImage:[UIImage imageNamed:@"yourImage"] forState:UIControlStateNormal];
[self.navButton addTarget:self action:@selector(yourNavButtonAction) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *logOutBarButton = [[UIBarButtonItem alloc] initWithCustomView:self.navButton];

[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObject:navButton, nil]];

And in the method handling your navButton tap (yourNavButtonAction from above)

[self performSegueWithIdentifier:@"yourSegueIdentifier" sender:self];

**Note that this will require you to create a storyboard segue that originates from your ViewController itself, as opposed to a button on that ViewController. Control drag from your ViewController to the target ViewController, give the resulting segue an identifier (yourSegueIdentifier above) and you're set.

hgwhittle
  • 9,316
  • 6
  • 48
  • 60