如何在类中以编程方式加载故事板?

8 浏览
0 Comments

如何在类中以编程方式加载故事板?

我遇到的问题是我想找到一种同时使用storyboard和xib的方法。但是我找不到适当的方法来以编程方式加载和显示storyboard。项目一开始是用xib进行开发的,现在很难将所有的xib文件嵌套到storyboard中。所以我在寻找一种以代码方式实现的方法,就像使用alloc、init、push来处理viewControllers一样。在我的情况下,我在storyboard中只有一个控制器:UITableViewController,它有一些我想要显示的静态单元格内容。如果有人知道在不进行大规模重构的情况下同时使用xib和storyboard的正确方法,我将非常感激任何帮助。

0
0 Comments

从上述代码可以看出,这是一个通过代码来加载故事板的问题。原因可能是开发者需要在特定的情况下动态地加载故事板,而不是依赖于故事板之间的固定跳转关系。

解决方法是通过使用UIStoryboard类来实现。首先,需要创建一个UIStoryboard对象,并指定故事板的名称和bundle。然后,使用该故事板对象的instantiateViewControllerWithIdentifier方法来获取指定标识符的视图控制器对象。最后,通过导航控制器的pushViewController方法将该视图控制器推入导航堆栈中,并选择是否使用动画效果。

以下是代码示例:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
DetailViewController *detailViewController = [storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
[self.navigationController pushViewController:detailViewController animated:YES];

以上代码中,首先创建了一个名为"MainStoryboard"的UIStoryboard对象。然后,通过指定的标识符"DetailViewController"来获取对应的视图控制器对象。最后,使用导航控制器的pushViewController方法将该视图控制器推入导航堆栈中,并选择使用动画效果。

通过上述代码示例,开发者可以通过代码来加载故事板,并在需要的时候动态地跳转到指定的视图控制器。这样可以更加灵活地控制应用程序的导航流程。

0
0 Comments

如何从类中编程加载故事板?

在编程中,有时需要从类中加载故事板。下面是一些在不同版本的Swift中加载故事板的方法和示例代码。

在Swift 3中,可以按照以下方式加载故事板:

let storyboard = UIStoryboard(name: "Main", bundle: nil)

let vc = storyboard.instantiateViewController(withIdentifier: "viewController")

self.navigationController!.pushViewController(vc, animated: true)

在Swift 2中,可以按照以下方式加载故事板:

let storyboard = UIStoryboard(name: "Main", bundle: nil)

let vc = storyboard.instantiateViewControllerWithIdentifier("viewController")

self.navigationController!.pushViewController(vc, animated: true)

在加载故事板之前,需要为视图控制器指定一个故事板ID。可以通过以下步骤为视图控制器指定故事板ID:

1. 在Interface Builder中打开故事板

2. 选择视图控制器

3. 在Identity Inspector中的Storyboard ID字段中为视图控制器指定一个唯一的ID

在Swift(旧版)中,可以按照以下方式加载故事板:

let storyboard = UIStoryboard(name: "Main", bundle: nil)

let vc = storyboard.instantiateViewControllerWithIdentifier("viewController") as? UIViewController

self.navigationController!.pushViewController(vc!, animated: true)

如果想要在没有导航控制器的情况下使用加载故事板,可以按照以下方式进行:

let storyboard = UIStoryboard(name: "Main", bundle: nil)

let vc = storyboard.instantiateViewController(withIdentifier: "viewController")

present(vc, animated: true, completion: nil)

在Swift 2中,不需要添加"as? UIViewController",编译器会自动确定它的类型。另外,也可以使用self.storyboard来合并第1行和第2行代码。

通过上述方法,我们可以在类中编程加载故事板,并在应用程序中进行页面导航。

0
0 Comments

如何从类中以编程方式加载Storyboard?

在Storyboard中,转到属性检查器并设置视图控制器的标识符。然后,您可以使用以下代码呈现该视图控制器。

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"myViewController"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:NULL];

如果您想要启动场景的默认视图控制器,可以使用[sb instantiateInitialViewController]

对于James Beith的代码,如果您的视图控制器在当前视图控制器之间切换,您必须重用那个UIViewControler *vc。我通过艰难的方式发现,该vc会一直存在并与故事板nib连接,直到用户在新视图上按下按钮,因此在此代码的之前实例化的vc会导致内存泄漏。

如果有人想要在应用程序委托中执行此操作,可以按以下顺序将[self presentViewcontroller]逻辑替换为以下几行代码:1)self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 2)self.window.rootViewController = vc;和3)[self.window makeKeyAndVisible];。您还可以删除modalTransitionStyle行,因为这不是应用程序委托中的模态转换。

如果您想要“推送”新的故事板而不是模态弹出,请参考chaithraVeeresh的答案。

请记住,如果您想要设置任何参数,您需要将视图控制器强制转换为您的视图控制器类,就像您通常在prepareForSegue中做的那样。

如何调用该ViewController的Cocoa Touch类文件?

0