iPhone 点击计数器
我正在尝试让 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,首先,您不以这种方式更新标签,因为只是将 anotherLabel 作为参数传递,这并不意味着它将更改它的属性。另外,您没有为 doButton: 传递正确的参数。在这种情况下,我会忘记一个参数并通过以下方式更新 anotherLabel:
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: