一:
在界面上拖入相应的控件
二: 给每个控件设置关联
//监听按钮的点击
- (IBAction)compute:(id)sender;
//第一个文本输入框的值
@property (weak, nonatomic) IBOutlet UITextField *numField1;
@property (weak, nonatomic) IBOutlet UITextField *numField2;
@property (weak, nonatomic) IBOutlet UILabel *totalLabel;
三: 处理按钮点击事件
- (IBAction)compute:(id)sender {
//1 取得两个文本输入框的值
NSString *text1 = self.numField1.text;
NSString *text2 = self.numField2.text;
//2 相减
int deffernece = 0;
int num1 = [text1 intValue];
int num2 = [text2 intValue];
if(num1 > num2 || num1 == num2){
deffernece = num1 - num2;
}else{
//弹框
//创建一个弹框
//initWithTitle 标题
//message 详细信息
//delegate 代理
//cancelButtonTitle 取消按钮的文字
//otherButtonTitles 其他按钮的文字
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"输入的数字不合理" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
//2.2显示
[alert show];
}
//3 把结果显示在右边的文本标签中
_totalLabel.text = [NSString stringWithFormat:@"%d",deffernece];
[self.view endEditing:YES];
}