【发布时间】:2015-04-10 01:30:59
【问题描述】:
我在实例化自定义视图控制器时确实遇到了麻烦。
This 是我设置的故事板。第三个视图控制器是我要展示的。
这两种方法我都试过了。
1:这会导致黑屏。
var searchController: SearchController = SearchController()
self.presentViewController(searchController, animated: true, completion: nil)
2:这会导致弹出一个白色的空视图控制器。
let mainStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let searchController : UIViewController = mainStoryboard.instantiateViewControllerWithIdentifier("searchController") as! UIViewController
self.presentViewController(searchController, animated: true, completion: nil)
这是实际视图控制器的代码:
class SearchController: UIViewController {
lazy var searchBar: UISearchBar = UISearchBar(frame: CGRectMake(0, 0, 200, 20))
override func viewDidLoad() {
super.viewDidLoad()
var leftItem = (UIBarButtonItem)(customView: searchBar)
self.title = "Search"
self.navigationItem.leftBarButtonItem = leftItem
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
这很令人困惑,因为我在使用上面的方法 #1 时正确呈现了另一个自定义视图控制器。为什么它不适用于这个控制器?
非常感谢大家。
【问题讨论】:
标签: ios swift uiviewcontroller storyboard