为什么不是';我的方法不完全有效吗?(iPhone SDK)
我的代码中有一个方法:为什么不是';我的方法不完全有效吗?(iPhone SDK),iphone,sdk,console,uibutton,nslog,Iphone,Sdk,Console,Uibutton,Nslog,我的代码中有一个方法: -(IBAction) actionButtonPressed: (id) sender{ NSLog(@"%@",actionButton.titleLabel.text); if (actionButton.titleLabel.text == @"Begin Recording"){ [actionButton setTitle:@"Finish Recording" forState:UIControlStateNormal];
-(IBAction) actionButtonPressed: (id) sender{
NSLog(@"%@",actionButton.titleLabel.text);
if (actionButton.titleLabel.text == @"Begin Recording"){
[actionButton setTitle:@"Finish Recording" forState:UIControlStateNormal];
[actionButton setTitle:@"Finish Recording" forState:UIControlStateApplication];
[actionButton setTitle:@"Finish Recording" forState:UIControlStateHighlighted];
[actionButton setTitle:@"Finish Recording" forState:UIControlStateReserved];
[actionButton setTitle:@"Finish Recording" forState:UIControlStateSelected];
[actionButton setTitle:@"Finish Recording" forState:UIControlStateDisabled];
UIImage *redButton = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"bigredbutton" ofType:@"png"]];
[actionButton setBackgroundImage:redButton forState:UIControlStateNormal];
[actionButton setBackgroundImage:redButton forState:UIControlStateApplication];
[actionButton setBackgroundImage:redButton forState:UIControlStateHighlighted];
[actionButton setBackgroundImage:redButton forState:UIControlStateReserved];
[actionButton setBackgroundImage:redButton forState:UIControlStateSelected];
[actionButton setBackgroundImage:redButton forState:UIControlStateDisabled];
}
if (actionButton.titleLabel.text == @"Finish Recording"){
[self dismissModalViewControllerAnimated:YES];
}
}
出于某种原因,NSLog调用确实起作用并显示在控制台上。该按钮开始读取开始录制,但按下该按钮不起任何作用,即使它与Interface Builder中的Touch up内部绑定以调用此方法并被引用。您的问题是这一行:
if (actionButton.titleLabel.text == @"Begin Recording"){
这是比较指针,而不是字符串。你需要:
if ([actionButton.titleLabel.text isEqualToString:@"Begin Recording"]){
您的问题是这一行:
if (actionButton.titleLabel.text == @"Begin Recording"){
这是比较指针,而不是字符串。你需要:
if ([actionButton.titleLabel.text isEqualToString:@"Begin Recording"]){
这基本上是正确的,但我认为应该使用isEqualToString,而不是isEqual。为了扩展Dave所说的内容,当比较常量字符串时,==运算符将只计算为true,这样使用它仍然是不好的形式。使用isEqualToString进行所有字符串比较。@Andrew-很公平。编辑答案。(虽然我在使用isEqual:时从来没有遇到过问题)是的,我认为isEqual会起作用,因为是你说的,但我以前从未使用过它。。。我不知道他们之间是否有任何区别。这基本上是正确的,但我认为应该使用isEqualToString,而不是isEqual。为了扩展Dave所说的内容,当比较常量字符串时,==运算符将只计算为true,这样使用它仍然是不好的形式。使用isEqualToString进行所有字符串比较。@Andrew-很公平。编辑答案。(虽然我在使用isEqual:时从来没有遇到过问题)是的,我认为isEqual会起作用,因为是你说的,但我以前从未使用过它。。。我不知道他们之间有什么不同。