从 ModalViewController 中选择第二个选项卡

发布于 2025-01-08 07:47:33 字数 1477 浏览 1 评论 0原文

在我的应用程序委托中,我在选项卡栏顶部加载了一个视图控制器。该控制器上有三个按钮,一个用于导航到每个选项卡。当按下第二个按钮时,我想关闭视图控制器并转到第二个选项卡。但这似乎并不按正常方式工作。

我的 AppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{    
    //-- Insert a delay of 5 seconds before the splash screen disappears
    [NSThread sleepForTimeInterval:3.0];        

    // Set the tab bar controller as the window's root view controller and display.
    self.window.rootViewController = self.tabBarController;

    // Set StartView to load first
    StartViewController *startViewController = [[StartViewController alloc] initWithNibName:@"StartView" bundle: nil];
    [window addSubview: [startViewController view]];
    [window makeKeyAndVisible];

    [self.tabBarController presentModalViewController:startViewController animated:NO];
    [startViewController release];

    return YES;
}

这是我当前的 IBAction,它似乎不起作用:

 - (IBAction) toSecondView:(id)sender
    {
    // Show status bar
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];

    [(UITabBarController *)self.parentViewController setSelectedIndex:1];

    [self dismissModalViewControllerAnimated:NO];
}

我也尝试了这些,但没有成功:

self.tabBarController.selectedIndex = 1;  

任何人都

[self.tabBarController setSelectedIndex:1];

可以帮助我并解释我所缺少的内容吗?

In my app delegate I load a view controller on top of my tabbar. This controller had three buttons on it, one to navigate to each tab. When the second button is pressed, I want to dismiss the view controller and go to the second tab. But this doesn't seem to work the normal way.

My AppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{    
    //-- Insert a delay of 5 seconds before the splash screen disappears
    [NSThread sleepForTimeInterval:3.0];        

    // Set the tab bar controller as the window's root view controller and display.
    self.window.rootViewController = self.tabBarController;

    // Set StartView to load first
    StartViewController *startViewController = [[StartViewController alloc] initWithNibName:@"StartView" bundle: nil];
    [window addSubview: [startViewController view]];
    [window makeKeyAndVisible];

    [self.tabBarController presentModalViewController:startViewController animated:NO];
    [startViewController release];

    return YES;
}

And here is my current IBAction, which doesn't seem to work:

 - (IBAction) toSecondView:(id)sender
    {
    // Show status bar
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];

    [(UITabBarController *)self.parentViewController setSelectedIndex:1];

    [self dismissModalViewControllerAnimated:NO];
}

I tried these too, without success:

self.tabBarController.selectedIndex = 1;  

and

[self.tabBarController setSelectedIndex:1];

Can anyone help me out and explain me what I'm missing?

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

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

发布评论

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

评论(1

请你别敷衍 2025-01-15 07:47:33

发生这种情况有以下原因。

您已将 ViewController 作为子视图添加到窗口中,无需添加 SubView,因为您已经将该 ViewController 呈现为 ModalViewController。

请尝试如下。

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    
//-- Insert a delay of 5 seconds before the splash screen disappears
[NSThread sleepForTimeInterval:3.0];        

// Set the tab bar controller as the window's root view controller and display.
self.window.rootViewController = self.tabBarController;

// Set StartView to load first
StartViewController *startViewController = [[StartViewController alloc] initWithNibName:@"StartView" bundle: nil];
//[window addSubview: [startViewController view]]; no need to add subView here
[window makeKeyAndVisible];

[self.tabBarController presentModalViewController:startViewController animated:NO];
[startViewController release];
return YES;

}

-(IBAction) toSecondView:(id)sender
{
// Show status bar
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
//create delegate's class object for accessing tabBarController
 AppDelegate* delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
 //instead of [(UITabBarController *)self.parentViewController setSelectedIndex:1];
 //delegate.tabBarController your tabBarControler at which you have added viewController
[delegate.tabBarController setSelectedIndex:1];

[self dismissModalViewControllerAnimated:NO];

}

This happend because a below of reason.

You have added ViewController Onto the Window As a subView, no need to add SubView because you are already presenting that ViewController as ModalViewController.

Please Try as below.

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    
//-- Insert a delay of 5 seconds before the splash screen disappears
[NSThread sleepForTimeInterval:3.0];        

// Set the tab bar controller as the window's root view controller and display.
self.window.rootViewController = self.tabBarController;

// Set StartView to load first
StartViewController *startViewController = [[StartViewController alloc] initWithNibName:@"StartView" bundle: nil];
//[window addSubview: [startViewController view]]; no need to add subView here
[window makeKeyAndVisible];

[self.tabBarController presentModalViewController:startViewController animated:NO];
[startViewController release];
return YES;

}

-(IBAction) toSecondView:(id)sender
{
// Show status bar
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
//create delegate's class object for accessing tabBarController
 AppDelegate* delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
 //instead of [(UITabBarController *)self.parentViewController setSelectedIndex:1];
 //delegate.tabBarController your tabBarControler at which you have added viewController
[delegate.tabBarController setSelectedIndex:1];

[self dismissModalViewControllerAnimated:NO];

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文