草庐IT

ios - 子类化 UIAlertController 并遇到运行时错误

coder 2023-09-07 原文

我是 iOS 和 OSX 编程的新手,决定从 Swift 开始,使用 iOS8 API 测试应用程序,并尝试一些我在其他环境中使用的编程技术。但是,我遇到了一种奇怪的情况,希望有人能识别并帮助我。这涉及子类化 UIAlertController 的困难。

这是我的第一次尝试:

import UIKit

class FubarAlertController: UIAlertController {

    convenience init (message:String) {
        self.init(title: "Alert", message: message, preferredStyle: UIAlertControllerStyle.Alert);
        self.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
    }
}

但是,我在 Xcode 中收到以下警告,我不明白这些警告 - 在我看来,这些错误在我看来是自相矛盾的(请原谅我的双关语)

use of self in delegating initializer before self.init is called
Self.init is't called on all paths in delegating initialiser

然后我尝试了

class FubarAlertController: UIAlertController {

    convenience init (message:String) {
        self.init();
        self.title = "Alert";
        self.message = message;
        self.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
    }
}

并且没有遇到任何编译时错误。但是,当我像这样从一个简单的 ViewController 中使用它时

class FubarController: UIViewController {

    // method invoked when a UIBarButtonItem action takes place 

    @IBAction
    func enterTextButtonAction(sender: UIBarButtonItem) {

        let controller = FubarAlertController(message: "Fubar!");
        presentViewController(controller, animated: true, completion: nil);
    }   
}

我再次收到以下运行时错误,我不太明白

*** Terminating app due to uncaught exception 'NSGenericException', 
reason: 'Your application has presented a UIAlertController
(<UiToolKit.TextChangedAlertController: 0x7adf2340>) of style 
UIAlertControllerStyleActionSheet. The modalPresentationStyle of a 
UIAlertController with this style is UIModalPresentationPopover. You 
must provide location information for this popover through the alert 
controller's popoverPresentationController. You must provide either a 
sourceView and sourceRect or a barButtonItem.  If this information is
not known when you present the alert controller, you may provide it in the 
UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.'

但是,如果我简单地实现 FubarAlertController 而根本没有任何方法或属性,我可以像这样调用它,就好像它是界面构建器操作中的 UIAlertController 一样

class FubarController: UIViewController {

    // method invoked when a UIBarButtonItem action takes place 

    @IBAction
    func enterTextButtonAction(sender: UIBarButtonItem) {
        let controller = FubarAlertController(title: "Alert", message: "Fubar!", preferredStyle: UIAlertControllerStyle.Alert);
        controller.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
        presentViewController(controller, animated: true, completion: nil);
    }   
}   

...一切都按预期工作 - 没有编译时或运行时错误,但我不明白为什么!

所以我的问题是:

(i) 为什么我不能以我第一次尝试的方式实现便捷的 init 方法?我在这里缺少什么魔法知识?为什么我不能在便利初始化器的 self 上下文中调用父类(super class) init 方法?

(ii) 当我第二次实现 UIAlertController 的子类时,为什么会出现运行时错误而不是编译时错误?

非常感谢你走到这一步并期待一些反馈 - 我很难过!

最佳答案

我知道这是一个老问题,但我认为它至少可以使用部分答案。

(I) 我必须像您一样做才能使 UIAlertController 的子类正常工作。我认为简单的答案是“不要这样做”。根据他们的文档,Apple 在技术上不允许对 UIAlertController 进行子类化:

The UIAlertController class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/

(二) 在 iPad 上运行我的 iPhone 应用程序时,我遇到了同样的错误。

*** Terminating app due to uncaught exception 'NSGenericException', 
reason: 'Your application has presented a UIAlertController of style 
UIAlertControllerStyleActionSheet. The modalPresentationStyle of a 
UIAlertController with this style is UIModalPresentationPopover. You 
must provide location information for this popover through the alert 
controller's popoverPresentationController. You must provide either a 
sourceView and sourceRect or a barButtonItem.  If this information is
not known when you present the alert controller, you may provide it in the 
UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.'

由于 iPad 上的弹出窗口不是全屏,iPad 需要设置 sourceViewsourceRect 属性来确定弹出窗口应该在屏幕上的哪个位置显示。

每当您将样式设置为 .ActionSheet 时,将这样的代码添加到您的 UIAlertController 中:

//For iPad, check that there is a popover and set action button as the sender
if let popoverController = controller.popoverPresentationController {
        //Sender will be a UIButton, cast down to UIView
        popoverController.sourceView = sender as? UIView
        popoverController.sourceRect = sender.bounds
}

根据您的情况,您可能需要对此进行调整。我上面的示例来自 IBAction 函数,其中 sender 是一个 UIButton。

关于ios - 子类化 UIAlertController 并遇到运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26935703/

有关ios - 子类化 UIAlertController 并遇到运行时错误的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby-on-rails - Rails - 子类化模型的设计模式是什么? - 2

    我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

  3. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  4. ruby - 如何每月在 Heroku 运行一次 Scheduler 插件? - 2

    在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/

  5. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  6. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  7. ruby - 通过 RVM (OSX Mountain Lion) 安装 Ruby 2.0.0-p247 时遇到问题 - 2

    我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search

  8. ruby - Sinatra:运行 rspec 测试时记录噪音 - 2

    Sinatra新手;我正在运行一些rspec测试,但在日志中收到了一堆不需要的噪音。如何消除日志中过多的噪音?我仔细检查了环境是否设置为:test,这意味着记录器级别应设置为WARN而不是DEBUG。spec_helper:require"./app"require"sinatra"require"rspec"require"rack/test"require"database_cleaner"require"factory_girl"set:environment,:testFactoryGirl.definition_file_paths=%w{./factories./test/

  9. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  10. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

随机推荐