我想在启动时使用模态UITableView来询问用户密码等,如果它们尚未配置。但是,调用uitableview的命令似乎在viewDidLoad中不起作用。
启动代码:
- (void)viewDidLoad {
rootViewController = [[SettingsController alloc]
initWithStyle:UITableViewStyleGrouped];
navigationController = [[UINavigationController alloc]
initWithRootViewController:rootViewController];
// place where code doesn't work
//[self presentModalViewController:navigationController animated:YES];
}
但是,稍后通过按钮调用时,相同的代码可以正常工作:
- (IBAction)settingsPressed:(id)sender{
[self presentModalViewController:navigationController animated:YES];
}
相关问题:当UITableView使用该命令退出时,我如何感知(在上层)?
[self.parentViewController dismissModalViewControllerAnimated:YES];
答案 0 :(得分:7)
您可以将presentModalViewController:animated:
调用放在代码的其他位置 - 它应该在视图控制器的viewWillAppear
方法中工作,或者在应用委托中的applicationDidFinishLaunching
方法中工作(这是我放置了我的发射模态控制器。)
至于知道视图控制器何时消失,您可以在父视图控制器上定义一个方法,并覆盖子控制器上的dismissModalViewControllerAnimated
的实现以调用该方法。像这样:
// Parent view controller, of class ParentController
- (void)modalViewControllerWasDismissed {
NSLog(@"dismissed!");
}
// Modal (child) view controller
- (void)dismissModalViewControllerAnimated:(BOOL)animated {
ParentController *parent = (ParentController *)(self.parentViewController);
[parent modalViewControllerWasDismissed];
[super dismissModalViewControllerAnimated:animated];
}
答案 1 :(得分:6)
我遇到了同样的问题。我知道这个话题很老但也许我的解决方案可以帮助其他人...... 您只需要在方法中移动模态定义:
// ModalViewController initialization
- (void) presentStartUpModal
{
ModalStartupViewController *startUpModal = [[ModalStartupViewController alloc] initWithNibName:@"StartUpModalView" bundle:nil];
startUpModal.delegate = self;
[self presentModalViewController:startUpModal animated:YES];
[startUpModal release];
}
接下来,在viewDidLoad
中,使用performSelector:withObject:afterDelay:
作为延迟值,在0
中调用您的模态定义方法。像这样:
- (void)viewDidLoad
{
[super viewDidLoad];
//[self presentStartUpModal]; // <== This line don't seems to work but the next one is fine.
[self performSelector:@selector(presentStartUpModal) withObject:nil afterDelay:0.0];
}
我仍然不明白为什么“标准”方式不起作用。
答案 2 :(得分:0)
如果您打算这样做,那么您将必须声明自己的协议,以便能够判断UITableView何时解除了parentViewController,因此您声明了一个具有类似
的方法的协议-(void)MyTableViewDidDismiss
然后在您的父类中,您可以实现此协议,在tableView中解除ModalView后,您可以在委托上调用MyTableViewDidDismiss(这是父视图控制器)。