Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

titaniumdecoy

macrumors member
Original poster
Oct 13, 2005
86
0
I have a UITextView for which I want to allow zooming. I prepared a delegate class that implements the required methods and conforms to UIScrollViewDelegate. However when I set this class as the UITextView's delegate it does not work. What am I doing wrong?
 
In MainViewController.m, I have this code in viewDidLoad. MainViewController conforms to UIScrollViewDelegate.

Code:
UIScrollView *scrollView = (UIScrollView *)[myTextView superview];
[scrollView setDelegate:self];

However, this causes my app to crash on launch:

Code:
2008-09-14 11:15:48.226 MyApp[3264:20b] *** -[MainView setDelegate:]: unrecognized selector sent to instance 0x4609a0
2008-09-14 11:15:48.227 MyApp[3264:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[MainView setDelegate:]: unrecognized selector sent to instance 0x4609a0'
2008-09-14 11:15:48.228 MyApp[3264:20b] Stack: (
    <snip>
)
 
You have sent setDelegate: to an object that doesn't respond to it. Are you sure that's the correct method name? Are you sure scrollView is a UIScrollView? It seems odd that you are getting the super view of the text view. A UITextView is a UIScrollView via inheritance after all...
 
If I replace that code with [myTextView setDelegate:self]; I don't get an error but the text view is still not zoomable and I get a warning that says MainViewController does not implement UITextViewDelegate protocol.
 
If I replace that code with [myTextView setDelegate:self]; I don't get an error but the text view is still not zoomable and I get a warning that says MainViewController does not implement UITextViewDelegate protocol.

Is self an instance of MainViewController? I assume so. So the next question is: does it implement all required methods in the protocol and declare that it implements the protocol in the header?
 
MainViewController implements UIScrollViewDelegate and related methods. However, it does not implement the UITextViewDelegate protocol.
 
MainViewController implements UIScrollViewDelegate and related methods. However, it does not implement the UITextViewDelegate protocol.

Then it's not clear how you can set it as a delegate for a UITextView. Unlike desktop Cocoa textviews are not contained in scroll views: they are scroll views. Either implement the UITextViewDelegate protocol (blank methods), or find a way to call the method on the superclass...
 
Good point.

I tried implementing UITextViewDelegate and that made no difference (all methods are optional). Not sure what to do now, I guess I'll have to keep looking.
 
OK, I've thought about this and I have a potential solution.

Add a category to UITextView that adds a method to set the scroll delegate. This would allow you to set the scroll delegate without setting the text delegate. It's possible this might have unexpected consequences, but I would have thought it's pretty safe. Something like this:

In file UITextView+ScrollDelegate.h
Code:
#import <UIKit/UIKit.h>

@interface UITextView (ScrollDelegate)

-(void) setScrollDelegate:(id) scrollDelegate;

@end

In file UITextView+ScrollDelegate.m
Code:
#import "UITextView+ScrollDelegate.h"

@implementation UITextView (ScrollDelegate)

- (void) setScrollDelegate:(id) scrollDelegate
{
[super setDelegate:scrollDelegate];
}

@end
 
Thanks, robbieduncan. I got zooming working! I had to set the minimumZoomScale and maximumZoomScale as properties of the UITextView, rather than implementing them as delegate methods. I misread the docs.
 
I've not actually done anything with zooming myself, but I think I read somewhere it's up to you to zoom. Don't quote me on that. I can only suggest reading the documentation...
 
You missed my edit. Got it working. Scrolling and zooming work well together. It turns out I didn't need to use the category you wrote for me (thanks, by the way), I could just use set the delegate. It wasn't working before because I tried to implement methods that returned the max and min zoom values in the delegate rather than setting those values of the UITextView itself. Thanks again. :)

EDIT: There is one final problem. It turns out that when I am zoomed in such that the text goes off the edge of the screen, I can only scroll vertically and not horizontally. I have set the view to scroll horizontally in Interface Builder but apparently the view doesn't recognize that the content's size has changed.
 
Zoom in UITextView

You missed my edit. Got it working. Scrolling and zooming work well together. It turns out I didn't need to use the category you wrote for me (thanks, by the way), I could just use set the delegate. It wasn't working before because I tried to implement methods that returned the max and min zoom values in the delegate rather than setting those values of the UITextView itself. Thanks again. :)

EDIT: There is one final problem. It turns out that when I am zoomed in such that the text goes off the edge of the screen, I can only scroll vertically and not horizontally. I have set the view to scroll horizontally in Interface Builder but apparently the view doesn't recognize that the content's size has changed.

Hi, I am doing the same thing as you before. I have a UITextView and I would like to make it can scroll vertical and user can zoom in or out by multiple touch. I have try many method, but not good. Can you please help and let me know how you can do it well ? Thanks !
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.