Swift 从不同的控制器调用视图控制器或从不同的控制器调用警报
Swift calling a view controller from a different controller or calling an alert from different controller
我想知道是否可以在 swift 中调用警报或控制器或从单独的控制器中简化功能。我想从 viewcontroller1 访问我的警报到应用程序委托,我想在应用程序委托中触发它而不是创建另一个警报,我有一个目的为什么我想在应用程序委托中触发它。这可能吗?谢谢你 。我可以在应用程序委托的视图控制器 1 中调用我的警报吗?而且我想在应用程序委托中从视图控制器 1 调用我的 collection 视图,这可能吗?
collection 查看代码
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// handle tap events
// print("You selected cell #\(indexPath.item)!")
// print("emong gepili:" , titleArray[indexPath.row])
if indexPath.row == 0 {
var a = loggedInUsername
if ((a?.range(of: "mother")) != nil) {
performSegue(withIdentifier: "goToSegue", sender: nil)
print("yolo")
}else {
print("do nothing")
var alert = UIAlertController(title: "No Access", message: "You Can't Add A Chore", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
} else {
print("You selected cell #\(indexPath.item)!")
if let getTempDetails: [String : Any] = getAllDetail[indexPath.row],
let name = getTempDetails["name"] as? String,
let id = getTempDetails["id"] as? Int,
let description = getTempDetails["desc"] as? String,
let chorereward = getTempDetails["reward"] as? String,
let choreschedule = getTempDetails["sched"] as? String
// let chorescheds = getTempDetails["sched"] as? String
// let parent = getTempDetails["parent"] as? String,
// let child = getTempDetails["child"] as? String,
// let occurrence = getTempDetails["occurrence"] as? String,
// let status = getTempDetails["date_created"] as? String,
// let datec = getTempDetails["status"] as? String,
// let datemod = getTempDetails["date_modified"] as? String {
{
let stat = getTempDetails["status"] as! NSDictionary
let statname = stat["name"]
let str = getTempDetails["occurrence"] as! NSDictionary
let strname = str["name"]
警报代码
let alert = UIAlertController(title: "Chore Name: \(getTempDetails["name"] as! String)", message: "", preferredStyle:
UIAlertControllerStyle.alert)
alert.addTextField { (choreField) in
choreField.text = getTempDetails["name"] as! String
}
alert.addTextField { (rewardField) in
// rewardField.text = getTempDetails["reward"] as! String
let rewardData = getTempDetails["reward"]
let reward = (rewardData as! NSString).integerValue
rewardField.placeholder = "Chore Reward"
rewardField.text = String(reward)
}
alert.addTextField { (idField) in
idField.placeholder = "Email"
var a = "\(getTempDetails["id"] as? Int ?? 0)!"
a.removeLast()
idField.text = a
}
........
创建一个新的 NSObject class 并将全局函数作为其 class 函数,如下例所示
class wrapperClass: NSObject
{
class func alert(_ title : String, message : String, view:UIViewController)
{
let alert = UIAlertController(title:title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
view.present(alert, animated: true, completion: nil)
}
}
用法
wrapperClass.alert("Service Remainder", message: "Enter Username", view: self)
注意 - AppDelgate 不是视图控制器,因此您不能在 AppDelegate 中调用此函数您可以在第一个显示的控制器中做一件事
因此,根据我的理解,您正试图在 AppDelegate
期间显示警报。
但是不可能在那里显示,基本上会发生的是 AppDelegate
将在应用程序启动时被调用,之后将调用堆栈中第一个的视图控制器。
如果您在 AppDelegate 中设置了任何条件,并且在这种情况下想要在那里显示警报,
可以使用
func showAlertAppDelegate(title : String,message : String,buttonTitle : String,window: UIWindow){
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: buttonTitle, style: UIAlertActionStyle.default, handler: nil))
window.rootViewController?.present(alert, animated: true, completion: nil)
}
并像这样使用它:
showAlertAppDelegate(title: "Alert!", message: "Message", buttonTitle: "Ok", window: self.window!)
注意:-此警报将在调用 App 委托后加载的第一个 VC 上显示。
使用此扩展程序在应用程序中的任何位置显示警报或 ViewController:
extension UIApplication {
class func topViewController(controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let navigationController = controller as? UINavigationController {
return topViewController(controller: navigationController.visibleViewController)
}
if let tabController = controller as? UITabBarController {
if let selected = tabController.selectedViewController {
return topViewController(controller: selected)
}
}
if let presented = controller?.presentedViewController {
return topViewController(controller: presented)
}
return controller
}
}
如何使用?
UIApplication.topViewController()?.present(yourAlertVC, animated: true, completion: nil)
Now What I've understood from your problem,
Clearly calling collection view's delegate methods from AppDelegate is a wrong approach as iOS Geek said it is Singleton.
But what you can do you can create a observer for that Like
the below one.
NotificationCenter.default.addObserver(self, selector: #selector(theMethodWhichYouWanaHit(params:)), name: Notification.Name("Notification_Name"), object: nil)
方法如下:
func theMethodWhichYouWanaHit(_ params : Notification){
collectionView.delegate = self
collectionView.dataSource = self
let yourObject = params.object
// use the object it has all the data in it (aka objectToBePassed )
}
大功告成!
现在,只需 post 观察者就像一个函数调用一样,它会完成剩下的工作。:
NotificationCenter.default.post(name: NSNotification.Name("Notification_Name"), object: objectToPassed, userInfo:nil)
为了移除观察者:
NotificationCenter.default.removeObserver(self, name: NSNotification.Name("Notification_Name"), object: nil)
我想知道是否可以在 swift 中调用警报或控制器或从单独的控制器中简化功能。我想从 viewcontroller1 访问我的警报到应用程序委托,我想在应用程序委托中触发它而不是创建另一个警报,我有一个目的为什么我想在应用程序委托中触发它。这可能吗?谢谢你 。我可以在应用程序委托的视图控制器 1 中调用我的警报吗?而且我想在应用程序委托中从视图控制器 1 调用我的 collection 视图,这可能吗?
collection 查看代码
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// handle tap events
// print("You selected cell #\(indexPath.item)!")
// print("emong gepili:" , titleArray[indexPath.row])
if indexPath.row == 0 {
var a = loggedInUsername
if ((a?.range(of: "mother")) != nil) {
performSegue(withIdentifier: "goToSegue", sender: nil)
print("yolo")
}else {
print("do nothing")
var alert = UIAlertController(title: "No Access", message: "You Can't Add A Chore", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
} else {
print("You selected cell #\(indexPath.item)!")
if let getTempDetails: [String : Any] = getAllDetail[indexPath.row],
let name = getTempDetails["name"] as? String,
let id = getTempDetails["id"] as? Int,
let description = getTempDetails["desc"] as? String,
let chorereward = getTempDetails["reward"] as? String,
let choreschedule = getTempDetails["sched"] as? String
// let chorescheds = getTempDetails["sched"] as? String
// let parent = getTempDetails["parent"] as? String,
// let child = getTempDetails["child"] as? String,
// let occurrence = getTempDetails["occurrence"] as? String,
// let status = getTempDetails["date_created"] as? String,
// let datec = getTempDetails["status"] as? String,
// let datemod = getTempDetails["date_modified"] as? String {
{
let stat = getTempDetails["status"] as! NSDictionary
let statname = stat["name"]
let str = getTempDetails["occurrence"] as! NSDictionary
let strname = str["name"]
警报代码
let alert = UIAlertController(title: "Chore Name: \(getTempDetails["name"] as! String)", message: "", preferredStyle:
UIAlertControllerStyle.alert)
alert.addTextField { (choreField) in
choreField.text = getTempDetails["name"] as! String
}
alert.addTextField { (rewardField) in
// rewardField.text = getTempDetails["reward"] as! String
let rewardData = getTempDetails["reward"]
let reward = (rewardData as! NSString).integerValue
rewardField.placeholder = "Chore Reward"
rewardField.text = String(reward)
}
alert.addTextField { (idField) in
idField.placeholder = "Email"
var a = "\(getTempDetails["id"] as? Int ?? 0)!"
a.removeLast()
idField.text = a
}
........
创建一个新的 NSObject class 并将全局函数作为其 class 函数,如下例所示
class wrapperClass: NSObject
{
class func alert(_ title : String, message : String, view:UIViewController)
{
let alert = UIAlertController(title:title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
view.present(alert, animated: true, completion: nil)
}
}
用法
wrapperClass.alert("Service Remainder", message: "Enter Username", view: self)
注意 - AppDelgate 不是视图控制器,因此您不能在 AppDelegate 中调用此函数您可以在第一个显示的控制器中做一件事
因此,根据我的理解,您正试图在 AppDelegate
期间显示警报。
但是不可能在那里显示,基本上会发生的是 AppDelegate
将在应用程序启动时被调用,之后将调用堆栈中第一个的视图控制器。
如果您在 AppDelegate 中设置了任何条件,并且在这种情况下想要在那里显示警报,
可以使用
func showAlertAppDelegate(title : String,message : String,buttonTitle : String,window: UIWindow){
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: buttonTitle, style: UIAlertActionStyle.default, handler: nil))
window.rootViewController?.present(alert, animated: true, completion: nil)
}
并像这样使用它:
showAlertAppDelegate(title: "Alert!", message: "Message", buttonTitle: "Ok", window: self.window!)
注意:-此警报将在调用 App 委托后加载的第一个 VC 上显示。
使用此扩展程序在应用程序中的任何位置显示警报或 ViewController:
extension UIApplication {
class func topViewController(controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let navigationController = controller as? UINavigationController {
return topViewController(controller: navigationController.visibleViewController)
}
if let tabController = controller as? UITabBarController {
if let selected = tabController.selectedViewController {
return topViewController(controller: selected)
}
}
if let presented = controller?.presentedViewController {
return topViewController(controller: presented)
}
return controller
}
}
如何使用?
UIApplication.topViewController()?.present(yourAlertVC, animated: true, completion: nil)
Now What I've understood from your problem,
Clearly calling collection view's delegate methods from AppDelegate is a wrong approach as iOS Geek said it is Singleton. But what you can do you can create a observer for that Like the below one.
NotificationCenter.default.addObserver(self, selector: #selector(theMethodWhichYouWanaHit(params:)), name: Notification.Name("Notification_Name"), object: nil)
方法如下:
func theMethodWhichYouWanaHit(_ params : Notification){
collectionView.delegate = self
collectionView.dataSource = self
let yourObject = params.object
// use the object it has all the data in it (aka objectToBePassed )
}
大功告成!
现在,只需 post 观察者就像一个函数调用一样,它会完成剩下的工作。:
NotificationCenter.default.post(name: NSNotification.Name("Notification_Name"), object: objectToPassed, userInfo:nil)
为了移除观察者:
NotificationCenter.default.removeObserver(self, name: NSNotification.Name("Notification_Name"), object: nil)