2

我使用 presentModalViewController 来显示一些控制器。

A控制器://它将显示B的控制器并接收消息,当它收到消息时,我想知道B是否显示?

-(void)viewDidLoad{

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recvPushMessage:) name:RECV_PUSH_MESSAGE_NOTIFY object:nil];

}
-(void)buttonAction(id)sender{
     B* b = [B alloc] init];
     UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:(UIViewController*)b];
     [self presentModalViewController:navigationController animated:YES];
     //release 
}
-(void)recvPushMessage{
   //i want to kown B is it show or not
   if(b is showing){
      //do something
   }
   else{
      //do something for A
   }

}

B控制器://当B显示时,我不想在A中调用recvPushMessage。

 -(void)viewDidLoad{

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recvPushMessage:) name:RECV_PUSH_MESSAGE_NOTIFY object:nil];

}
-(void)recvPushMessage{

   //do something for B
}
4

1 回答 1

0

检查这是否是您正在寻找的,

-(void)recvPushMessage{

   if (self.presentedViewController) { //check if you have this model view controller
      //do something
   }
   else{
      //do something for A
   }
}

对于版本 < iOS 5.0,您可以使用self.modalViewController.

更新: 对于您的第二个问题,您可以获取topViewController导航控制器并将其与 C 进行比较。您可以使用类似的东西[self.modalViewController topViewController]来检查它是否是 C,然后继续。

例如:-

if ([[(UINavigationController *)self.modalViewController topViewController] isKindOfClass:[C class]])
于 2013-01-16T06:58:47.770 回答