02

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColor whiteColor];

    

    //创建一个承载视图,作为画纸

    UIView *containerView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    containerView.backgroundColor = [UIColor whiteColor];

    [self.window addSubview:containerView];

    [containerView release];

    

    //第一个输入框

    _firstTf = [self createTextFieldWithFrame:CGRectMake(20, 90, 100, 30) borderStyle:UITextBorderStyleRoundedRect placeholder:@"第一个数" keyboardType:UIKeyboardTypeNumberPad secureTextEntry:NO];

    [containerView addSubview:_firstTf];

    

    //加号的label

    UILabel *addLabel = [self createLabelWithFrame:CGRectMake(130, 95, 20, 20) text:@"+" textAlignment:NSTextAlignmentCenter textColor:[UIColor blackColor] numberOfLines:0 font:[UIFont boldSystemFontOfSize:14]];

    [containerView addSubview:addLabel];

    

    //第二个输入框

    _secondTf = [self createTextFieldWithFrame:CGRectMake(160, 90, 100, 30) borderStyle:UITextBorderStyleRoundedRect placeholder:@"第二个数" keyboardType:UIKeyboardTypeNumberPad secureTextEntry:NO];

    [containerView addSubview:_secondTf];

    

    //等号label

    UILabel *equalLabel = [self createLabelWithFrame:CGRectMake(270, 95, 20, 20) text:@"=" textAlignment:NSTextAlignmentCenter textColor:[UIColor blackColor] numberOfLines:0 font:[UIFont boldSystemFontOfSize:14]];

    [containerView addSubview:equalLabel];

    

    //显示结果的label

    _resultLabel = [self createLabelWithFrame:CGRectMake(300, 90, 60, 30) text:@"结果" textAlignment:NSTextAlignmentCenter textColor:[UIColor redColor] numberOfLines:0 font:[UIFont boldSystemFontOfSize:18]];

    [containerView addSubview:_resultLabel];

    

    //计算的按钮

    UIButton *countButton = [self createButtonWithFrame:CGRectMake(100, 150, 200, 30) title:@"计算" titleColor:[UIColor purpleColor] backgroundImage:nil action:@selector(add)];

    [containerView addSubview:countButton];

    

    

    [self.window makeKeyAndVisible];

    return YES;

}

 

- (void)add {

    int first = [self.firstTf.text intValue];

    int second = [self.secondTf.text intValue];

    int result = first + second;

    [self.firstTf resignFirstResponder];

    [self.secondTf resignFirstResponder];

    self.resultLabel.text = [NSString stringWithFormat:@"%d",result];

}

 

posted @ 2016-02-23 08:40  whwhll  阅读(124)  评论(0)    收藏  举报