ios85
ios85

Reputation: 2134

Creating Delegate in UIScrollView Subclass

I have subclassed a UIScrollView to customize it a bit. I am trying to create a delegate that will notify several other classes that a user has done a certain thing in the UIScrollView. In my UIScrollView class I have the code below. The problem I am running into is I am getting the warning

Property 'delegate' 'retain (or strong)' attribute does not match the property inherited from 'UIScrollView'

I see that this is because my Class in inheriting from UIScrollView, but my delegate is conforming to the NSObject. This is the first time I tried creating my own delegate. What can I do to fix this?

My Code:

#import <UIKit/UIKit.h>

@protocol ChangeSpaceDelegate <NSObject>
- (void)changeSpace:(int)spaceId;

@end

@interface CustomUIScrollView : UIScrollView {
    id<ChangeSpaceDelegate> delegate;
}

@property (retain, nonatomic)id delegate;

@end

Upvotes: 1

Views: 2106

Answers (3)

petershine
petershine

Reputation: 3200

As mentioned by @Steve Madsen, I often add own delegate properties for subclasses. Like UITableView has separate DataSource and Delegate properties, and being assigned with the same object. In a long run, this will definitely pay off by not forcing you to repeat what have been already implemented in super class, and keeping your subclass implementations more manageable

Upvotes: 1

iDev
iDev

Reputation: 23278

You don't have to create the delegate object in custom scrollview class since you are subclassing it from UIScrollView. You can directly use it as self.delegate in your custom scrollview class.

Upvotes: 1

Steve Madsen
Steve Madsen

Reputation: 13791

To answer your question specifically, you are redefining the property attribute on the delegate property you get from UIScrollView. It should, like all delegates, be weak (or, pre-iOS 5, unsafe_unretained).

However, you shouldn't do it this way. UIScrollView already has a delegate, and if you expect to put your own delegate object implementing your new delegate methods into it, the inner workings of UIScrollView aren't going to be happy. Define a new protocol and a new delegate property for it.

@property (weak, nonatomic) id<ChangeSpaceDelegate> changeSpaceDelegate;

Upvotes: 2

Related Questions