【问题标题】:Xcode UIViewController to customUIViewController without IBXcode UIViewController 到 customUIViewController 没有 IB
【发布时间】:2014-07-04 13:31:51
【问题描述】:

我有一个 UIViewController,在该视图控制器中,我想在不使用 Interface Builder 的情况下调用 CustomUIViewController。我一直在使用它,它可以工作,但它需要一个故事板 ID。

UIStoryboard *storyboard = self.storyboard;
UIViewController *myVC = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:@"PageID"];
[self presentModalViewController:myVC animated:YES];

有没有一种方法可以在不使用 segue 和 IB 的情况下从 UIViewController 转到 customViewController?

【问题讨论】:

    标签: ios uiviewcontroller uistoryboard


    【解决方案1】:

    使用这个:

    UIViewController *myVc = [[UIViewController alloc] init];
    [self presentViewController:myVc animated:YES completion:nil];
    

    这显然会显示一个新的空白视图控制器,如果您想要 PageID 视图,那么您将 UIViewController 替换为 PageID 类的名称(即 .h/.m 文件名)

    所以我假设:

    PageID *myVc = [[PageID alloc] init];
    [self presentViewController:myVc animated:YES completion:nil];
    

    如果你想在导航控制器中使用它:

    PageID *myVc = [[PageID alloc] init];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myVc];
    [self presentViewController: navController animated:YES completion:nil];
    

    或者如果您不希望它是模态的(不需要创建新的导航控制器,因为它将被添加到现有的导航控制器堆栈中,即[self navigationController]

    [[self navigationController] pushViewController:myVc animated:YES];
    

    【讨论】:

    • 没问题。标记为答案,以便其他人知道他们是否想要相同的解决方案:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-20
    • 1970-01-01
    相关资源
    最近更新 更多