我的故事板中有3个项目.1. viewController(A)连接到2.导航控制器和3. viewController(B)没有连接任何东西所有3个项目都在故事板中设置了恢复标识符.
viewController(B)是初始视图控制器.我在B中尝试获取附加A的导航控制器,但总是返回nil:
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyBoard.instantiateViewControllerWithIdentifier("viewControllerA") as! ViewControllerA
print(vc.navigationController?) // always prints nil
Run Code Online (Sandbox Code Playgroud)
为什么!?
更新#1 我不能像视图控制器一样声明UINavigationController.我已经尝试将id为'myNavigationController'的navigationcontroller设置为storyboardID:
当故事板,我得到这个错误
let navigationController = storyBoard.instantiateViewControllerWithIdentifier("myNavigationController") as! UINavigationController
print(self.navigationController!) // fatal error: unexpectedly found nil while unwrapping an Optional value
Run Code Online (Sandbox Code Playgroud)
我也尝试在恢复标识符中设置id,它早先在实例化的行中被轰炸了
@ColdLogic,看看我在整个项目中搜索该标识符时得到的匹配:
在我的情节提要中,有导航控制器,viewContrl1和viewContrl2。对于下一次更新,我必须设法将viewContrl2隔离到viewContrl2的另一个实例。
从stackOverflow获得一些帮助后,我通过以下方式进行了管理:
let newView2 : ViewContrl2 = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("viewContrl2") as! ViewContrl2
...
...
view2.presentViewController(newView2, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
实际上,它确实满足我的需求,但顶部没有NavigationBar。在ViewContrl2类中,有@IBOutlet弱变量导航:UINavigationItem !,我直接从StoryBoard连接。但是,似乎导航在新创建的viewContrl2中不起作用。我需要这样做是因为用户必须能够按“返回”并返回上一个视图。任何人都可以解决这个问题吗?
谢谢
uinavigationcontroller uinavigationitem ios presentviewcontroller swift
我没有任何技术上的麻烦,但我想知道在界面生成器中是否有更好的方法可以这样做,因为考虑添加新的导航控制器看起来是重复的和不必要的。当我将另一个视图控制器(通过Show segue)添加到导航层次结构中,然后尝试添加栏按钮项时,直到将视图控制器嵌入到新的导航控制器中之前,我才能这样做。这就是我的意思:
无法添加条形按钮项:
能够添加条形按钮项:
interface-builder uinavigationbar uinavigationcontroller uibarbuttonitem swift
我是iOS开发的新手,我正在构建一个由UINavigationController和三个ViewControllers组成的应用程序,我将其称为A,B和C.
最初显示视图控制器A. 从那里,我有两个工作流程:
A> C.
A> B> C.
在第二个工作流程中,我在视图控制器B上定义了shouldPerformSegue(withIdentifier).在此方法的内部,我需要做一些工作来决定是否应该显示视图控制器C或取消segue并显示错误消息.假设没有发生错误,将执行查看控制器C的segue.当显示视图控制器C时,后退按钮指向观察控制器B,但我需要它指向后面以查看控制器A.
我已经尝试通过在返回true之前调用shouldPerformSegue(withIdentifier)末尾的以下内容从导航堆栈中删除视图控制器B以允许segue继续:
self.navigationController?.viewControllers.remove(at: index)
Run Code Online (Sandbox Code Playgroud)
如果我正确阅读文档,要删除的视图控制器的索引对应于以下内容:
The root view controller is at index 0
The back view controller is at index n-2
The top view controller is at index n-1
Run Code Online (Sandbox Code Playgroud)
不幸的是,我没有任何运气让它正常工作.我确定我做错了什么,但我不确定还有什么可以尝试的.谁能指出我正确的方向?谢谢!
尝试将“滑出菜单”连接到另一个ViewController时,Swift 3出现问题,但是prepareforsegue出现问题。
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
let DestVC = segue.destination as! ViewControllerSlide
Run Code Online (Sandbox Code Playgroud)
这是我遇到错误的地方。^^^
var indexPath : IndexPath = self.tableView.indexPathForSelectedRow!
DestVC.varView = indexPath.row
}
Run Code Online (Sandbox Code Playgroud) 我试图Timer在用户按下后退按钮时取消初始化/无效,但在他按下下一个ViewController时不尝试取消初始化/无效。
var timer = Timer()
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timePrinter), userInfo: nil, repeats: true)
timer.fire()
override func viewWillDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
if self.isMovingFromParentViewController{
timer.invalidate()
}
}
Run Code Online (Sandbox Code Playgroud)
当用户按下返回按钮时,它不起作用。
我使用导航控制器从一个视图推送到另一个视图,但没有弹出任何我推送的视图。因此,在加载视图时,我想从堆栈中弹出上一个视图。
推送视图的代码:
var identities = [String]()
identities = ["A", "B", "C", "D", "E"]
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vcName = identities[indexPath.row]
let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
self.navigationController?.pushViewController(viewController!, animated: true)
}
Run Code Online (Sandbox Code Playgroud)
用于弹出视图的测试代码(不是我想要的):
self.navigationController?.viewControllers.remove(at: 0)
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激。
解决了:
@DonMag帮助我逐步了解了该行代码的正确使用和放置。
memory-leaks uinavigationcontroller viewcontroller ios swift
我正在尝试做的是将所有以前的视图控制器从堆栈弹出到“菜单”控制器。我有一段代码应该执行此操作,但是单击“菜单”时,应用程序崩溃。
崩溃的原因:“试图弹出到不存在的视图控制器。”
这是我的代码的一部分:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// 0 = menu
if indexPath.row == 0 {
let vcName = identities[indexPath.row]
let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
let _ = navigationController?.popToViewController(viewController!, animated: true)
} else {
let vcName = identities[indexPath.row]
let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
self.navigationController?.pushViewController(viewController!, animated: true)
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个UITabBarController带有3个标签的SettingsVC,您可以在其中选择外观MainVC(ViewControllers根据用户的喜好,有2种不同的外观应显示为MainVC)。
我制作了该应用程序,并且可以正常工作,但是它确实有问题,因为我只是相互推挤ViewControllers,在某些情况下,切换时会看到错误tabs。
主板
这是我的代码的一部分(尝试了很多东西之后),该代码根据以下内容更改了VC UserDefaults
CheckSettingsStatus.swift
//
// Check for Double Column Settings and navigate to Double Column VC
//
func checkMainVCViewStatusAndNavigateAcordingly(){
if UserDefaults.standard.integer(forKey: KEY_MAIN_VC_VIEW) == 1{
//let appDel = UIApplication.shared.delegate as! AppDelegate
//appDel.goToDoubleMainVC()
let mainVC = storyboard?.instantiateViewController(withIdentifier: "DoubleColumnMainVC") as! DoubleColumnMainVC
self.navigationController?.viewControllers[0] = mainVC
//navigationController?.pushViewController(mainVC, animated: false)
var navigationArray = self.navigationController?.viewControllers
print("Number of VCs = \(String(describing: navigationArray?.count))")
if navigationArray?.count == nil{
self.navigationController?.pushViewController(mainVC, animated: false)
}else{
self.navigationController?.popToRootViewController(animated: …Run Code Online (Sandbox Code Playgroud)