ios - 以编程方式更改 View Controller

标签 ios objective-c

我有一个 UIViewController,它有一个按钮,我在 StoryBoard 中创建了两个 View Controller ,并将其连接到 Cocoa 类。我想根据某些条件将 UIViewController 更改为两个 View Controller 之一。

我试过:

// 1
BOOL isLoggedIn=TRUE;
NSString *storyboardId = isLoggedIn ? @"FirstViewController" : @"SecondViewController";
self.view = [self.view.inputViewController.storyboard instantiateViewControllerWithIdentifier:storyboardId]; 

// 2
[self.view addSubview:FirstViewController];

感谢您的及时回复

最佳答案

Main.storyboard 中选择您的 subview Controller ,然后从右侧菜单中的身份检查器中将 StoryBoardID 更改为 someID

然后,将此代码添加到按钮操作方法中:

ViewController *NVC = [self.storyboard instantiateViewControllerWithIdentifier:@"someID"];
[self presentViewController:NVC animated:YES completion:nil];

关于ios - 以编程方式更改 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31630213/

相关文章:

iphone - 滚动时感觉滞后,同时从后台线程中的 Assets 加载全屏图像

objective-c - 在 NSTextView 中为行片段重绘单个矩形

ios - pbkdf2 objective c 比较哈希

ios - 忽略 #pragma 发出的警告

ios - NSURLConnection 用于多个 View 不异步接收数据

ios - AVcaptureMetadataOutput 在 ios10 中不起作用

ios - 如何将多重纹理用作一种纹理?

ios - Cocoapods - 安装 pod 并验证文件存在后为 'MBProgressHUD.h not found'

objective-c - 记录声音并将其保存到文件

ios - 运行IOS错误 "failed to read asset tags"xcode 10.13