切换视图时触发事件

发布于 2024-12-20 02:09:23 字数 645 浏览 2 评论 0原文

这很容易。

假设我有两个视图:firstView 和 SecondView。

firstView 是根视图。

我从第一个视图加载第二个视图:

secondView *secondViewController;
secondViewController = [[SecondView alloc] 
                      initWithNibName:@"SecondView" bundle:nil];
[self.view addSubview:SecondViewController.view];

我在第二个视图中添加了一个“后退”按钮。

当我单击该按钮时,我返回到firstView:

[self.view removeFromSuperView];

这是问题:

当firstView - >时secondaryView,触发了secondView中的viewDidLoad

当我在 SecondView 中使用 removeFromSuperView 返回时,如何触发事件来通知firstView?

It is easy.

Suppose I have two views: firstView and SecondView.

firstView is the ROOT view.

I load secondView from firstView:

secondView *secondViewController;
secondViewController = [[SecondView alloc] 
                      initWithNibName:@"SecondView" bundle:nil];
[self.view addSubview:SecondViewController.view];

I added a "Back" button in secondView.

When I click that button I go back to firstView:

[self.view removeFromSuperView];

Here is the question:

When firstView -> secondView, viewDidLoad in secondView is triggered.

How can I trigger an event to inform firstView when I go back using removeFromSuperView in secondView?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的心境我的脸 2024-12-27 02:09:24

你到底想做什么?也许您只想从第一个视图控制器转到第二个视图控制器?然后回到你的第一个视图控制器?在这种情况下,如果您的第一个控制器已经嵌入导航控制器中,只需这样做:

[self.navigationController pushViewController:secondViewController animated:YES];

但是如果您只想从 Interface Builder 创建一个视图,然后将其添加到您的第一个视图之上,您可以使用通知中心发布当您从第二个视图中单击“后退”按钮时发生的事件:

- (void) backButtonClicked:(id)sender {
    [self.view removeFromSuperView];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"back"
                                                        object:self.view];
}

然后您可以在第一个视图控制器中添加此事件的观察者,如下所示:

- (void) pushSecondViewController {
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(secondViewDidGoBack:) 
                                                 name:@"back"
                                               object:secondViewController.view];

    SecondView *secondViewController = [[SecondView alloc] initWithNibName:@"SecondView"
                                                                    bundle:nil];
    [self.view addSubview:secondViewController.view];
}

- (void) secondViewDidGoBack:(NSNotification *)notification {
    [[NSNotificationCenter defaultCenter] removeObserver:self];

    NSLog(@"My Second View Did Go back !");
}

我希望这会对您有所帮助!

What do you want to do exactly ? maybe you just want to go from your first view controller to your second view controller ? and then go back to your first view controller ? in this case just do like this, if your first controller is already embbeded in a navigation controller:

[self.navigationController pushViewController:secondViewController animated:YES];

But if you just want to create a view from Interface Builder and then add it above your first view, you can use the notification center to post an event when you click on your Back button from your second view:

- (void) backButtonClicked:(id)sender {
    [self.view removeFromSuperView];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"back"
                                                        object:self.view];
}

You can then add an observer for this event in your first view controller, like this:

- (void) pushSecondViewController {
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(secondViewDidGoBack:) 
                                                 name:@"back"
                                               object:secondViewController.view];

    SecondView *secondViewController = [[SecondView alloc] initWithNibName:@"SecondView"
                                                                    bundle:nil];
    [self.view addSubview:secondViewController.view];
}

- (void) secondViewDidGoBack:(NSNotification *)notification {
    [[NSNotificationCenter defaultCenter] removeObserver:self];

    NSLog(@"My Second View Did Go back !");
}

I hope this will help you !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文