在导航控制器中embedded的Tab中,在后退button旁边添加额外的button
我有一个故事板,看起来像所示的图像 。 单个导航控制器pipe理我的向下钻取导航,向下钻取所有视图1,2,3和4时添加一个“后退”button。 我想要做的是,在右侧的“后退”导航栏上添加一个额外的button,用于3个选项卡视图中的每一个(2,3和4)。这可以在Swift中完成吗? 谁能帮忙? 我不能使用故事板添加它,因为button似乎在故事板上消失,并且在运行应用程序时不显示。
你不想添加另一个返回button; 你想添加一个正确的酒吧button项目。 您可以在代码或故事板中执行此操作。
在代码中,您需要执行以下操作:
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Some Title", style: .Plain, target: self, action: <your-selector>)
target
和action
指向你的视图控制器(或另一个对象,如果这是你如何设置)的某种方法。
在故事板中,
- 如果视图控制器还没有,请将
UINavigationItem
拖放到每个视图控制器。 - 将
UIBarButtonItem
到每个视图控制器导航栏的右侧。
- configurationbutton的标题或图像。
- 按住Control键从右侧的button项拖动到它所在的视图控制器,并select点击该button时想要触发的
IBAction
方法。
尝试拖动一个栏button项,而不是UIbutton。 你可以在每个控制器的负载中设置button的标题,甚至可以在代码中创buildbutton。 这是如何做到这一点
override func viewDidLoad() { super.viewDidLoad() let barButton = UIBarButtonItem(title: "VC1 bar button", style: UIBarButtonItemStyle.Plain, target: self, action: "The action function name") self.navigationItem.rightBarButtonItem = barButton }
把这个放在你的viewDidLoad()
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Custom, target: self, action: "*add a func saying what you want the button to do*")
例:
func save() { UIImageWriteToSavedPhotosAlbum(imageView.image!, self, "image:didFinishSavingWithError:contextInfo:", nil) }
所以在这种情况下,我会说:
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Custom, target: self, action: "save")
我希望这有帮助!
注意:只有在运行应用程序时,才能在故事板中看到它
我find了一个简单的方法来做到这一点
let barButton = UIBarButtonItem(image: UIImage(named: "YourImage"), style: .plain, target: self, action: #selector(yourClass.yourFunction(_:))) self.navigationController?.topViewController?.navigationItem.rightBarButtonItem = barButton
添加这个代码和所有视图控制器,你想添加一个正确的button,除了后退button。
我希望这个代码能帮助你!