8

@"willAnimateRotationToInterfaceOrientation"パラメーターtoInterfaceOrientationduration(質問 #1 ) を含む通知をUIViewControllerアプリケーション (質問 #2 ) の全員に送信する必要があります。そのためのコードをどのように書くのですか?

[[NSNotificationCenter defaultCenter]
  addObserver:self
     selector:@selector(willAnimateRotationToInterfaceOrientation:toInterfaceOrientation:duration)
         name:@"willAnimateRotationToInterfaceOrientation"
       object:nil];

[[NSNotificationCenter defaultCenter] 
  postNotificationName:@"willAnimateRotationToInterfaceOrientation"
                object:self];
4

3 に答える 3

20

ディクショナリpostNotificationName:object:userInfo:内で渡したいパラメータを使用してバンドルします。userInfo

例:

このような通知を投稿できます

NSDictionary * userInfo = @{ @"toOrientation" : @(toOrientation) };
[[NSNotificationCenter defaultCenter] postNotificationName:@"willAnimateRotationToInterfaceOrientation" object:nil userInfo:userInfo];

次に、次のようにして、渡した情報を取得します。

- (void)willAnimateRotationToInterfaceOrientation:(NSNotification *)n {
    UIInterfaceOrientation toOrientation = (UIInterfaceOrientation)[n.userInfo[@"toOrientation"] intValue];
  //..
}

上記の内容を要約すると、通知を処理するために使用されるセレクターは、タイプのオプションのパラメーターを 1 つ取り、ディクショナリNSNotification内に渡したい情報を格納できます。userInfo

于 2013-04-11T20:00:21.043 に答える
1

これはあなたが思うようには機能しません。NSNotification通知メッセージの呼び出しには、オブジェクトであるオプションのパラメーターが 1 つあります。

-(void)myNotificationSelector:(NSNotification*)note;
-(void)myNotificationSelector;

通知オブジェクトには、userInfo関連情報を渡すために使用できるディクショナリであるプロパティがあります。ただし、通知センターによって呼び出される任意のセレクターを登録することはできません。;-postNotificationName:object:userInfo:の代わりに使用して、通知でその辞書を渡します。パラメータは、-postNotificationName:object:作成した です。userInfoNSDictionary

于 2013-04-11T20:01:30.690 に答える
0

メソッドを簡単に呼び出すことができます。これにより、引数が少なくなり、複雑な呼び出しが行われます。

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

- (void)doStuff {
  [self willAnimateRotationToInterfaceOrientation:someOrientation
                                    toOrientation:someOtherOrientation
                                         duration:1];
}

ただし、自分自身を呼び出してはいけませんwillAnimateRotationToInterfaceOrientation:。代わりに、ローテーションやその他のときにアクティブ化するコードを含むメソッドから呼び出されるメソッドを作成します。

于 2013-04-11T20:01:29.830 に答える