我有三个连接到选项卡栏 Controller 的 View Controller ,我认为它应该自动设置 presentingViewController/presentedViewController。但是,当我在 View Controller 之间切换并记录 [self presentingViewController] 时,它会记录 null。我将日志放在每个 View Controller 的 viewDidAppear 方法中。
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:YES];
NSLog(@" my presenting view controller is %@", [self presentingViewController]);
}
输出:
my presenting view controller is (null)
最佳答案
如果你想知道你来自哪个选项卡,那么你应该子类化选项卡栏 Controller ,并将其设置为自己的委托(delegate)。创建一个整数属性 oldIndex,您可以在委托(delegate)方法 tabBarController:shouldSelectViewController: 中的选项卡切换之前设置它。
@interface RDTBC () <UITabBarControllerDelegate>
@end
@implementation RDTBC
- (void)viewDidLoad {
[super viewDidLoad];
self.delegate = self;
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
self.oldIndex = self.selectedIndex;
return YES;
}
然后在你的 View Controller 中,你可以这样获取它,
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(@"%ld",[(RDTBC *)self.tabBarController oldIndex]);
}
关于ios - presentingViewController 显示为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26566018/