我有一个MainViewController,它将CustomModalViewController添加为子视图。
当调用委托函数customModalClickedButtonAtIndex
时,MainViewController
导航应该推送NextView
。
CustomModal正确removeFromSuperview
,但是因为NextView
没有被推送而出现问题。
MainViewController.m
...
@implementation MainViewController
- (IBAction)btnShowCustomModal:(id)sender
{
// Add the CustomModal as a SubView
CustomModalViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"CustomModal"];
viewController.delegate = self;
[self.navigationController addChildViewController:viewController];
[viewController didMoveToParentViewController:self];
[self.navigationController.view addSubview:viewController.view];
}
#pragma mark - CustomModalViewControllerDelegate
- (void)customModalClickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
// Left Button
}
else if (buttonIndex == 1) {
// Right Button
}
// Try to push the NextView. It's not working properly.
UIViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"NextView"];
[self.navigationController pushViewController:viewController animated:YES];
}
...
@end
CustomModalViewController.h
#import <UIKit/UIKit.h>
@protocol CustomModalViewControllerDelegate <NSObject>
- (void)customModalClickedButtonAtIndex:(NSInteger)buttonIndex;
@end
@interface CustomModalViewController : UIViewController
@property (weak, nonatomic) id <CustomModalViewControllerDelegate> delegate;
@end
CustomModalViewController.m
#import "CustomModalViewController.h"
@interface CustomModalViewController ()
@end
@implementation CustomModalViewController
- (IBAction)btnLeft:(id)sender
{
// Close the Modal and return the delegate method.
[self.view removeFromSuperview];
[self.delegate customModalClickedButtonAtIndex:0];
}
- (IBAction)btnRight:(id)sender
{
// Close the Modal and return the delegate method.
[self.view removeFromSuperview];
[self.delegate customModalClickedButtonAtIndex:1];
}
@end
答案 0 :(得分:0)
RemoveFromSuperview将清理视图,之后无法保证执行。所以代码永远不会被调用。
你会想要做两件事之一。 1.让调用视图句柄关闭它,因为它是添加它的那个,所以这样做也不错。 2.首先调用您的代码,然后调整顺序以确保它仍然有效。