iPhone 点击计数器

发布于 2024-09-26 14:21:05 字数 945 浏览 6 评论 0原文

我正在尝试让 UI 按钮通过 Xcode 和 Interface Builder 与 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++;

            }

I'm trying to get a UI Button to interact with a UI Label through Xcode and Interface Builder. What should I change in this code to do so? (I have everything linked up in Interface Builder already. The app just crashes when I press the button.)

@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++;

            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

送君千里 2024-10-03 14:21:05

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

static int count;
count++;
NSString *countString = [NSString stringWithFormat:@"%d", count];
[anotherLabel setText: countString];

Ok, first off, your not updating the label this way, since just passing anotherLabel as a parameter and that will not mean that it will change the properties of it. Also, you are not passing the correct parameter for doButton:. In this case I would just forget a parameter and up date the anotherLabel by saying:

static int count;
count++;
NSString *countString = [NSString stringWithFormat:@"%d", count];
[anotherLabel setText: countString];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文