iPhone Tap Counter

时间:2010-10-09 05:02:11

标签: iphone xcode interface uilabel

我正在尝试通过Xcode和Interface Builder获取UI按钮与UI标签进行交互。我应该在此代码中更改哪些内容? (我已经在Interface Builder中链接了所有内容。当我按下按钮时应用程序崩溃了。)

@synthesize window;
@synthesize label;
@synthesize anotherLabel;
@synthesize myButton;


    #pragma mark -
    #pragma mark Application lifecycle



    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

     [myButton setTitle:@"Press Here" forState:UIControlStateNormal];

     window.backgroundColor = [UIColor grayColor];

     label.text = [[NSDate date] description];


        // Override point for customization after application launch.

     [myButton addTarget:anotherLabel action:@selector(doButton:) forControlEvents:UIControlEventTouchUpInside];

        [window makeKeyAndVisible];

     return YES;
    }

     -(void) doButton:(UILabel *)anotherLabel{
     static int count;
        count++;

            }

1 个答案:

答案 0 :(得分:2)

好的,首先,你没有这样更新标签,因为只是将另一个标签作为参数传递,这并不意味着它会改变它的属性。另外,您没有为doButton传递正确的参数:在这种情况下,我会忘记一个参数,并通过说:

来更新另一个标签
static int count;
count++;
NSString *countString = [NSString stringWithFormat:@"%d", count];
[anotherLabel setText: countString];