如何从放置在 UIAlertController 上的 UITextField 处理程序访问放置在 UIAlertController 上的按钮?

     2023-03-17     284

关键词:

【中文标题】如何从放置在 UIAlertController 上的 UITextField 处理程序访问放置在 UIAlertController 上的按钮?【英文标题】:How to get access to button placed on UIAlertController from handler of UITextField placed on UIAlertController? 【发布时间】:2016-10-02 14:25:11 【问题描述】:

我使用 UIAlertController 要求用户输入一个字符串,这个字符串不应该为空。为此,我正在为 UIAlertController 上的 UITextField 上的每个编辑事件添加处理程序。此处理程序检查,如果 textField 为空,则禁用 «OK» 按钮(位于 UIAlertController 上),如果 textField 不为空,则启用 «OK» 按钮。 «OK» 按钮也位于 UIAlertController 上。 我无法从此访问“确定”按钮 处理函数:

    没有机会将按钮作为参数直接传递给处理函数,因为我使用#selector 调用它。 我也尝试通过处理函数的 UITextField.superview 访问 UIAlertController,但没有成功:它返回了 UIView,无法向下转换为 UIAlertController。不清楚为什么会这样:UITextField 是 UIAlertController 的子视图,因此 UITextField.superview 必须返回 UIAlertController。 我还尝试了最简单的方法:在我的 ViewController 类中将 UIAlertController 声明为属性:这允许我从处理函数访问按钮,而无需将其直接传递给函数。但在这种情况下,我遇到了另一个问题:在运行时 Xcode 在调试区域显示此警告:不允许在解除分配时尝试加载视图控制器的视图,并且可能导致未定义的行为 ()

我的问题是如何从我的处理程序函数中访问“确定”按钮?

class TVC_ProgramsList: UITableViewController 

    var enterNameAC = UIAlertController()

    @IBAction func addpush(sender: UIBarButtonItem) 
        addProgram()
    

    func addProgram() 

        enterNameAC = UIAlertController(title: "Adding program", message: "Enter program name", preferredStyle: UIAlertControllerStyle.Alert)
        enterNameAC.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
        enterNameAC.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
        enterNameAC.addTextFieldWithConfigurationHandler  (textField) -> Void in
            textField.placeholder = "Program name"

            textField.addTarget(self, action: #selector(TVC_ProgramsList.enterProgramNameAC_editingTextField), forControlEvents: UIControlEvents.AllEditingEvents)

        

        self.presentViewController(enterNameAC, animated: true, completion: nil)

    


    func enterProgramNameAC_editingTextField() 
        let programNameString = enterNameAC.textFields![0].text!

        if programNameString.characters.count > 0 
            enterNameAC.actions[1].enabled = true
         else 
            enterNameAC.actions[1].enabled = false
        
    


【问题讨论】:

【参考方案1】:

你为什么不做一个UIAlertAction的属性?我已经更改了您的代码:

class TVC_ProgramsList: UITableViewController 

  var enterNameAC = UIAlertController()
  let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)

  @IBAction func addpush(sender: UIBarButtonItem) 
     addProgram()
  

  func addProgram() 
    enterNameAC = UIAlertController(title: "Adding program", message: "Enter program name", preferredStyle: UIAlertControllerStyle.Alert)
    enterNameAC.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
    enterNameAC.addAction(self.okAction)
    enterNameAC.addTextFieldWithConfigurationHandler  (textField) -> Void in
        textField.placeholder = "Program name"
        textField.addTarget(self, action: #selector(TVC_ProgramsList.enterProgramNameAC_editingTextField), forControlEvents: UIControlEvents.AllEditingEvents)

    

    self.presentViewController(enterNameAC, animated: true, completion: nil)

  

  func enterProgramNameAC_editingTextField() 
    let programNameString = enterNameAC.textFields![0].text!
    // use okAction here 
    self.okAction.enabled = programNameString.characters.count > 0
  

addTarget 传递它是不可能的。但这是一种有效的方法,可以从中生成类属性,从而使其在 ViewController 的其他部分中可访问。

【讨论】:

我是 Swift 和 Xcode 的新手,在问我的问题之前,我认为将其声明为类属性 UIAlertController 或其子视图是一种粗略的方式,我认为这种方式不会不符合 Apple 的建议或类似的东西:) 但是现在,当你说这是正确的方法时,我对此很冷静。 一个类中有多个属性是很正常的。一旦多个函数需要一个对象,您就应该开始考虑创建属性,而不是一直将对象作为参数传递。【参考方案2】:

