刚刚开始学习iphone的东西,boss说要用纯代码写界面,那个纠结!
平台:xcode4.1
工程:iphone viewbased
在loadview中写
- (void) loadView{
[super loadView];
UIView *contentView = [[UIView alloc]initWithFrame: [[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor lightGrayColor];
self.view = contentView;
[contentView release];
//label
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0.0, 0.0, 320, 30)];
label.text = @"PureCode";
label.center = contentView.center;
label.backgroundColor = [UIColor lightGrayColor];
label.textAlignment = UITextAlignmentCenter;
[self.view addSubview:label];
[label release];
//button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(10.0, 20.0, 77, 37)];
[button setTitle:@"Button" forState:UIControlStateNormal];
[button setTitle:@"ok" forState:UIControlStateDisabled];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
按钮事件,在viewcontroller.h中:
- (void) buttonPressed:(id)sender;
相应的在.m文件中: - (void)buttonPressed:(id)sender{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"ButtonPressed"
message:@"You have pressed the button"
delegate:nil
cancelButtonTitle:@"Yesh"
otherButtonTitles: nil];
[alert show];
}
上图:
可以增加其它控件:
//image
UIImage *image = [UIImage imageNamed:@"raw.png"];
UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
[imageView setFrame:CGRectMake(100, 100, 157, 57)];
[imageView sizeToFit];
imageView.backgroundColor = [UIColor blueColor];
[self.view addSubview:imageView];
[imageView release];