【Demo 0003】支持交互的应用

本章学习要点

         1.   接触更多UI控件(UIImageView, UIButtong, UILabel)

         2.   掌握控件事件处理过程

一、创建工程

        1.  XCode 》File 》Projects (Cmd + Shift + N)  

        2.  IOS 》User Interface 》Empty Application

        3.  Product Name:  Demo0003   Device: iPhone

二、编写代码

   - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
   { 
        self.window = [[[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]] autorelease]; 
        // Override point for customization after application launch. 
        self.window.backgroundColor = [UIColorwhiteColor]; 
        [self.windowmakeKeyAndVisible];

 

CGSize screenSize = [[UIScreen mainScreen] bounds].size;

 

// add imageview

UIImage*image= [UIImage imageNamed:@"login.jpg"];

CGRect imageRect= CGRectMake((screenSize.width - image.size.width)/2, 80, image.size.width, image.size.height);

UIImageView*imageView= [[[UIImageView alloc] initWithImage:image] autorelease];

[imageView setFrame:imageRect];

[self.window addSubview:imageView];

 

// add label

CGRect labelRect= CGRectMake(0, imageRect.origin.y + imageRect.size.height + 50, screenSize.width, 30);

UILabel*label= [[[UILabel alloc] initWithFrame:labelRect] autorelease];

label.tag= 1;

label.text= @"你还没有点击哟";

label.font= [UIFont systemFontOfSize:15];

label.textColor= [UIColor blueColor];

label.textAlignment= NSTextAlignmentCenter;

[self.window addSubview:label];

 

// add button

int buttonWidth= 120;

int left= (screenSize.width - buttonWidth) / 2;

int top= labelRect.origin.y + labelRect.size.height + 20;

CGRect buttonRect= CGRectMake(left, top, buttonWidth, 40);

UIButton*button= [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

[button setFrame:buttonRect];

[button setTitle:@"点击我"forState:UIControlStateNormal];

[button addTarget:selfaction:@selector(tapButton:) forControlEvents:UIControlEventTouchUpInside];

[self.window addSubview:button];

 

    returnYES;

}

三、测试程序

 

_____________________________________________________________________________

演示代码

posted @ 2013-06-16 09:09  zTercel  阅读(147)  评论(0编辑  收藏  举报