UIButton 的行为确实很奇怪。崩溃并给出多个错误
我的应用程序现在遇到一些严重问题。好的,有两个“注册”按钮。上面的一项是“注册”,下面的一项是“登录”。因此,如果我填写所有字段并按“注册”按钮(上面的按钮),那么它将带我到故事板图像中最右侧的视图(查看提供的图像),该视图将由以下命令调用
ConfirmationScreenController *csc = [self.storyboard instantiateViewControllerWithIdentifier:@"Confirmation Screen"];
[self.view addSubview:csc.view];
:这很好用。视图显示了,但是如果我单击底部的“登录”按钮,应用程序就会崩溃。该按钮调用此方法:
-(IBAction)logIn:(id)sender {
NSLog(@"hello");}
我认为我正确地连接了所有内容。奇怪的部分是:当我重置内容和设置并再次运行它时,它会记录“hello”一两次,但是当我再次单击它时,它会再次崩溃并显示(gdb)
。或者有一次它说我在 __NSCFTimer 上调用了该方法,即使我从未设置过任何计时器,甚至从未向计时器发送任何内容......这到底是什么?
MainViewController 上的所有按钮都工作正常。
顺便说一下,构建或运行时没有任何警告。
这是回溯:
#0 0x0183709b in objc_msgSend ()
#1 0x00024570 in -[UIApplication sendAction:to:from:forEvent:] ()
#2 0x0002455a in -[UIApplication sendAction:toTarget:fromSender:forEvent:] ()
#3 0x000c9b76 in -[UIControl sendAction:to:forEvent:] ()
#4 0x000ca03f in -[UIControl(Internal) _sendActionsForEvents:withEvent:] ()
#5 0x000c92fe in -[UIControl touchesEnded:withEvent:] ()
#6 0x00049a30 in -[UIWindow _sendTouchesForEvent:] ()
#7 0x00049c56 in -[UIWindow sendEvent:] ()
#8 0x00030384 in -[UIApplication sendEvent:] ()
#9 0x00023aa9 in _UIApplicationHandleEvent ()
#10 0x0157dfa9 in PurpleEventCallback ()
#11 0x016671c5 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ ()
#12 0x015cc022 in __CFRunLoopDoSource1 ()
#13 0x015ca90a in __CFRunLoopRun ()
#14 0x015c9db4 in CFRunLoopRunSpecific ()
#15 0x015c9ccb in CFRunLoopRunInMode ()
#16 0x0157c879 in GSEventRunModal ()
#17 0x0157c93e in GSEventRun ()
#18 0x00021a9b in UIApplicationMain ()
#19 0x000026f8 in main (argc=1, argv=0xbffff65c) at /Users/Martin/Desktop/MyApplication/MyApplication/main.m:16
Current language: auto; currently objective-c
I have some serious problems with my app right now. Okay, so there are two "sign up" buttons. The upper one is "sign up" and the one below is "sign in". So if I fill out all the fields and press the "sign up" button (the upper one), then it will take me to the most right view in my storyboard image (look at the image provided) which will be called by:
ConfirmationScreenController *csc = [self.storyboard instantiateViewControllerWithIdentifier:@"Confirmation Screen"];
[self.view addSubview:csc.view];
and this works fine. The view shows up, but if i click the "login" button on the bottom the application crashes. The button calls this method:
-(IBAction)logIn:(id)sender {
NSLog(@"hello");}
I hooked everything up correctly I think. The strange part is : When I reset the content and settings, and run it again it will log "hello" once or twice, but when I click it once again it crashes again with (gdb)
. Or one time it said I called the method on __NSCFTimer
, even though I have never set up any timer or even sent anything to a timer ... What the hell is this ?
All the buttons on the MainViewController work fine.
By the way, there are no warnings when building or at runtime.
This is the backtrace:
#0 0x0183709b in objc_msgSend ()
#1 0x00024570 in -[UIApplication sendAction:to:from:forEvent:] ()
#2 0x0002455a in -[UIApplication sendAction:toTarget:fromSender:forEvent:] ()
#3 0x000c9b76 in -[UIControl sendAction:to:forEvent:] ()
#4 0x000ca03f in -[UIControl(Internal) _sendActionsForEvents:withEvent:] ()
#5 0x000c92fe in -[UIControl touchesEnded:withEvent:] ()
#6 0x00049a30 in -[UIWindow _sendTouchesForEvent:] ()
#7 0x00049c56 in -[UIWindow sendEvent:] ()
#8 0x00030384 in -[UIApplication sendEvent:] ()
#9 0x00023aa9 in _UIApplicationHandleEvent ()
#10 0x0157dfa9 in PurpleEventCallback ()
#11 0x016671c5 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ ()
#12 0x015cc022 in __CFRunLoopDoSource1 ()
#13 0x015ca90a in __CFRunLoopRun ()
#14 0x015c9db4 in CFRunLoopRunSpecific ()
#15 0x015c9ccb in CFRunLoopRunInMode ()
#16 0x0157c879 in GSEventRunModal ()
#17 0x0157c93e in GSEventRun ()
#18 0x00021a9b in UIApplicationMain ()
#19 0x000026f8 in main (argc=1, argv=0xbffff65c) at /Users/Martin/Desktop/MyApplication/MyApplication/main.m:16
Current language: auto; currently objective-c
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我遇到了同样的问题,这是因为我没有对正在创建和呈现的控制器进行强引用 - 然后调用 IBAction。
本质上,控制器被创建并显示,但此后没有人引用它,因此它的引用计数为零并且内存不足。因此调用操作将导致
EXC_BAD_ACCESS
。I had this same issue, and it was because I didn't have a strong reference to the controller being created and presented - which the
IBAction
was then being called on.Essentially, the controller is created and displayed but no one has a reference to it after that so its reference count goes to zero and it goes out of memory. So calling an action will result in an
EXC_BAD_ACCESS
.也许异常没有被任何人捕获并在堆栈中向上移动,直到它无法进一步前进为止。
确保您能够了解异常发生位置的一种方法是为所有异常插入断点,并在 THROW 上插入断点,这将停止问题发生的位置以及异常开始的位置。
这可能有助于追踪问题发生的位置。
It maybe that the exception is not being caught by anyone and traveling up the stack until it cannot go further.
One way to make sure you can get a clue of where the exception is occurring is to insert a Breakpoint for ALL exceptions and on THROW That will stop where the problem occurs and to where the exception started.
This may help in tracking down where the issue is occurring.
先尝试一下 -
在用于显示确认屏幕的代码中,您只需将其视图添加到当前视图控制器中,而不是实际以模态方式呈现它。按如下方式更改之前的代码:
更改
为
这样,新的视图控制器将接管对接收到的操作的响应并正确处理它们。如果这不起作用,还有一些建议:
可以尝试的一件事是重命名 IBAction 方法,然后在情节提要中断开它的连接并重新连接。另外,请确保
.h
文件中的方法定义与.m
文件中的方法完全相同。例如,与
如果您在其中一个中包含
(id)sender
部分,请确保在另一个中也包含它。如果这些都不起作用,我就会退后并弃踢。转到故事板并断开所有挂钩并重新连接它们:
IBOutlet
和IBAction
。TRY THIS FIRST -
In your code for presenting the confirmation screen, you simply added its view to the current viewcontroller instead of actually presenting it modally. Alter your previous code as follows:
Change
to
This way, the new viewcontroller will take over responding to received actions and handle them correctly. If this doesn't work, some more suggestions follow:
One thing to try is renaming the
IBAction
method, then disconnecting it in the storyboard and reconnecting it. Also, make sure the method definition in your.h
file is exactly the same as the method in your.m
file. For instance,is not the same as
If you have the
(id)sender
part in one, make sure you have it in the other as well.If none of this works, I would drop back and punt. Go to your storyboard and disconnect all your hooks and reconnect them:
IBOutlet
s andIBAction
s.您需要将ConfirmationScreenController 添加为头文件中的属性。我有同样的问题。但是将子视图控制器添加为头文件中的属性将防止崩溃。
You need to add ConfirmationScreenController as the property in your header file. I had the same issue. But adding your subview controller as the property in your header file will prevent the crash.