gpt4 book ai didi

swift - 快速访问navigationController subview 的项目

转载 作者:行者123 更新时间:2023-11-30 14:17:29 26 4
gpt4 key购买 nike

这里是 ios 应用程序编码的新手,因此我们将不胜感激。

我有一个应用程序,当旋转到横向时会自动打开侧面菜单,我需要它来禁用导航 Controller subview 中的菜单按钮。这是我的代码。

导航 Controller

import Foundation
class GDNavigationController:UINavigationController{
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//Send notification when the device is rotated.
NSNotificationCenter.defaultCenter().addObserver(self, selector: "rotated", name: UIDeviceOrientationDidChangeNotification, object: nil)
}

func rotated(){
/*
Put the code here to access the menu button
*/
if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
{
//disable the menu button here
self.revealViewController().setFrontViewPosition(FrontViewPosition.Right, animated: false)
}

if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
{
//enable the menu button here
self.revealViewController().setFrontViewPosition(FrontViewPosition.Left, animated: false)
}
}
}

我的ViewController代码

import Foundation
class LocalCollectionController: UICollectionViewController{
@IBOutlet var MenuViewButton: UIBarButtonItem!
override func viewDidLoad() {
MenuViewButton.target = self.revealViewController()
MenuViewButton.action = Selector("revealToggle:")
}
}

根据选择的菜单项,我有不同的导航 Controller 加载不同的 View Controller 。不同的 navigationController 共享相同的子类,但我永远不知道加载了哪个 viewController,这就是我需要找出的内容以及如何访问该 viewController 中的按钮。

有人可以帮忙吗?

最佳答案

所以我想通了,将我的导航 Controller 更改为这个

导入基础类 GDNavigationController:UINavigationController{

var BBItem = UIBarButtonItem()
override func viewDidAppear(animated: Bool) {
BBItem = self.topViewController.navigationItem.leftBarButtonItem!

}

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//Send notification when the device is rotated.
NSNotificationCenter.defaultCenter().addObserver(self, selector: "rotated", name: UIDeviceOrientationDidChangeNotification, object: nil)
self.rotated()
}


func rotated(){

if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
{
self.revealViewController().setFrontViewPosition(FrontViewPosition.Right, animated: true)
self.BBItem.enabled = false
}

if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
{
self.revealViewController().setFrontViewPosition(FrontViewPosition.Left, animated: true)
self.BBItem.enabled = true
}

}

}

关于swift - 快速访问navigationController subview 的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30949990/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com