我可以在viewDidLoad..中调用presentModalViewController吗?

发布于 2024-09-08 21:39:04 字数 1345 浏览 10 评论 0原文

在我的 iPhone 应用程序中,我有 HomeViewController 和 ContentViewController。我使用 NSUserDefaults 保存 ContentViewController 中的值 并根据保存的值,当应用程序重新启动时,我将加载 ContentView 而不是 HomeView。如果 NSUserDefaults 中没有值,则显示 HomeView。

在 HomeView 中我有一些按钮..就像这样..每个按钮都用于一本书,因此在 contentView 中,如果我单击某个页面,则将显示所有页面编号(在 ContentView 的滚动视图的底部),不会显示ContentView 上面标签中的文本。如果用户在 contentView 中关闭应用程序,则页面号和书号将被保存...如果用户单击主页按钮,所有信息将被删除。 在主页视图中,我检查 NSUserDefaults,如果它包含值,它应该显示该书的确切页面 以下是代码...

//HomeViewController.m

- (void)viewDidLoad {

    [super viewDidLoad];

    contentViewController = [[ContentViewController alloc] initWithNibName:@"ContentView" bundle:nil];

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

    NSLog(@"...%d,%d,%d",[prefs integerForKey:@"Stage"],[prefs integerForKey:@"Stimulus"],[prefs integerForKey:@"Card"]);

    if(!([prefs integerForKey:@"Stage"] ==0 && [prefs integerForKey:@"Stimulus"] ==0 && [prefs integerForKey:@"Card"] ==0)){


        [contentViewController setCurrentState:[prefs integerForKey:@"Stage"]]; 

        [contentViewController setCurrentStimulus:[prefs integerForKey:@"Stimulus"]];

        [contentViewController setCurrentCard:[prefs integerForKey:@"Card"]];

        [self presentModalViewController:contentViewController animated:YES];
    }
}

但它显示的是主页视图。

In my iPhone app I have HomeViewController and ContentViewController. I am saving the values in ContentViewController by using NSUserDefaults
and based on saved values I will load the ContentView instead of HomeView when the app is restarted. if there r no values in the NSUserDefaults it displays the HomeView.

in HomeView I have some buttons..it's like this.. each button is for a book so in contentView all the page nos (in the bottom in a scroll view in ContentView) will be displayed if I click on a page no it displays the text in the above label of ContentView.if the user closes the app in contentView, the page no and book no will be saved...if the user clicks on home button all the information will be deleted.
In the Homeview im checking the NSUserDefaults, if it contains values it should display that exact page of that book
the following is the code...

//HomeViewController.m

- (void)viewDidLoad {

    [super viewDidLoad];

    contentViewController = [[ContentViewController alloc] initWithNibName:@"ContentView" bundle:nil];

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

    NSLog(@"...%d,%d,%d",[prefs integerForKey:@"Stage"],[prefs integerForKey:@"Stimulus"],[prefs integerForKey:@"Card"]);

    if(!([prefs integerForKey:@"Stage"] ==0 && [prefs integerForKey:@"Stimulus"] ==0 && [prefs integerForKey:@"Card"] ==0)){


        [contentViewController setCurrentState:[prefs integerForKey:@"Stage"]]; 

        [contentViewController setCurrentStimulus:[prefs integerForKey:@"Stimulus"]];

        [contentViewController setCurrentCard:[prefs integerForKey:@"Card"]];

        [self presentModalViewController:contentViewController animated:YES];
    }
}

but it's displaying the homeview.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

演多会厌 2024-09-15 21:39:04

尝试使用下面所示的方法viewDidAppear而不是viewDidLoad

- (void)viewDidAppear:(BOOL)animated 
{
  contentViewController = [[ContentViewController alloc] initWithNibName:@"ContentView" bundle:nil];

  NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

  NSLog(@"...%d,%d,%d",[prefs integerForKey:@"Stage"],[prefs integerForKey:@"Stimulus"],     [prefs integerForKey:@"Card"]);

if(!([prefs integerForKey:@"Stage"] ==0 && [prefs integerForKey:@"Stimulus"] ==0 &&     [prefs integerForKey:@"Card"] ==0))
  {
   [contentViewController setCurrentState:[prefs integerForKey:@"Stage"]];
   [contentViewController setCurrentStimulus:[prefs integerForKey:@"Stimulus"]];
   [contentViewController setCurrentCard:[prefs integerForKey:@"Card"]];
   [self presentModalViewController:contentViewController animated:YES];
  }
}

Try using the method viewDidAppear shown below instead of viewDidLoad

- (void)viewDidAppear:(BOOL)animated 
{
  contentViewController = [[ContentViewController alloc] initWithNibName:@"ContentView" bundle:nil];

  NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

  NSLog(@"...%d,%d,%d",[prefs integerForKey:@"Stage"],[prefs integerForKey:@"Stimulus"],     [prefs integerForKey:@"Card"]);

if(!([prefs integerForKey:@"Stage"] ==0 && [prefs integerForKey:@"Stimulus"] ==0 &&     [prefs integerForKey:@"Card"] ==0))
  {
   [contentViewController setCurrentState:[prefs integerForKey:@"Stage"]];
   [contentViewController setCurrentStimulus:[prefs integerForKey:@"Stimulus"]];
   [contentViewController setCurrentCard:[prefs integerForKey:@"Card"]];
   [self presentModalViewController:contentViewController animated:YES];
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文