obj*_*ive 56 iphone xcode objective-c modalviewcontroller ios
我几乎尝试了一切.这是我尝试过的:
-(void)viewWillAppear:(BOOL)animated
{
NSLog(@"Test");
}
-(void)viewDidAppear:(BOOL)animated
{
NSLog(@"Test");
}
-(void)viewDidLoad
{
NSLog(@"Test");
}
Run Code Online (Sandbox Code Playgroud)
当模态视图控制器被解除时,为什么这些都不能在我的父视图控制器中工作?我怎样才能让它发挥作用?
Kir*_*sar 122
这个答案被重写/扩展,以解释3个最重要的方法(@galambalazs)
最简单的方法是使用回调block.如果您只有一个对解雇感兴趣的侦听器(父视图控制器),那么这很好.您甚至可以通过该事件传递一些数据.
在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];
Run Code Online (Sandbox Code Playgroud)
在SecondViewController.h中
@interface MainViewController : UIViewController
@property (nonatomic, copy) void (^didDismiss)(NSString *data);
// ... other properties
@end
Run Code Online (Sandbox Code Playgroud)
在SecondViewController.m中
- (IBAction)close:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
if (self.didDismiss)
self.didDismiss(@"some extra data");
}
Run Code Online (Sandbox Code Playgroud)
Delegation 是Apple推荐的模式:
解雇呈现的视图控制器
如果呈现的视图控制器必须将数据返回到呈现视图控制器,则使用委托设计模式来促进传输.委派可以更轻松地在应用程序的不同部分重用视图控制器.通过委托,呈现的视图控制器存储对委托对象的引用,该委托对象实现来自正式协议的方法.在收集结果时,呈现的视图控制器会在其委托上调用这些方法.在典型的实现中,呈现视图控制器使其自身成为其呈现的视图控制器的委托.
MainViewController
在MainViewController.h中
@interface MainViewController : UIViewController <SecondViewControllerDelegate>
- (void)didDismissViewController:(UIViewController*)vc;
// ... properties
@end
Run Code Online (Sandbox Code Playgroud)
在MainViewController.m中的某个地方(呈现)
SecondViewController* svc = [[SecondViewController alloc] init];
svc.delegate = self;
[self presentViewController:svc animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)
MainViewController.m中的其他地方(被告知解雇)
- (void)didDismissViewController:(UIViewController*)vc
{
// this method gets called in MainVC when your SecondVC is dismissed
NSLog(@"Dismissed SecondViewController");
}
Run Code Online (Sandbox Code Playgroud)
SecondViewController
在SecondViewController.h中
@protocol SecondViewControllerDelegate <NSObject>
- (void)didDismissViewController:(UIViewController*)vc;
@end
@interface SecondViewController : UIViewController
@property (nonatomic, weak) id<SecondViewControllerDelegate> delegate;
// ... other properties
@end
Run Code Online (Sandbox Code Playgroud)
在SecondViewController.m中的某个地方
[self.delegate didDismissViewController:self];
[self dismissViewControllerAnimated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)
(注意:带有didDismissViewController:方法的协议可以在整个应用程序中重用)
另一种解决方案是发送NSNotification.这也是一种有效的方法,它可能比委托更容易,以防您只想在不传递大量数据的情况下通知解雇.但它的主要用例是当您想要解雇事件的多个侦听器时(除了父视图控制器之外).
但是一定要在完成后务必将自己从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");
}
Run Code Online (Sandbox Code Playgroud)
在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];
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
Pau*_*l.s 10
模态视图应该告诉其父级将其解雇,然后父级将知道,因为它负责解雇.
如果您创建一个新项目并选择Utility Application模板,则可以看到此示例.
| 归档时间: |
|
| 查看次数: |
59854 次 |
| 最近记录: |