gpt4 book ai didi

iphone - 作为模态视图 Controller 调用底层 ViewController 中的函数被取消

转载 作者:太空狗 更新时间:2023-10-30 03:24:54 30 4
gpt4 key购买 nike

我有一个 mainViewController。我调用 [self pushModalViewController:someViewController] 使 someViewController 成为事件 View 。

现在我想在 mainViewController 中调用一个函数,因为 someViewController 随着 [self dismissModalViewController] 消失了。

viewDidAppear 没有被调用可能是因为 View 已经存在,就在模态视图下方。一旦 modalView 自行关闭,如何调用 mainViewController 中的函数?

非常感谢!

最佳答案

这个答案被重写/扩展以解释 3 种最重要的方法 (@galambalazs)

1。 block

最简单的方法是使用回调 block .如果您只有一个监听器(父 View Controller )对解雇感兴趣,这很好。您甚至可以随事件传递一些数据。

MainViewController.m

SecondViewController* svc = [[SecondViewController alloc] init];
svc.didDismiss = ^(NSString *data) {
// this method gets called in MainVC when your SecondVC is dismissed
NSLog(@"Dismissed SecondViewController");
};
[self presentViewController:svc animated:YES completion:nil];

SecondViewController.h

@interface MainViewController : UIViewController
@property (nonatomic, copy) void (^didDismiss)(NSString *data);
// ... other properties
@end

SecondViewController.m

- (IBAction)close:(id)sender 
{
[self dismissViewControllerAnimated:YES completion:nil];

if (self.didDismiss)
self.didDismiss(@"some extra data");
}

2。代表团

Delegation是 Apple 推荐的模式:

Dismissing a Presented View Controller

If the presented view controller must return data to the presenting view controller, use the delegation design pattern to facilitate the transfer. Delegation makes it easier to reuse view controllers in different parts of your app. With delegation, the presented view controller stores a reference to a delegate object that implements methods from a formal protocol. As it gathers results, the presented view controller calls those methods on its delegate. In a typical implementation, the presenting view controller makes itself the delegate of its presented view controller.

主视图 Controller

MainViewController.h

@interface MainViewController : UIViewController <SecondViewControllerDelegate>
- (void)didDismissViewController:(UIViewController*)vc;
// ... properties
@end

MainViewController.m 中的某处(呈现)

SecondViewController* svc = [[SecondViewController alloc] init];
svc.delegate = self;
[self presentViewController:svc animated:YES completion:nil];

MainViewController.m 中的其他地方(被告知解雇)

- (void)didDismissViewController:(UIViewController*)vc
{
// this method gets called in MainVC when your SecondVC is dismissed
NSLog(@"Dismissed SecondViewController");
}

第二 View Controller

SecondViewController.h

@protocol SecondViewControllerDelegate <NSObject>
- (void)didDismissViewController:(UIViewController*)vc;
@end

@interface SecondViewController : UIViewController
@property (nonatomic, weak) id<SecondViewControllerDelegate> delegate;
// ... other properties
@end

SecondViewController.m 中的某处

[self.delegate myActionFromViewController:self];
[self dismissViewControllerAnimated:YES completion:nil];

(注意:带有 didDismissViewController: 方法的协议(protocol)可以在您的整个应用中重复使用)


3。通知

另一个解决方案是发送 NSNotification .这也是一种有效的方法,如果您只想通知解雇而不传递太多数据,它可能比委派更容易。但它的主要用例是当您想要多个监听器 解散事件时(不仅仅是父 View Controller )。

但请确保在完成后始终将自己从 NSNotificationCentre 中删除!否则,即使在您被解除分配后,您也会因为被要求通知而有崩溃的风险。 [编者注]

MainViewController.m

- (IBAction)showSecondViewController:(id)sender 
{
SecondViewController *secondVC = [[SecondViewController alloc] init];
[self presentViewController:secondVC animated:YES completion:nil];

// Set self to listen for the message "SecondViewControllerDismissed"
// and run a method when this message is detected
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(didDismissSecondViewController)
name:@"SecondViewControllerDismissed"
object:nil];
}

- (void)dealloc
{
// simply unsubscribe from *all* notifications upon being deallocated
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didDismissSecondViewController
{
// this method gets called in MainVC when your SecondVC is dismissed
NSLog(@"Dismissed SecondViewController");
}

SecondViewController.m

- (IBAction)close:(id)sender 
{
[self dismissViewControllerAnimated:YES completion:nil];

// This sends a message through the NSNotificationCenter
// to any listeners for "SecondViewControllerDismissed"
[[NSNotificationCenter defaultCenter]
postNotificationName:@"SecondViewControllerDismissed"
object:nil userInfo:nil];
}

希望这对您有所帮助!

关于iphone - 作为模态视图 Controller 调用底层 ViewController 中的函数被取消,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4150677/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com