iphone开发-用代码显示控件

来源:互联网 发布:网络机柜 尺寸 编辑:程序博客网 时间:2024/06/02 02:23

之前一直都是用Xib文件拖控件学习,今天第一次用只用代码画界面,虽然只是很小的程序,这对于初学的我可是研究了一天呐!!!算是开了个头了!


.h 文件

#import <UIKit/UIKit.h>@interface RootViewController : UIViewController{    UIImageView *imageView;}@property (nonatomic,retain) UIImageView *imageView;@end

.m文件

////  RootViewController.m//  noxib////  Created by J plus on 7/31/12.//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.//#import "RootViewController.h"@interface RootViewController ()@end@implementation RootViewController@synthesize imageView;-(void)dealloc{    [imageView release];    [super dealloc];}- (void)viewDidLoad{    [super viewDidLoad];    UIView *view =     [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];             //设置背景颜色:灰      view.backgroundColor = [UIColor lightGrayColor];     //创建标签          CGRect frame=CGRectMake(0, 0, 160, 230);    imageView=[[UIImageView alloc] initWithFrame:frame];    imageView.image=[UIImage imageNamed:@"1.jpeg"];        imageView.tag=1001;    [view addSubview:imageView];    self.view=view;            frame = CGRectMake(10, 265, 300, 20);//设置标签的位置及大小x y width height表示左上角和大小      UILabel *label = [[UILabel alloc] initWithFrame:frame]; //创建并初始化标签,并且使大小是frame      label.textAlignment = UITextAlignmentCenter; //标签中文字的对齐方向是居中      label.backgroundColor = [UIColor clearColor]; //标签背景颜色透明的      label.font = [UIFont fontWithName:@"Verdana" size:20]; //标签文字的Verdana体20号大小      label.text = @"This is a label1";//标签文字      label.tag = 1000;     [view addSubview:label];            //创建text     frame = CGRectMake(10, 320, 300, 30);     UITextField *text=[[UITextField alloc]initWithFrame:frame];     text.backgroundColor=[UIColor whiteColor];     text.text=@"I am Roger!!";     [view addSubview:text];    //创建按钮              frame= CGRectMake(10, 370, 300, 50);//按钮位置大小      UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];     button.frame = frame;     [button setTitle:@"Click Me, Please!" forState:UIControlStateNormal];     button.backgroundColor = [UIColor clearColor];     button.tag = 2000;     [button addTarget:self                action:@selector(buttonClicked:) //响应事件       forControlEvents:UIControlEventTouchUpInside];     //把标签,文本和按钮添加到view视图里面      [view addSubview:button];          self.view = view;       //创建一个视图UIView对象,获取初始化屏幕大小,UIScreen mainScreen该设备的内部屏幕,applicationFrame应用程序的屏幕面积帧点            // Do any additional setup after loading the view, typically from a nib.}-(IBAction) buttonClicked: (id) sender{    UIAlertView *alert = [[UIAlertView alloc]                          initWithTitle:@"Action invoked!" message:@"Button clicked!"                          delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];    [alert show];}- (void)viewDidUnload{    [super viewDidUnload];    // Release any retained subviews of the main view.}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);}@end


原创粉丝点击