How to navigate to next UIViewController using UIT

2019-08-31 08:04发布

问题:

I'm trying to use an UITextView as a button, and after clicking on the UITextView it should pop to to another UIViewController (not the root one, next one).

Using an UIButton works fine (by dragging to next UIViewController and choosing "Push" as the segue type, without doing any coding). But, doing the same thing with a UITextView does not work!

So, my question is how to navigate to the next view controller using an UITextField?

回答1:

Try adding a UITapGestureRecognizer to the text view and using that callback to trigger a segue.

I don't think you can do this without code.



回答2:

Add Tap Gesture to UITextView

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(mySelector)];
  tapRecognizer.numberOfTapsRequired = 1;
[_textView addGestureRecognizer:tapRecognizer];

Push or Present UIVieWController in UITapGestureRecognizer action method.

-(void)mySelector
        {
        // Navigate  to  next view controller
        }


回答3:

Set the userInteractionEnabled attribute?

textView.userInteractionEnabled = YES;

You should be able to use the textView like a button...



回答4:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myTapped:)];
tapRecognizer.numberOfTapsRequired = 1;
[txtView addGestureRecognizer:tapRecognizer];

    -(void)myTapped::(UIGestureRecognizer *)gesture
    {
                // Do what you want here
    }

try this code