UI第一个出画面的代码

来源:互联网 发布:易语言监听端口数据 编辑:程序博客网 时间:2024/04/30 09:29
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds] ] autorelease];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
   
    UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1000, 1000)];
    self.window.backgroundColor = [UIColor grayColor];
    [self.window addSubview:aView];
    
    UITextField *firstNumber = [[UITextField alloc] initWithFrame:CGRectMake(20, 40, 50, 30)];
    firstNumber.borderStyle = UITextBorderStyleRoundedRect;//输入框的样式
    firstNumber.keyboardType =  UIKeyboardTypeNumberPad;//弹出键盘样式
    firstNumber.tag = 100;
    [self.window addSubview:firstNumber];
    [firstNumber release];
    
    UILabel *plusLable = [[UILabel alloc] initWithFrame:CGRectMake(80, 40, 20, 30)];
    plusLable.text = @"+"; //所显示的文本内容
    plusLable.textAlignment = NSTextAlignmentCenter; //所显示的文本内容位置
    [self.window addSubview:plusLable];
    [plusLable release];
    
    UITextField *secondNumber = [[UITextField alloc] initWithFrame:CGRectMake(110, 40, 50, 30)];
    secondNumber.borderStyle = UITextBorderStyleRoundedRect;
    secondNumber.keyboardType = UIKeyboardTypeNumberPad;
    secondNumber.tag = 200;
    [self.window addSubview:secondNumber];
    [secondNumber release];
    
    UILabel *resultLabel = [[UILabel alloc] initWithFrame:CGRectMake(230, 40, 30, 30)];
    resultLabel.textAlignment = NSTextAlignmentLeft;
    resultLabel.tag = 300;
    [self.window addSubview:resultLabel];
    [resultLabel release];
    
    UIButton *caulatorButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    caulatorButton.frame = CGRectMake(170, 40, 50, 30);
    [caulatorButton setTitle:@" = " forState:UIControlStateNormal];
    [caulatorButton addTarget:self action:@selector(plus:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:caulatorButton];
    
    
    return YES;
}
- (void)plus:(UIButton *)button
{
    UITextField *firstTf = (UITextField *)[self.window viewWithTag:100];
    UITextField *secondTf = (UITextField *)[self.window viewWithTag:200];
    NSString *firstNumberStr = firstTf.text;
    NSString *secondNumberStr = secondTf.text;
    int result = firstNumberStr.intValue + secondNumberStr.intValue;
    UILabel *resultLable = (UILabel *)[self.window viewWithTag:300];
    resultLable.text = [NSString stringWithFormat:@"%d",result];
    
}
    

0 0
原创粉丝点击