[英]Creating and transitioning views without using storyboard with swift
I am trying to create a new UIViewController
class in a new file and transition to that UIViewController
from the default ViewController
. 我试图在一个新文件中创建一个新的
UIViewController
类,并从默认的ViewController
过渡到该UIViewController
。 In Objective-C
I would create a new file along with it's corresponding xib file then import that file into my main ViewController.m
and use the view's presentViewController
method to transition to the new view. 在
Objective-C
我将创建一个新文件及其对应的xib文件,然后将该文件导入到我的主ViewController.m
并使用视图的presentViewController
方法过渡到新视图。
Example: from main ViewController.m
示例:从主
ViewController.m
SecondViewController* secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewControllerXib" bundle:nil];
[self presentViewController:secondView animated:YES completion:nil];
secondView = nil;
Then return to main ViewController.m
using 然后使用返回主
ViewController.m
[self dismissViewControllerAnimated:YES completion:nil];
I have search around but all the examples I've come across are all using Storyboard
which I prefer not to use. 我到处搜索,但是遇到的所有示例都使用
Storyboard
,但我不想使用。 How can I achieve this without the using Storyboard
in swift? 不迅速使用
Storyboard
怎么办?
There's nothing different in terms of using Storyboards and .XIB files or doing all UIKit in pure code. 在使用Storyboards和.XIB文件或以纯代码完成所有UIKit方面没有什么不同。 Swift and Obj-C have access to the same IDE tools in this regard so you might be looking for a problem that isn't there.
Swift和Obj-C在这方面可以使用相同的IDE工具,因此您可能正在寻找一个不存在的问题。
Seems like you're just asking how to write Swift syntax based on existing Obj-C code. 似乎您只是在问如何根据现有的Obj-C代码编写Swift语法。 "Programmatic View Controller Examples with Swift" ought to be googlable with a little more effort, but to your code:
应该稍微花些功夫才能使用“带有Swift的Programmatic View Controller示例”,但对于您的代码:
import UIKit
class SecondViewController: UIViewController {
}
class FirstViewController: UIViewController {
func foo() {
let secondView = SecondViewController(nibName: "SecondViewControllerXib", bundle: nil)
presentViewController(secondView, animated: true, completion: nil)
}
func bar() {
dismissViewControllerAnimated(true, completion: nil)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.