iPhone模拟器在第一次触摸后挂起

时间:2009-10-04 11:51:26

标签: iphone objective-c

点击任一按钮后,应用程序将停止响应任何触摸事件。 (Controller#increase / Controller#decrease方法)。

以下是启动时控制台的输出

[Session started at 2009-10-04 14:41:20 +0300.]
GNU gdb 6.3.50-20050815 (Apple version gdb-1344) (Fri Jul  3 01:19:56 UTC 2009)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".sharedlibrary apply-load-rules all
Attaching to process 1340.
Pending breakpoint 1 - ""Controller.m":46" resolved
2009-10-04 14:41:23.339 HelloPoly[1340:207] My polygon: Hello I am a 5-sided polygon (aka a Pentagon) with angles of 108 degrees (1.884956 radians).
(gdb) 

在调试模式下运行应用程序我发现确实有对相应方法的调用以及更新UI。所以该方法确实到达它的末尾并返回。

2009-10-04 14:42:25.723 HelloPoly[1340:207] sides increased to 6

然而,取消通话

[self updateInterface];

应用程序正常运行(例如触摸事件处理),但当然不符合规范:)。

程序代码如下

//Controller extends NSObject

#import "Controller.h"

@implementation Controller


- (void)awakeFromNib { // configure your polygon here
    polygon.minimumNumberOfSides = 3;
    polygon.maximumNumberOfSides = 12;
    polygon.numberOfSides = numberOfSidesLabel.text.integerValue;

    NSLog (@"My polygon: %@", polygon);
}

- (void)updateDecreaseButton{
    decreaseButton.enabled = [polygon hasMinimumSides];
}

- (void)updateIncreaseButton{
    increaseButton.enabled = [polygon hasMaximumSides];
}

- (void)updateNumberOfSidesLabel{
    numberOfSidesLabel.text = [NSString stringWithFormat:@"%i", polygon.numberOfSides];
}

- (void)updateInterface {
    [self updateDecreaseButton];
    [self updateIncreaseButton];
    [self updateNumberOfSidesLabel];
}

- (IBAction)decrease:(id)sender {
    NSLog(@"sides decreased to %i", [polygon decreaseSides]);
    [self updateInterface];
}

- (IBAction)increase:(id)sender {
    NSLog(@"sides increased to %i", [polygon increaseSides]);
    [self updateInterface];
}
@end

2 个答案:

答案 0 :(得分:1)

正如另一个论坛的成员指出的那样,问题实际上是一个“逻辑”问题。

似乎按钮(增加/减少)实际上已被禁用。在第一次“触摸”之后。

e.g。多边形没有到达其最大边,因此它应该 NOT (!)缺失

- (void)updateDecreaseButton{
    decreaseButton.enabled = ![polygon hasMinimumSides];
}

- (void)updateIncreaseButton{
    increaseButton.enabled = ![polygon hasMaximumSides];
}

我错过了它的原因是默认情况下,UIButton如果禁用则没有不同的样式。我的建议是,总是为UIButton的每个状态指定一个自定义样式,以避免混淆,如此。

您可以在Interface Builder中执行此操作。选择UIButton小部件后,选择工具 - >属性检查员。

答案 1 :(得分:0)

我不是100%肯定,但更新接口函数是否每秒被称为x次?如果不是这样就不必在每次触摸事件后告诉接口更新?如果删除两个[self updateInterface],应用程序是否按预期运行;调用事件处理程序?