我还找到了这个问题的另一个解决方案:懒惰声明 enterNameAC 以避免在运行时在调试区域出现警报消息就足够了:

lazy var enterNameAC = UIAlertController()

但这还不是全部:我只说是关于 Swift 2,但昨天我更新到 Swift 3,而且,这是奇迹:它可以在没有任何调试区域的情况下工作消息,即使没有惰性声明!

【讨论】:

如何从 Appdelegate 显示 UIAlertController

】如何从Appdelegate显示UIAlertController【英文标题】:HowtoshowUIAlertControllerfromAppdelegate【发布时间】:2016-03-2213:31:25【问题描述】:我正在iOS应用上使用PushNotification。我想在应用收到通知时显示UIalertcontroller。我在AppDelegate中尝试以... 查看详情

如何从 UIAlertcontroller 关闭模态 ViewController

】如何从UIAlertcontroller关闭模态ViewController【英文标题】:HowtodismissmodalViewControllerfromUIAlertcontroller【发布时间】:2016-09-0909:47:43【问题描述】:我展示了一个模态viewcontroller,用户可以在该模态上决定编辑或删除显示的汽车。如... 查看详情

如何在 UIAlertController 上添加动态按钮?

】如何在UIAlertController上添加动态按钮?【英文标题】:HowtoadddynamicbuttonsonUIAlertController?【发布时间】:2015-11-1920:22:20【问题描述】:我正在将我的代码从使用UIActionSheet转换为使用UIAlertController。我使用UIActionSheet的方式是这样的... 查看详情

如何从任何 NSObject 子类显示 UIAlertController?

】如何从任何NSObject子类显示UIAlertController?【英文标题】:HowdoidisplayUIAlertControllerfromanyNSObjectsubclass?【发布时间】:2015-07-1709:17:29【问题描述】:我正在管理一个遗留代码库,它需要显示来自各地的警报消息。这是一种糟糕的... 查看详情

将 UIPickerView 放在 UIAlertController 的中心

】将UIPickerView放在UIAlertController的中心【英文标题】:PlaceUIPickerViewinthecentreoftheUIAlertController【发布时间】:2018-12-2513:41:45【问题描述】:如何将UIPickerView放置在UIAlertController的中心?现在我有了这个代码:letalertController=UIAlertCont... 查看详情

如何从视图中完全消除 uialertcontroller?

】如何从视图中完全消除uialertcontroller?【英文标题】:Howtodismissuialertcontrollerfromviewcompletely?【发布时间】:2015-06-1707:24:38【问题描述】:实际上,我有一个具有4种方法的视图控制器。3种方法用于显示UIAlertController,没有“确定... 查看详情

从 MSMessagesAppViewController 正确显示 UIAlertController

】从MSMessagesAppViewController正确显示UIAlertController【英文标题】:ProperlyPresentaUIAlertControllerfromMSMessagesAppViewController【发布时间】:2016-09-2901:15:48【问题描述】:我正在尝试弄清楚如何在我的iMessage应用程序扩展中显示具有UIAlertContro... 查看详情

从 UIAlertController 按钮单击导航到 ViewController

】从UIAlertController按钮单击导航到ViewController【英文标题】:NavigatetoViewControllerfromUIAlertControllerbuttonclick【发布时间】:2015-02-1016:45:42【问题描述】:我对Swift开发很陌生,我尝试参考SwiftUIAlertControllerAPI,但在单击UIAlertController上... 查看详情

