PresentViewController iOS 上的 PushView

Posted

技术标签:

【中文标题】PresentViewController iOS 上的 PushView【英文标题】:PushView on PresentViewController iOS 【发布时间】:2013-10-30 06:42:50 【问题描述】:

我想在PresentViewControllerPush我的viewController

当我最初点击按钮时,我正在加载PresentViewController,这是我的代码。

- (IBAction)JoinClicked:(id)sender

    JoinWithViewController *detail_view_controller = [[JoinWithViewController alloc] initWithNibName:@"JoinWithViewController" bundle:nil];
    [self.view.window.rootViewController presentViewController:detail_view_controller
                                                      animated:YES
                                                    completion:NULL];


这很好用,但是当我点击 PresentViewController 上的按钮时,我想推送我的视图,但不推送。

请帮忙,提前致谢。

【问题讨论】:

:appDelegate中如何初始化rootViewController请放上你的代码。 appDelegate 中没有任何内容我正在使用 stroyboard 你也可以使用 CATransition 来使用动画和持续时间来填充你的视图。为此使用 框架。 你知道这方面的任何例子吗? 如果即使你使用 storyBoard 你也需要做 didFinishLaunchingWithOptions: self.viewController = [[viewController alloc] initWithNibName:@"viewController" bundle:nil]; UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:self.viewController]; self.window.rootViewController = 导航;推送视图控制器 【参考方案1】:
JoinWithViewController *detail_view_controller = [[JoinWithViewController alloc] initWithNibName:@"JoinWithViewController" bundle:nil];
[self.navigationController pushViewController:detail_view_controller animated:YES];

尝试像这样推送 viewController。如果您使用 TabBar,请在 AppDelegate 中执行此操作。如果您创建 TabBar 拖放意味着离开。以编程方式创建 TabBar,如下所示。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    UITabBarController *tabController = [[UITabBarController alloc]init];

    self.viewController = [[CalculatorViewController alloc] initWithNibName:@"CalculatorViewController" bundle:nil];
    UITabBarItem *tab_item1 = [[UITabBarItem alloc]init];
    //tab_item1.title = @"Calculator";
    tab_item1.image = [UIImage imageNamed:@"Calculator.png"];
    [self.viewController setTabBarItem:tab_item1];
    UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:self.viewController];

    ShowPhoneViewController *phone = [[ShowPhoneViewController alloc]init];
    UITabBarItem *tab_item2 = [[UITabBarItem alloc]init];
    tab_item2.title = @"Latest Mobiles";
    tab_item2.image = [UIImage imageNamed:@"Mobile.png"];
    [phone setTabBarItem:tab_item2];
    UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:phone];

    CurrencyConvertorViewController *currency = [[CurrencyConvertorViewController alloc]init];
    UITabBarItem *tab_item3 = [[UITabBarItem alloc]init];
    tab_item3.title = @"Units Convertor";
    tab_item3.image = [UIImage imageNamed:@"Dollar.png"];
    [currency setTabBarItem:tab_item3];
    UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:currency];

    SettingsPageViewController *setting = [[SettingsPageViewController alloc]init];
    UITabBarItem *tab_item4 = [[UITabBarItem alloc]init];
    tab_item4.title = @"Settings";
    tab_item4.image = [UIImage imageNamed:@"Settings.png"];
    [setting setTabBarItem:tab_item4];
    UINavigationController *nav4 = [[UINavigationController alloc]initWithRootViewController:setting];


    tabController.viewControllers = [NSArray arrayWithObjects:nav1, nav2, nav3, nav4, nil];
    self.window.rootViewController = tabController;

    [self.window makeKeyAndVisible];
    return YES;

我希望你现在得到了

【讨论】:

@Krunal:你得到了想要的输出?【参考方案2】:

pushviewcontrollerUINavigationController 的功能,您可以在其中将一个视图控制器推送到另一个视图控制器上。在这里,您使用单个视图控制器作为根视图控制器,因此您必须将 rootviewcontroller 更改为 UINavigationcontroller 或者您可以使用“addSubview 方法”添加当前视图控制器上的新视图控制器。

但更好的选择是将 uinavigationcontroller 添加为 rootviewcontroller

AppDelegate.m

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

     FirstViewController *first = [[FirstViewController alloc]
                                 initWithNibName:@"FirstViewController" bundle:nil];
     UINavigationController *navController =[[UINavigationController alloc] 
                                            initWithRootViewController:first];
     [self.window setRootViewController:navController];   

现在,当您想在单击按钮时从 FirstViewController 切换到 SecondViewController 时,您必须使用 pushviewcontroller

FirstViewController.h

-(void) nextBtnPressed 
    SecondViewController *second = [[SecondViewController alloc]  initWithNibName:@"SecondViewController" bundle:nil];
     [self.navigationController pushViewController:second animated:TRUE];
 

【讨论】:

当我在didFinishLaunchingWithOptions 中编写您的代码时,我无法看到我的底部TabBar 1.在 Appdelegte.m 中,您可以将导航控制器添加为 rootviewcontroller(这里 FirstViewController 是 UINavigationController 的 rootViewController)。 2.(当你不使用 Storyboard 时)现在假设在按下下一个按钮时,你想从 FirstViewController 切换到 SecondViewController 而不是你必须调用 nextBtnPressed 方法。

以上是关于PresentViewController iOS 上的 PushView的主要内容,如果未能解决你的问题,请参考以下文章

PresentViewController 在 iOS 中隐藏导航栏

PresentViewController iOS 上的 PushView

如何使用 presentViewController 呈现 iOS 键盘扩展

PresentViewController 与 ModalPresentationStyle “UIModalPresentationFormSheet” IOS 8

iOS presentViewController 方法引起的问题

在 iOS 上,presentViewController 会导致溢出(如堆栈溢出)吗?