之前我问过这个问题,但由于某种原因,它说是关闭的,但我从来没有得到正确的答案,任何人都可以帮我添加一个文本字段,如果用户在文本字段中输入数字,一旦数字达到相同的数字作为计数器,它会显示警报。
#import <UIKit/UIKit.h>
int NumberCount;
@interface ViewController : UIViewController
{
int counter;
IBOutlet UILabel *count;
}
-(IBAction)minus;
-(IBAction)plus;
-(IBAction)zero;
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
-(IBAction)plus {
counter=counter + 1;
count.text = [NSString stringWithFormat:@"%i",counter];
}
-(IBAction)minus {
counter=counter - 1;
count.text = [NSString stringWithFormat:@"%i",counter];
}
-(IBAction)zero {
counter=0;
count.text = [NSString stringWithFormat:@"%i",counter];
}
- (void)viewDidLoad {
counter=0;
count.text = @"0";
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
我尝试添加它,但它没有工作
if (count.text == textField ) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Congrats"
message:@"You met that number"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
我的ViewController.h:
__weak IBOutlet UITextField *textField;
答案 0 :(得分:1)
您正在将字符串与UITextField对象进行比较。 您必须将count.text字符串与textField.text字符串进行比较。
试试这个:
if ([count.text isEqualToString:textField.text]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Congrats"
message:@"You met that number"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}