2

内部に UIScrollview を持つクラス (UIView) を作成しました。デリゲート「scrollViewDidEndDecelerating」を通じて、3 つの結果 (1,2,3) を得ることができます。この結果をメインの ViewController に送信するにはどうすればよいですか?

ViewController ヘッダー

#import "Picker.h"

ViewController の実装

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

ピッカーの実装

(...)

-(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;

    }
}

(...)

Scrollview 以外にも UIView クラスを作成する必要がありました。

ありがとうございました!

4

3 に答える 3

2

Pickerクラス内でプロパティを定義できます。

@property (nonatomic, assign) MyViewController* controller;

次のように Picker のインスタンスを初期化します。

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

次に、次のことができます。

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

    [self.controller myMethod]; 
    ...

これは依存性注入として知られており、2 つのクラス間でこのような強い結合を作成することが理にかなっていれば問題ありません。デリゲート プロトコルを作成して、そのようなやり取りをより充実した状態にすることができます。

カップリングを回避できる別のオプションは、 を介した通知を使用することですNSNotificationCenter

この場合、1 つのオブジェクト (コントローラー) がPickerScrollViewChanged通知を監視するために登録されます。

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

aは、そのメソッドpickerからそのような通知を送信します。scrollViewDidEndDecelerating

[[NSNotificationCenter defaultCenter] postNotificationName:kPickerScrollViewChanged object:self];
于 2012-06-30T17:03:50.307 に答える
1

PickerDelegate必要な値をメイン ビュー コントローラーに返すメソッドを含むプロトコルを作成することをお勧めします。

@protocol PickerDelegate <NSObject>

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

@end

ピッカーには次のプロパティがあります。

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

次に、View Controller をピッカー デリゲートとして設定できます。picker.delegate = self;

Picker UIView の scrollview デリゲート メソッドでは、次を使用してメッセージをビュー コントローラーに渡すことができます。

-(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];
}

ビュー コントローラーは、ピッカー デリゲート メソッドを実装する必要があります。

-(void)picker:(Picker *)picker didScrollToResult:(NSInteger)result {
    // View controller now has result variable to action upon.
}
于 2012-06-30T17:06:36.973 に答える
1

View Controller クラスでは、viewDidLoad メソッドにオブザーバー (通知の登録) を追加する必要があります。内部scrollViewDidEndDeceleratingに通知を投稿する必要があります。

したがって、viewDidLoad では次のようになります。

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

scrollViewDidEndDecelerating 内で、

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

オブザーバーが不要な場合は忘れずに削除してください。

于 2012-06-30T17:07:51.223 に答える