presentModalViewController可以在启动时工作吗?

Ban*_*ong 5 iphone objective-c

我想在启动时使用模态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];
}
Run Code Online (Sandbox Code Playgroud)

但是,稍后通过按钮调用时,相同的代码可以正常工作:

- (IBAction)settingsPressed:(id)sender{
    [self presentModalViewController:navigationController animated:YES];
}
Run Code Online (Sandbox Code Playgroud)

相关问题:当​​UITableView使用命令退出时,我如何感知(在上层)?

[self.parentViewController dismissModalViewControllerAnimated:YES];
Run Code Online (Sandbox Code Playgroud)

Tim*_*Tim 7

您可以将presentModalViewController:animated:调用放在代码的其他位置- 它应该在viewWillAppear视图控制器的方法中工作,或者在applicationDidFinishLaunchingapp delegate 中的方法中工作(这是我放置启动模式控制器的位置).

至于知道视图控制器何时消失,您可以在父视图控制器上定义一个方法,并覆盖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];
}
Run Code Online (Sandbox Code Playgroud)


小智 6

我有同样的问题.我知道这个话题很老但也许我的解决方案可以帮助别人......你只需要在一个方法中移动你的模态定义:

// ModalViewController initialization
- (void) presentStartUpModal 
{
    ModalStartupViewController *startUpModal = [[ModalStartupViewController alloc] initWithNibName:@"StartUpModalView" bundle:nil];
    startUpModal.delegate = self;

    [self presentModalViewController:startUpModal animated:YES];
    [startUpModal release]; 
}
Run Code Online (Sandbox Code Playgroud)

接下来,在with 中将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];
}
Run Code Online (Sandbox Code Playgroud)

我仍然不明白为什么"标准"方式不起作用.