• 【Demo 0003】支持交互的应用


    本章学习要点

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

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

    一、构建需求

          1.  统计用户点击按钮次数

          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 = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 
           // Override point for customization after application launch. 
               self.window.backgroundColor = [UIColor whiteColor]; 
               [self.window makeKeyAndVisible];

           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:self action:@selector(tapButton:) forControlEvents:UIControlEventTouchUpInside];

           [self.window addSubview:button];

     

           returnYES;

        }

     

        - (void) tapButton:(id)sender

        {

            static long tapCount = 0;

            UILabel*label= (UILabel*)[self.window viewWithTag:1];

            label.text= [NSString stringWithFormat:@"你己点我%ld次了", ++tapCount];

        }

     

       代码解说: 

       1. 代码中分别创建了三个控件(UIImageView,  UILabel, UIButton), 并设置控件的相关属性;

       2. 在创建UIButton时,不是通过alloc之后再init的方式, 而是使用UIButtonWithType:类创建   
          UIButton*button= [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

       3. 为button对象加入TouchUpInside事件, 并关联tapButton事件,
          [button addTarget:self action:@selector(tapButton:) forControlEvents:UIControlEventTouchUpInside];

       4. 当用户点击button后,系统将调用tapButton事件,在此事件消息中我们创建了一个静态变量,每点击一次变量加1,最后显示到Label控件中;

       5. 值的一提的是在tapButton事件消息中的,我们通过tag来获取获取的label控件对象

          UILabel*label= (UILabel*)[self.window viewWithTag:1];

          创建时设定了label tag属性值:

     

          UILabel *label= [[[UILabel allocinitWithFrame:labelRect] autorelease];

     

          label.tag1;

     

    三、测试程序

             Tmp5fe77a18                     Tmp18d3ed18

    _____________________________________________________________________________

    演示代码

  • 相关阅读:
    结构体运算符重载出错分析
    已知空间三个点,解算外接圆圆心坐标,C++编程实现
    IQueryable与IEnumberable的区别
    Asp.net MVC中关于@Html标签Label、Editor使用
    MVC 基架不支持 Entity Framework 6 或更高版本 即 NuGet的几个小技巧
    MVC下拉框Html.DropDownList 和DropDownListFor 的常用方法
    js jq封装ajax方法
    httpSession的正确理解
    vs未能解析此远程名称: 'api.nuget.org'
    未能加载文件或程序集“EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”
  • 原文地址:https://www.cnblogs.com/ztercel/p/3138484.html
Copyright © 2020-2023  润新知