【问题标题】:How do I load another view controller from the current view controller's implementation file?如何从当前视图控制器的实现文件中加载另一个视图控制器?
【发布时间】:2013-03-06 00:48:55
【问题描述】:

我需要创建一个包含登录/注册表单和自定义 Google 地图的应用。我是 iOS 编程新手,我正在努力快速学习这个应用程序所需的东西。

所以,我已经创建了登录表单的前端和后端,它可以工作。我有一个由“登录”按钮触发的操作,该按钮验证凭据并触发错误或显示 Google 地图。

我想在另一个控制另一个 xib 文件的视图控制器中显示谷歌地图。我创建了视图控制器和 xib 文件。

我的问题是如何从放置在当前视图控制器的实现文件中的操作加载另一个视图控制器?

现在,我有这个代码:

UIViewController *mapViewController =
                [[BSTGMapViewController alloc] initWithNibName:@"BSTGMapViewController"
                                                           bundle:nil];

我怎样才能使它成为窗口的“根视图控制器”并可能具有过渡(考虑到我的逻辑是可以的)?

【问题讨论】:

    标签: iphone ios objective-c model-view-controller ios5


    【解决方案1】:

    如果你想从另一个打开ViewController,那么你应该在你的IBAction 中这样定义它。不过,将viewController 设为属性是个好主意。

    FirstViewController.h

    @class SecondViewController;
    
    @interface FirstViewController : UIViewController
    
    @property(strong,nonatomic)SecondViewController *secondViewController;
    @end
    

    FirstViewController.m

    -(IBAction)buttonClicked:(id)sender{
        self.secondViewController =
                        [[SecondViewController alloc] initWithNibName:@"SecondViewController"
                                                                   bundle:nil];
       [self presentViewController:self.secondViewController animated:YES completion:nil]; 
    
    }
    

    您应该在 AppDelegate 类中将 viewController 设置为 rootViewController,如下所示

    YourFirstViewController *firstViewController=[[YourFirstViewController alloc]initWithNibName:@"YourFirstViewController" bundle:nil];
    
    self.window.rootViewController=yourFirstViewController;
    

    【讨论】:

    • 我记得尝试过。让我测试一下,我会回来报告的。
    • 嗨@nsgulliver,如果我在 secondViewController 中有一个按钮,但我想在将 secondViewController 添加为子视图时隐藏 firstViewController 中的按钮。怎么办?
    猜你喜欢
    • 2017-09-17
    • 1970-01-01
    • 2019-07-03
    • 2020-02-01
    • 1970-01-01
    • 2015-12-02
    • 1970-01-01
    • 1970-01-01
    • 2016-11-20
    相关资源
    最近更新 更多