0

I have following view hierarchy: navigation controller, inside it I pushed another view controller which contains UITableView with custom UIButtons in the cells. I have another view controller (MyCustomViewController2), which I want to show above all this stuff with animation.

But I am confused with this hierarchy and I don't know how to overwrite - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event method in my custom UIButton class. The code that I've come so far is:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    MyCustomViewController2 *myVC = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"MyVC"];
    [self.window addSubview: myVC.view];
}

But it's so bad! And I don't have any animations and I should delete it to remove. What can I try next?

halfer
  • 19,824
  • 17
  • 99
  • 186
user3742622
  • 1,037
  • 3
  • 22
  • 40

1 Answers1

0

It's highly recommended not to subclass a UIButton. So I wouldn't.

But since UIButton is a subclass of UIControl, you can use this method directly:

- (void)addTarget:(id)target
           action:(SEL)action
 forControlEvents:(UIControlEvents)controlEvents

Example in code could look like this:

[self.myButton addTarget:self action:@selector(showOtherVC) forControlEvents: UIControlEventTouchUpInside];

This will look at the target (self) for a particular method (showOtherVC) when the user lifts their finger from the button.

You could then have the current View Controller present the new one modally using this UIViewController method as example:

-(void)showOtherVC
{
     MyCustomViewController2 *myVC = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"MyVC"];
    [self presentViewController:myVC animated:true completion:nil];
}

Read more on UIControl here: UIControl Documentation

And check out other UIViewController modal transition styles here: UIViewController Documentation

Community
  • 1
  • 1
esreli
  • 4,993
  • 2
  • 26
  • 40