我的所有视图控制器都使用标签栏控制器进行链接,效果很好。我唯一的问题是当显示弹出框时我无法将用户发送到另一个视图控制器以获得适当的环境。我尝试使用
tabBarController.selectedIndex = 1
以下是一些相关的代码:
//user selects a row this function triggers an event
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView == self.tableviewlineneeds
{
forklift.paramneeds = "\(forklift.k)&f=\(forklift.forkliftid)&id=\(forklift.needuniqueid[(indexPath as NSIndexPath).row])&o="
let popneeds = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "verifyneedsaction") as! NeedsActionPopover
popneeds.modalPresentationStyle = UIModalPresentationStyle.popover
popneeds.popoverPresentationController!.delegate = self
popneeds.popoverPresentationController?.permittedArrowDirections = .any
popneeds.popoverPresentationController?.sourceView = tableviewlineneeds
popneeds.popoverPresentationController?.sourceRect = tableviewlineneeds.rectForRow(at: indexPath)
popneeds.preferredContentSize = CGSize(width: 225, height: 180)
popneeds.needitem = forklift.itemneeds[(indexPath as NSIndexPath).row]
self.present(popneeds, animated: true, completion: nil)
}
if tableView == self.tableviewlinepickup
{
forklift.parampickup = "\(forklift.k)&f=\(forklift.forkliftid)&id=\(forklift.pickupuniqueid[(indexPath as NSIndexPath).row])&o="
let poppickup = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "verifypickupaction") as! PickupActionPopover
poppickup.modalPresentationStyle = UIModalPresentationStyle.popover
poppickup.popoverPresentationController!.delegate = self
poppickup.popoverPresentationController?.permittedArrowDirections = .any
poppickup.popoverPresentationController?.sourceView = tableviewlinepickup
poppickup.popoverPresentationController?.sourceRect = tableviewlinepickup.rectForRow(at: indexPath)
poppickup.preferredContentSize = CGSize(width: 300, height: 180)
forklift.pickuplocation = forklift.locationpickup[(indexPath as NSIndexPath).row]
poppickup.pickupitem = forklift.itempickup[(indexPath as NSIndexPath).row]
self.present(poppickup, animated: true, completion: nil)
}
}
这是触发弹出窗口的地方。这是放松赛段:
@IBAction func unwindLinePickup(_ segue: UIStoryboardSegue)
{
if segue.identifier == "confirm"
{
switch forklift.pickuplocation
{
case "Line 2":
self.sendToMoveView()
case "Line 3":
self.sendToMoveView()
case "Line 7":
self.sendToMoveView()
case "Line 8":
self.sendToMoveView()
default:
API.HTTPCall(forklift.parampickup, url: forklift.postmove!, method: "POST", int: 10)
}
}
}
这是我发送到扫描仪视图控制器的功能:
func sendToMoveView()
{
guard let tabBarController = tabBarController else { return }
tabBarController.selectedIndex = 1
}
我尝试在像viewDidAppear覆盖这样的地方使用tabBarController.selectedIndex,它可用于设置默认或布局子视图,我在AppDelegate中使用它之前设置了默认视图。所以在这种情况下我必须做什么才能使用它,因为以任何其他方式导航到VC会给我一个没有标签栏的VC :(我非常感谢任何帮助,谢谢。
答案 0 :(得分:0)
我在这个问题中找到了答案的线索:Change UITabbar selectedItem in Swift
然后我从初始视图控制器中删除了我的unwind segue并将其嵌入到标签栏控制器中,并将我的sendToMoveView函数更改为如下所示:
func sendToMoveView()
{
self.selectedViewController = self.viewControllers?[1]
}
我还在故事板中重新展开了我的展开连接,只是为了安全,即使它显示了正确的控制器。只是想指出,如果您没有为tabbar控制器提供正确的故事板ID,我所做的更改将不起作用。这个让我头疼。