从其他视图控制器调用 UIBarButtonItem
Call UIBarButtonItem from other view controller
我想在导航控制器(父亲)上实现一个 UIBarButtonItem,它嵌入了一个带有三个选项卡的 TabBar 控制器,我想在它上面实现一个动作,但是可以从所有视图调用它来实现不同的每个视图上的 UIBarButtonItem。
我想要的是当点击 UIBarButtonItem 时显示一个 ActionSheet,无论用户在哪个视图中,它在所有视图上都是相同的。
你能帮我吗,我在 iOS 和 Swift 编程方面真的很新。
谢谢!
好的,我想我已经回答了我自己的问题,而且非常简单。我只是创建了一个自定义视图控制器文件,并在我的历史板上与我的 TabBar 视图相关联,并为 UIButtonBarItem 添加操作,如下所示:
import UIKit
class CustomTabBarViewControler: UITabBarController {
@IBAction func showActionSheet(sender: UIBarButtonItem) {
let myActionSheet = UIAlertController(title: "Log in", message: "How do you want to log in", preferredStyle: UIAlertControllerStyle.ActionSheet)
let fbLoginButton = UIAlertAction(title: "Facebook", style: UIAlertActionStyle.Default)
{ (ACTION) in
print("Facebook Button Tapped")
}
let googlePlusButton = UIAlertAction(title: "Google Plus", style: UIAlertActionStyle.Default) {
(ACTION) in
print("Google Plus Button Tapped")
}
let twitterButton = UIAlertAction(title: "Twitter", style: UIAlertActionStyle.Default) {
(ACTION) in
print("Twitter Button Tapped")
}
let cancelButton = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) {
(ACTION) in
print("Cancel Button Tapped")
}
myActionSheet.addAction(fbLoginButton)
myActionSheet.addAction(googlePlusButton)
myActionSheet.addAction(twitterButton)
myActionSheet.addAction(cancelButton)
self.presentViewController(myActionSheet, animated: true, completion: nil)
}
}
这对我来说很好用。
我想在导航控制器(父亲)上实现一个 UIBarButtonItem,它嵌入了一个带有三个选项卡的 TabBar 控制器,我想在它上面实现一个动作,但是可以从所有视图调用它来实现不同的每个视图上的 UIBarButtonItem。
我想要的是当点击 UIBarButtonItem 时显示一个 ActionSheet,无论用户在哪个视图中,它在所有视图上都是相同的。
你能帮我吗,我在 iOS 和 Swift 编程方面真的很新。
谢谢!
好的,我想我已经回答了我自己的问题,而且非常简单。我只是创建了一个自定义视图控制器文件,并在我的历史板上与我的 TabBar 视图相关联,并为 UIButtonBarItem 添加操作,如下所示:
import UIKit
class CustomTabBarViewControler: UITabBarController {
@IBAction func showActionSheet(sender: UIBarButtonItem) {
let myActionSheet = UIAlertController(title: "Log in", message: "How do you want to log in", preferredStyle: UIAlertControllerStyle.ActionSheet)
let fbLoginButton = UIAlertAction(title: "Facebook", style: UIAlertActionStyle.Default)
{ (ACTION) in
print("Facebook Button Tapped")
}
let googlePlusButton = UIAlertAction(title: "Google Plus", style: UIAlertActionStyle.Default) {
(ACTION) in
print("Google Plus Button Tapped")
}
let twitterButton = UIAlertAction(title: "Twitter", style: UIAlertActionStyle.Default) {
(ACTION) in
print("Twitter Button Tapped")
}
let cancelButton = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) {
(ACTION) in
print("Cancel Button Tapped")
}
myActionSheet.addAction(fbLoginButton)
myActionSheet.addAction(googlePlusButton)
myActionSheet.addAction(twitterButton)
myActionSheet.addAction(cancelButton)
self.presentViewController(myActionSheet, animated: true, completion: nil)
}
}
这对我来说很好用。