草庐IT

ios - 将标题分配给 UIViewController 时发生了一些奇怪的事情

coder 2023-09-11 原文

我有一些 viewController,每个 viewController 都由 segues 连接。 rootViewController 连接到 NavigationController。现在,在一个 UIViewController 中,我正在执行一个 segue 以转到另一个 UIViewController,并且它有效:

self.performSegueWithIdentifier("toLoginTypeActivity", sender: self)

在执行 segues 之后我被带到的 UIViewController 是这样的:

import Foundation
import UIKit

class LoginTypeActivityViewController: UIViewController{

    override func viewDidLoad() {
        super.viewDidLoad()
        println("I am here.")
        //self.title = "Namaskar"         // Here seems to be something strange.
        println("I am here also.")
    }
}

但是,如果我在代码中注释掉 self.title(标记为奇怪),segue 将不起作用。虽然,所有 println 都在工作。甚至,“我也在这里”。被打印出来。但是,segues 没有得到执行。它给出运行时错误。

应该是什么问题,应该如何解决?我确定我已正确命名标识符。

这是运行时错误:

2014-10-13 17:21:27.878 mobilepay[29387:1382271] -[Swift._NSContiguousString set]: unrecognized selector sent to instance 0x7b6b33d0
2014-10-13 17:21:27.881 mobilepay[29387:1382271] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Swift._NSContiguousString set]: unrecognized selector sent to instance 0x7b6b33d0'
*** First throw call stack:
(
0   CoreFoundation                      0x00bb5df6 __exceptionPreprocess + 182
1   libobjc.A.dylib                     0x0083fa97 objc_exception_throw + 44
2   CoreFoundation                      0x00bbda75 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
3   CoreFoundation                      0x00b069c7 ___forwarding___ + 1047
4   CoreFoundation                      0x00b0658e _CF_forwarding_prep_0 + 14
5   UIFoundation                        0x067eeb91 __NSStringDrawingEngine + 29221
6   UIFoundation                        0x067e784d -[NSString(NSExtendedStringDrawing) drawWithRect:options:attributes:context:] + 171
7   UIKit                               0x013be151 -[UILabel _drawTextInRect:baselineCalculationOnly:] + 6626
8   UIKit                               0x013bbe30 -[UILabel drawTextInRect:] + 581
9   UIKit                               0x013be256 -[UILabel drawRect:] + 98
10  UIKit                               0x0123354b -[UIView(CALayerDelegate) drawLayer:inContext:] + 519
11  QuartzCore                          0x01077d51 -[CALayer drawInContext:] + 118
12  QuartzCore                          0x01077c87 _ZL16backing_callbackP9CGContextPv + 96
13  QuartzCore                          0x00f5c7ae CABackingStoreUpdate_ + 2788
14  QuartzCore                          0x01077c1f ___ZN2CA5Layer8display_Ev_block_invoke + 93
15  QuartzCore                          0x010ad406 x_blame_allocations + 15
16  QuartzCore                          0x01077a85 _ZN2CA5Layer8display_Ev + 1591
17  QuartzCore                          0x01077cd6 -[CALayer _display] + 33
18  QuartzCore                          0x01077446 _ZN2CA5Layer7displayEv + 142
19  QuartzCore                          0x01077cb0 -[CALayer display] + 33
20  QuartzCore                          0x0106bee6 _ZN2CA5Layer17display_if_neededEPNS_11TransactionE + 322
21  QuartzCore                          0x0106bf6c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 38
22  QuartzCore                          0x00fca676 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 284
23  QuartzCore                          0x00fcba3c _ZN2CA11Transaction6commitEv + 392
24  QuartzCore                          0x00fcc108 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
25  CoreFoundation                      0x00ad8fbe __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
26  CoreFoundation                      0x00ad8f00 __CFRunLoopDoObservers + 400
27  CoreFoundation                      0x00ace93a __CFRunLoopRun + 1226
28  CoreFoundation                      0x00ace1ab CFRunLoopRunSpecific + 443
29  CoreFoundation                      0x00acdfdb CFRunLoopRunInMode + 123
30  GraphicsServices                    0x0317124f GSEventRunModal + 192
31  GraphicsServices                    0x0317108c GSEventRun + 104
32  UIKit                               0x011a8e16 UIApplicationMain + 1526
33  mobilepay                           0x001410de top_level_code + 78
34  mobilepay                           0x0014111b main + 43
35  libdyld.dylib                       0x03ad7ac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

请注意,分配给 self.title 可以在其他 ViewController 中使用。

编辑:

这是更新后的代码。

class LoginTypeActivityViewController: UIViewController{

    var delegate = UIApplication.sharedApplication().delegate as AppDelegate

    override func viewDidLoad() {
        super.viewDidLoad()
        println("viewDidLoad self.title = \(self.title)")
        println("viewDidLoad self.navigationItem.title = \(self.navigationItem.title)")
    }

    override func viewWillAppear(animated: Bool) {
        println("viewWillAppear self.title = \(self.title)")
        println("viewWillAppear self.navigationItem.title = \(self.navigationItem.title)")
    }

    override func viewDidAppear(animated: Bool) {
        println("viewDidAppear self.title = \(self.title)")
        println("viewDidAppear self.navigationItem.title = \(self.navigationItem.title)")
        self.navigationItem.title = "Koshish"
        println("printing in viewDidAppear after setting self.navigatioItem.title = \(self.navigationItem.title)")
}

这是输出:

viewDidLoad self.title = nil
viewDidLoad self.navigationItem.title = nil
viewWillAppear self.title = nil
viewWillAppear self.navigationItem.title = nil
viewDidAppear self.title = nil
viewDidAppear self.navigationItem.title = nil
printing in viewDidAppear after setting self.navigatioItem.title = Optional("Koshish")
2014-10-14 12:46:48.931 mobilepay[34547:1593230] -[Swift._NSContiguousString set]: unrecognized selector sent to instance 0x7a162090
2014-10-14 12:46:48.933 mobilepay[34547:1593230] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Swift._NSContiguousString set]: unrecognized selector sent to instance 0x7a162090'
*** First throw call stack:
(
0   CoreFoundation                      0x00bd3df6 __exceptionPreprocess + 182
1   libobjc.A.dylib                     0x0085da97 objc_exception_throw + 44
2   CoreFoundation                      0x00bdba75 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
3   CoreFoundation                      0x00b249c7 ___forwarding___ + 1047
4   CoreFoundation                      0x00b2458e _CF_forwarding_prep_0 + 14
5   UIFoundation                        0x0680cb91 __NSStringDrawingEngine + 29221
6   UIFoundation                        0x0680584d -[NSString(NSExtendedStringDrawing) drawWithRect:options:attributes:context:] + 171
7   UIKit                               0x013dc151 -[UILabel _drawTextInRect:baselineCalculationOnly:] + 6626
8   UIKit                               0x013d9e30 -[UILabel drawTextInRect:] + 581
9   UIKit                               0x013dc256 -[UILabel drawRect:] + 98
10  UIKit                               0x0125154b -[UIView(CALayerDelegate) drawLayer:inContext:] + 519
11  QuartzCore                          0x01095d51 -[CALayer drawInContext:] + 118
12  QuartzCore                          0x01095c87 _ZL16backing_callbackP9CGContextPv + 96
13  QuartzCore                          0x00f7a7ae CABackingStoreUpdate_ + 2788
14  QuartzCore                          0x01095c1f ___ZN2CA5Layer8display_Ev_block_invoke + 93
15  QuartzCore                          0x010cb406 x_blame_allocations + 15
16  QuartzCore                          0x01095a85 _ZN2CA5Layer8display_Ev + 1591
17  QuartzCore                          0x01095cd6 -[CALayer _display] + 33
18  QuartzCore                          0x01095446 _ZN2CA5Layer7displayEv + 142
19  QuartzCore                          0x01095cb0 -[CALayer display] + 33
20  QuartzCore                          0x01089ee6 _ZN2CA5Layer17display_if_neededEPNS_11TransactionE + 322
21  QuartzCore                          0x01089f6c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 38
22  QuartzCore                          0x00fe8676 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 284
23  QuartzCore                          0x00fe9a3c _ZN2CA11Transaction6commitEv + 392
24  QuartzCore                          0x00fea108 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
25  CoreFoundation                      0x00af6fbe __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
26  CoreFoundation                      0x00af6f00 __CFRunLoopDoObservers + 400
27  CoreFoundation                      0x00aec93a __CFRunLoopRun + 1226
28  CoreFoundation                      0x00aec1ab CFRunLoopRunSpecific + 443
29  CoreFoundation                      0x00aebfdb CFRunLoopRunInMode + 123
30  GraphicsServices                    0x0318f24f GSEventRunModal + 192
31  GraphicsServices                    0x0318f08c GSEventRun + 104
32  UIKit                               0x011c6e16 UIApplicationMain + 1526
33  mobilepay                           0x00160f5e top_level_code + 78
34  mobilepay                           0x00160f9b main + 43
35  libdyld.dylib                       0x03af5ac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

已解决

我终于在我的代码中找到了问题:

在我的 didFinishLaunchingWithOptions 中,我这样做了:

var navigationBarAppearance = UINavigationBar.appearance()
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent
navigationBarAppearance.tintColor = UIColor(rgba: "#000000", alpha: 1.0)
navigationBarAppearance.barTintColor = UIColor(rgba: "#000000", alpha: 1.0)
navigationBarAppearance.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Arial-BoldMT", size: 20.0), NSForegroundColorAttributeName: "#000000", alpha: 1.0)]

如您所见,在 NSForegroundColorAttributeName 中,我只是设置了字符串,并没有用它调用 UIColor 函数。

我觉得 Swift 应该给我一些提示,告诉我我没有用正确的值调用 NSForegroundColorAttribute。

最佳答案

我无法给你答案,但可以就如何解决它提供建议。您的 UINavigationItem 的 title 属性以某种方式设置为某个对象而不是 NSString。

因此,在“//self.title”行的位置,记录 title 的值、类的 UINavigationItem 标题的值,如果其中一个不为 nil,则记录类。我猜它不知何故被设置为一个数字而不是一个字符串,但它可能只是垃圾或释放的东西。或者,您的 Storyboard文件可能已损坏。

编辑:所以这里有些东西真的很奇怪。其他想法:

  • 如果此 segue 在 vi​​ewDidLoad 之前由呈现 View 触发,则可能会发生奇怪的事情。

  • 在调用类中实现“override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)”,然后验证您是否在主线程上

  • 使用调试器,添加一个异常断点,当您遇到崩溃时,确认崩溃发生在线程 0 上。此外,四处寻找 - 有一个对象不响应选择器,您有它的地址,所以在调试器中执行“po 0x.......”,看看它是什么。

  • 如果做不到这一点,您将需要花一些时间并尝试创建一个以您的真实项目为模型的演示项目,但它存在同样的问题。如果你能做到这一点,你可以将它上传到像 Dropbox 这样的公共(public)网站,更新你的问题,像我这样的人会看到它(我至少会这样做)。传统上,如果你到了这一步就可以提供一些赏金,我只是这么说,所以你知道,但我会在没有赏金的情况下看看它(我真的不需要更多的积分)。

关于ios - 将标题分配给 UIViewController 时发生了一些奇怪的事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26340845/

有关ios - 将标题分配给 UIViewController 时发生了一些奇怪的事情的更多相关文章

  1. ruby-on-rails - 使用 Rmagick 或 ImageMagick 在背景上放置标题 - 2

    我有一张背景图片,我想在其中添加一个文本框。我想弄清楚如何将标题放置在其顶部的正确位置。(我使用标题是因为我需要自动换行功能)。现在,我只能让文本显示在左上角,但我需要能够手动定位它的开始位置。require'RMagick'require'Pry'includeMagicktext="Loremipsumdolorsitamet"img=ImageList.new('template001.jpg')img 最佳答案 这是使用convert的ImageMagick命令行的答案。如果你想在Rmagick中使用这个方法,你必须自己移植

  2. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  3. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  4. ruby-on-rails - 如何生成传递一些自定义参数的 `link_to` URL? - 2

    我正在使用RubyonRails3.0.9,我想生成一个传递一些自定义参数的link_toURL。也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:link_to'Samplelinktitle',...#HereIshouldimplementthecode#=>'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...我如何编写link_to语句“alàRubyonRailsWay”以实现该目的?如果我想通过传递一些

  5. ruby-on-rails - 启用 Rack::Deflater 时 ETag 发生变化 - 2

    在启用Rack::Deflater来gzip我的响应主体时偶然发现了一些奇怪的东西。也许我遗漏了一些东西,但启用此功能后,响应被压缩,但是资源的ETag在每个请求上都会发生变化。这会强制应用程序每次都响应,而不是发送304。这在没有启用Rack::Deflater的情况下有效,我已经验证页面源没有改变。我正在运行一个使用thin作为Web服务器的Rails应用程序。Gemfile.lockhttps://gist.github.com/2510816有没有什么方法可以让我从Rack中间件获得更多的输出,这样我就可以看到发生了什么?提前致谢。 最佳答案

  6. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  7. ruby - 当 attr_accessor 在类方法中时会发生什么? - 2

    所以我想到了这个,想知道当下面的一些事情完成后会发生什么。classTestdefself.abcattr_accessor:Johnendendobject=Test.newputs"beforecallingclassmethodabc:#{object.class.instance_methods(false)}"Test.abcputs"aftercallingclassmethodabc:#{object.class.instance_methods(false)}"这里我检查的是,getter和setter方法是否以这种方式创建。如果是这样,是那些实例方法或类方法。首先我创

  8. ruby - 找一些句子 - 2

    我想找到在某些文本中找到一些(让它是两个)句子的好方法。什么会更好-使用正则表达式或拆分方法?你的想法?应JeremyStein的要求-有一些例子示例:输入:ThefirstthingtodoistocreatetheCommentmodel.We’llcreatethisinthenormalway,butwithonesmalldifference.IfwewerejustcreatingcommentsforanArticlewe’dhaveanintegerfieldcalledarticle_idinthemodeltostoretheforeignkey,butinthis

  9. ruby - 将全局 $stdout 重新分配给控制台 - ruby - 2

    我正在尝试将$stdout设置为临时写入一个文件,然后返回到一个文件。test.rb:old_stdout=$stdout$stdout.reopen("mytestfile.out",'w+')puts"thisgoesinmytestfile"$stdout=old_stdoutputs"thisshouldbeontheconsole"$stdout.reopen("mytestfile1.out",'w+')puts"thisgoesinmytestfile1:"$stdout=old_stdoutputs"thisshouldbebackontheconsole"这是输出。r

  10. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

随机推荐