【问题标题】:dismissModalViewControllerAnimated from another view controller class从另一个视图控制器类dismissModalViewControllerAnimated
【发布时间】:2012-05-29 23:50:00
【问题描述】:

我确实有两个 ViewController 类,一个 firstviewController 另一个 secondViewController 在第一个 viewcontroller 我称之为[self dimissModalViewControllerAnimation:NO]; 关闭视图!现在我需要从另一个 secondViewController 类中删除相同的视图。

所以我需要调用 super 吗?

[super dismissModalViewControllerAnimated:NO];

或者我是否需要创建任何协议来关闭视图!从另一个 secondViewController 类。

谁能指导我解决这个问题。

【问题讨论】:

  • 您的第二个视图控制器类是否继承自第一个?还是它们都是独立的 UIViewController 子类?
  • 我认为协议足以满足您的要求..
  • @Nate 两者都是独立的 UIViewController

标签: iphone


【解决方案1】:

你可以在 firstViewController 的 viewDidLoad 中注册一个通知

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

在 firstViewController 中添加事件处理程序

- (void)handleNotification:(NSNotification*)note {
    [self dismissModalViewControllerAnimated:NO];
}

然后就可以在secondViewController中触发事件了

[[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" object:nil ];

【讨论】:

  • !它非常适合使用通知
【解决方案2】:

你应该只在重载方法定义时使用super,例如:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = @"Login / Signup";
}

通常,如果您试图告诉一个视图从另一个视图做某事,委托是您的朋友。您可以创建一个弱 delegate 变量来保存对要关闭的视图控制器的引用,然后调用 [delegate dismissModalViewControllerAnimated:NO];

【讨论】:

  • 我假设你的意思是说你会持有一个对 view controller 的引用,因为UIViewControllerdismissModalViewControllerAnimated:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-17
  • 2011-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多