0

I have a UIToolbarButton which has a UIButton to hold the image. Now, when I click on this UIToolbarButton, I want to open another view in the current view.

My Code :

- (IBAction)btnNowPlaying:(id)sender
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(openPlaying:)];
    [self.view addGestureRecognizer:tap];
}

- (void)openPlaying:(UIGestureRecognizer *)sender
{
    NewMainViewController *vc  = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"Center"];
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController * npvc = [storyboard instantiateViewControllerWithIdentifier:@"NowPlayingVC"];
    [vc.containerView addSubview:npvc.view];
}

This action, btnNowPlaying is on the UIButton.

halfer
  • 19,824
  • 17
  • 99
  • 186
Vaibhav Jhaveri
  • 1,579
  • 3
  • 28
  • 53

2 Answers2

1

It sounds like you simply want to navigate from one view controller to another. Just control-drag from your bar button item to the view controller you'd like to navigate to in your storyboard. Then, click the segue between the two view controllers and set its identifier in the Attributes inspector. After that, all you need to do is implement prepareForSegue::

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"THE_IDENTIFIER_YOU_ENTERED_IN_THE_ATTRIBUTES_INSPECTOR"]) {
        if ([segue.destinationViewController isKindOfClass:[NEWViewController class]]) {
            NEWViewController *vc = (NEWViewController *)segue.destinationViewController;
            // Any preparation you need to do for next view controller
            vc.someProperty = someValue;
        }
    }
}

It's a little strange to open a new view controller inside the current view controller. Why not just segue to it?

Another option would be to alloc-init a UIView instead of a view controller and simply addSubview:.

trevorj
  • 2,029
  • 1
  • 16
  • 11
0

First of all, you can use default UIBarButton item with Custom style and setter Image property instead of creating UIButton.

You don't need to create additional gesture recogniser. If you have connect IBAction with button in Interface Builder, you can put your transition code inside btnNowPlaying method.

Another way is to create IBOutlet, connect it with your button and create UIGestureRecognizer in - (void)viewDidLoad method.

Ponf
  • 1,190
  • 1
  • 12
  • 28
  • - (void)viewDidLoad { [super viewDidLoad]; UIImage *i = [UIImage imageNamed:@"1.png"]; CGSize destinationSize = CGSizeMake(25, 25); UIGraphicsBeginImageContext(destinationSize); [i drawInRect:CGRectMake(0, 0, destinationSize.width, destinationSize.height)]; UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); btnNewImage.image = newImage; // btnNewImage.tintColor = [UIColor clearColor]; } – Vaibhav Jhaveri Sep 11 '14 at 06:14
  • You don't need such tricks to provide button with image. You can set them from IB: https://www.evernote.com/shard/s42/sh/6abdad17-b29f-46ac-83fb-729d5324075f/270fdd745a210d9215a13c44bb6e29bb After that make sure, that you have connected action to outlet: https://www.evernote.com/shard/s42/sh/17fa6e99-9d0a-4ee2-af63-e64934ae362a/4011bd805c807bc8b16a25e267b8b46c Does your method btnNowPlaying called when you press button? – Ponf Sep 11 '14 at 07:19
  • If not, more detailed answer how to connect IBAction to button is here: http://stackoverflow.com/questions/3419168/how-to-trigger-methods-with-uibuttonbaritem – Ponf Sep 11 '14 at 07:25