Ios 显示自定义类中的alertController
我正在尝试显示我制作的类中的AlertController。 因为AlertController是UIResponder的一个子类,所以我使用Xcode建议我使用的以下代码行Ios 显示自定义类中的alertController,ios,swift,alert,nsobject,uialertcontroller,Ios,Swift,Alert,Nsobject,Uialertcontroller,我正在尝试显示我制作的类中的AlertController。 因为AlertController是UIResponder的一个子类,所以我使用Xcode建议我使用的以下代码行 superclass?.presentViewController(alertController, animated: true, completion: nil) 但是我不能编译,因为任何类?没有任何成员presentViewController。 我的类是NSObject的一个子类 还有其他解决办法吗? 谢谢问题在
superclass?.presentViewController(alertController, animated: true, completion: nil)
但是我不能编译,因为任何类?没有任何成员presentViewController。
我的类是NSObject的一个子类
还有其他解决办法吗?
谢谢问题在于你对“from”的理解。在界面中的某个视图前面会出现一个警报。因此,我们需要知道什么观点。答案是:某个视图控制器的主视图-其主视图位于界面中的视图控制器 因此,只有主视图位于界面中的视图控制器才能被告知显示警报。您必须“从”显示视图控制器
您需要有某种方法从代码所在的位置获取对该视图控制器的引用,以便代码可以告诉该视图控制器显示警报。这本身就是一个有趣的问题;事实上,“获取对现有对象的引用”是Cocoa编程艺术的一个主要部分。问题在于您对“来自”的理解。在界面中的某个视图前面会出现一个警报。因此,我们需要知道什么观点。答案是:某个视图控制器的主视图-其主视图位于界面中的视图控制器 因此,只有主视图位于界面中的视图控制器才能被告知显示警报。您必须“从”显示视图控制器
您需要有某种方法从代码所在的位置获取对该视图控制器的引用,以便代码可以告诉该视图控制器显示警报。这本身就是一个有趣的问题;实际上,“获取对现有对象的引用”是Cocoa编程艺术的一个主要部分。从您的视图控制器中,您可以传递控制器的引用、消息、标题,例如
Settings.getAlertViewConroller(self, DialogTitle: "Test Sale", strDialogMessege: "You are performing a test sale. This is not a real transaction.")
其中Setting是NSObject的子类。在Setting类中,u必须将方法定义为
class func getAlertViewConroller(globleAlert:UIViewController,DialogTitle:NSString,strDialogMessege:NSString){
let actionSheetController: UIAlertController = UIAlertController(title: DialogTitle, message: strDialogMessege, preferredStyle: .Alert)
let nextAction: UIAlertAction = UIAlertAction(title: "OK", style: .Default) { action -> Void in
}
actionSheetController.addAction(nextAction)
globleAlert.presentViewController(actionSheetController, animated: true, completion:nil)
}
i、 显示UIAlertController。从您的视图控制器中,您可以传递控制器的参考、消息、标题,例如
Settings.getAlertViewConroller(self, DialogTitle: "Test Sale", strDialogMessege: "You are performing a test sale. This is not a real transaction.")
其中Setting是NSObject的子类。在Setting类中,u必须将方法定义为
class func getAlertViewConroller(globleAlert:UIViewController,DialogTitle:NSString,strDialogMessege:NSString){
let actionSheetController: UIAlertController = UIAlertController(title: DialogTitle, message: strDialogMessege, preferredStyle: .Alert)
let nextAction: UIAlertAction = UIAlertAction(title: "OK", style: .Default) { action -> Void in
}
actionSheetController.addAction(nextAction)
globleAlert.presentViewController(actionSheetController, animated: true, completion:nil)
}
i、 e显示UIAlertController。您只需找到最顶层的视图控制器,然后从那里显示
alertcontroller
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
[topController presentViewController:alertController animated:YES completion:nil];
注意:这是代码的objective-c版本。请相应地将其转换为swift
SWIFT
let topController = UIApplication.sharedApplication().keyWindow!.rootViewController as UIViewController
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
topController.presentViewController(alertController, animated:true, completion:nil)
您只需要找到最上面的视图控制器,然后从那里显示
alertcontroller
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
[topController presentViewController:alertController animated:YES completion:nil];
注意:这是代码的objective-c版本。请相应地将其转换为swift
SWIFT
let topController = UIApplication.sharedApplication().keyWindow!.rootViewController as UIViewController
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
topController.presentViewController(alertController, animated:true, completion:nil)
最新Swift:
var topController:UIViewController = UIApplication.shared.keyWindow!.rootViewController!
while ((topController.presentedViewController) != nil) {
topController = topController.presentedViewController!;
}
topController.present(alertController, animated:true, completion:nil)
最新Swift:
var topController:UIViewController = UIApplication.shared.keyWindow!.rootViewController!
while ((topController.presentedViewController) != nil) {
topController = topController.presentedViewController!;
}
topController.present(alertController, animated:true, completion:nil)
presentViewController
是UIViewController
的一种方法,不是NSObject
@Krumelur的方法,我知道,但我正在尝试显示我的类中的UIAlertController。还有其他解决方法吗?presentViewController
是UIViewController
的一种方法,而不是NSObject
@Krumelur的方法,我知道,但我正在尝试从我的类中显示UIAlertController。还有其他解决方法吗?这是一个如此重要的问题(“获得参考”)以至于我在书中花了一节来讨论它:它可能会给你一些想法。天才!只需在init方法中包含UIViewController,瞧。完成了。这是一个如此重要的问题(“获得参考”)以至于我在书中花了一部分来讨论它:它可能会给你一些想法。天才!只需在init方法中包含UIViewController,瞧。对不起,但我要求SWIFT,我不知道如何处理OBC。请考虑使用SWIFT语言修改,但我要求SWIFT,我不知道如何处理OBC。请考虑用SWIFT语言修改。