从 AppDelegate 到 PresentedViewController 的警报:“尝试在...上呈现 UIAlertController 已经在呈现 UIAlertController”

】从AppDelegate到PresentedViewController的警报:“尝试在...上呈现UIAlertController已经在呈现UIAlertController”【英文标题】:AlertfromAppDelegatetoPresentedViewController:"AttempttopresentUIAlertControlleron...whitchisalreadypresentingUIAlert 查看详情

如何从 UIAlertController 的 UIAlertAction 处理程序中关闭 UIViewController?

】如何从UIAlertController的UIAlertAction处理程序中关闭UIViewController?【英文标题】:HowtodismissaUIViewControllerfromaUIAlertController\'sUIAlertActionhandler?【发布时间】:2016-03-0422:36:16【问题描述】:我想向用户展示一个简单的UIAlertController,其... 查看详情

注册通知时如何从存在的 UIAlertcontroller 获取 UIAlertAction

】注册通知时如何从存在的UIAlertcontroller获取UIAlertAction【英文标题】:HowtogetaUIAlertActionfromUIAlertcontrollerpresentedwhenregisteringForNotifications【发布时间】:2017-02-1004:13:03【问题描述】:我想根据用户是否允许/不注册通知来对视图控制... 查看详情

如何将 UIAlertController 输出到单独的文件并从那里输出数据?

】如何将UIAlertController输出到单独的文件并从那里输出数据?【英文标题】:HowcanIoutputUIAlertControllertoaseparatefileandoutputdatafromthere?【发布时间】:2021-11-0501:51:30【问题描述】:我正在尝试将警报输出到单独的函数,因为会有很多... 查看详情

禁止uialertcontroller的dimiss(代码片段)

UIAlertController是在iOS8.0以后才出现的,用于弹窗提示的视图,在8.0之前使用的是UIAlertView。UIAlertController有两种模式:Alert和Sheet。只有在Alert模式下,才可以向UIAlertController中添加UITextField,否则会崩溃。默认情况都是点击UIAlertCont... 查看详情

如何在 Swift 中旋转 UIAlertController

】如何在Swift中旋转UIAlertController【英文标题】:HowtorotateUIAlertControllerinSwift【发布时间】:2016-08-2310:30:25【问题描述】:我有一个工作的UIAlertController,但我想将alert.view向左旋转90度。我该怎么做?我的代码如下:letalert=UIAlertCon... 查看详情

从uialertview到uialertcontroller

 在iOS8中,苹果提供了UIAlertController,以取代原有的 UIAlertView和UIActionSheet,以前的 UIAlertView是这样的:1UIAlertView*alertView=[[UIAlertViewalloc]2initWithTitle:@"DefaultStyle"3message:@"thedefaultalertvie 查看详情

UIAlertController - 在警报中显示空白标题

】UIAlertController-在警报中显示空白标题【英文标题】:UIAlertController-showingblanktitlesinthealert【发布时间】:2016-10-0414:14:30【问题描述】:我们的一个应用程序有警报,我们曾经使用UIAlertView。自从UIAlertView在iOS9中被弃用以来,我们... 查看详情

如何在 UIAlertController 中提供超链接操作?

】如何在UIAlertController中提供超链接操作?【英文标题】:HowtogivehyperlinkactioninUIAlertController?【发布时间】:2020-02-1408:13:44【问题描述】:我无法单击UIAlertController中的超链接。我想在点击超链接时打开外部浏览器。-(void)viewDidAppe... 查看详情

UIAlertView 已弃用如何将此代码修复为 UIAlertController?我不知道如何在 UIAlertController 中使用 switch 语句

】UIAlertView已弃用如何将此代码修复为UIAlertController?我不知道如何在UIAlertController中使用switch语句【英文标题】:UIAlertViewisDeprecatedhowcanifixthiscodeasUIAlertController?Ican\'tfigureouthowtouseswitchstatementinUIAlertController【发布时间】:2016-07-22 查看详情