Valor de retorno de otro delegado de clase

I created a class (UIView) which has a UIScrollview inside. Through the delegate "scrollViewDidEndDecelerating" I can have 3 results (1,2,3). How do I send this result to the main ViewController?

ViewController header

#import "Picker.h"

ViewController implementation

picker = [[Picker alloc]initWithFrame:CGRectMake(10, 10, 300, 300)];
[self.view addSubview:picker];

Picker implementation

(...)

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    if (scrollView.contentOffset.x <= 100) {

        int result = 1;

    } else if (scrollView.contentOffset.x > 100 && scrollView.contentOffset.x <= 200) {

        int result = 2;

    } else {

        int result = 3;

    }
}

(...)

I had to create a UIView class because it has other things in addition to Scrollview.

¡Gracias!

preguntado el 30 de junio de 12 a las 17:06

3 Respuestas

You can define a property within your Picker clase:

@property (nonatomic, assign) MyViewController* controller;

and initalize Picker's instances like this:

picker = [[Picker alloc]initWithFrame:CGRectMake(10, 10, 300, 300) andController:self];

entonces podrías hacer:

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    [self.controller myMethod]; 
    ...

This is known as dependency injection and it is ok if it makes sense create such strong coupling among the two classes. You could create a delegate protocol to give such interaction a fuller status.

Another option you have, which allows you to avoid the coupling, is using notifications through NSNotificationCenter.

In this case, one object (your controller) would register for observing a PickerScrollViewChanged notificación:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMethod:) name:kPickerScrollViewChanged object:nil];

a picker would send such notification from its scrollViewDidEndDecelerating método:

[[NSNotificationCenter defaultCenter] postNotificationName:kPickerScrollViewChanged object:self];

Respondido el 30 de junio de 12 a las 18:06

thanks, but in this way the class can not be used in multiple classes. - Vinos

why not? you decide what signature has your method, if you need to pass in a reference to the picker, you are welcome... indeed, this is what I do with the notification example... - sergio

Te sugiero que crees un PickerDelegate protocol with a method to return the required value to the main view controller.

@protocol PickerDelegate <NSObject>

-(void)picker:(Picker *)picker didScrollToResult:(NSInteger)result;

@end

The Picker has a property:

@property (nonatomic, assign) id<PickerDelegate> delegate;

Then you can set your view controller as the picker delegate using picker.delegate = self;

In the Picker UIView's scrollview delegate method, you can pass the message to the view controller by using:

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x <= 100) {
        int result = 1;
    } else if (scrollView.contentOffset.x > 100 && scrollView.contentOffset.x <= 200) {
        int result = 2;
    } else {
        int result = 3;
    }
    [self.delegate picker:self didScrollToResult:result];
}

The view controller needs to implement the picker delegate method:

-(void)picker:(Picker *)picker didScrollToResult:(NSInteger)result {
    // View controller now has result variable to action upon.
}

Respondido el 30 de junio de 12 a las 18:06

In your view controller class, you should add observer(register for notification) into viewDidLoad method. Inside scrollViewDidEndDecelerating you should post a notification.

So, in viewDidLoad:

[[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(scrollViewZoomed:)
 name:@"scrollViewDidZoom"
 object:nil];

Inside your scrollViewDidEndDecelerating,

[[NSNotificationCenter defaultCenter]
 postNotificationName:@"scrollViewDidZoom"
 object:self];

Do not forget to remove observer when its not required.

Respondido el 30 de junio de 12 a las 18:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.