zoukankan      html  css  js  c++  java
  • IOS入门之创建视图和控件绑定

    学习IOS几天了,跟着教程做了两个小应用了,现在先来总结一下。今天就是创建视图和绑带到代码了。其实就是常见的MVC模式实现。

    使用的Xcode版本是8.2。

    在Xcode创建项目之后,默认就会创建一个Main.stroyborad,程序的入口也就是这里,可以在GeneralDeployment Info里的Main Interface修改入口storyboard为其他的。

    在项目的目录中还能看见自动创建的ViewController类,其实就是一个ViewController对应一个界面。控件库中自带了几个ViewController,新建项目时,Single View Application 就是创建一个空白的ViewController。Tabbed Application就是创建一个Tab Bar Controller。

    ~~

    在MVC模式中

    * M -- 数据模型
    * V -- View 对应这里的storyboard上的界面
    * C -- Controller 对应这里的ViewController类
    
    • 通过storyboard,我们设计界面,然后通过制定界面控件和ViewController类的关系,达到View和Controller绑定。
    • 通过storyboard,还可以设计界面间的跳转segue
    • 上图中的ViewController就是整个storyboard的root view controller

    创建好摆放控件界面之后,就可以开始摆放控件和将控件遇ViewController绑定了

    1 摆放控件

    将控件从控件库,拖动到界面上就ok。

    2 设置控件样式

    选中控件,在右侧的inspector面板里选择button的类型、状态、标题、文字颜色、文字大小等属性

    3 将控件与controller绑定

    connect inspector

    选中控件,在右侧的connection inspector面板中,可以看见button的连接属性

    • Triggered Segues 这个action连接到一个界面时,点击button就将调转到连接的界面
    • Outlet Collections button将作为一个属性集合的一员
    • Sent Events 当button的不同点击事件触发时,连接到上面的方法就被触发
    • Refrencing Outlets button作为一个类的属性,一般就是button所在界面的ViewController类的属性

    button可以作为类属性的同时,将自己的事件与类的方法绑定;也可制作为属性,或者只绑定事件。

    4 开始操作

    有2种方式,可以进行绑定

    • 1 在controller类里定义属性和方法,再手动绑定
      • 1

          button的属性: @property(nonatomic,weak)IBOutlet UIButton* button;
          button的事件: - (IBAction)clickButton:(id)sender;
        
      • 2 在button的connection inspector面板中,将属性或事件绑定到button属性和clickButton方法上

      • 3 绑定之后,就能属性是和View Controller绑定的;touch up side 事件是和View Controller里的clickButton方法绑定的

    ~~

    • 2 自动绑定,好安逸。选中button所在的布局,然后点击Xcode右上角的assistan editor。确保是在ViewController.h文件,如果不是可以在顶部切换到。接着鼠标按着button + Ctrl,往interface区中拖,按图那样操作,就能实现绑定。

    ~~

    经过上述的操作之后,我们就能点击界面中间的button,做些事情了。
    最终控制器里的代码,加一句点击button就修改button的文字。

    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController
    
    @property (weak, nonatomic) IBOutlet UIButton *button;
    
    - (IBAction)clickButton:(id)sender;
    
    @end
    
    #import "ViewController.h"
    
    @interface ViewController ()
    @end
    

    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
    }
    
    - (IBAction)clickButton:(id)sender {
        //修改button文字
        [self.button setTitle:@"clicked!" forState:UIControlStateNormal];
    }
    
    @end
    

    ~~

    以上就是在storyboard里绑定Button和控制器的方法,其他控件也类似。

    小生入门,有何不对之处,还望指出。_

  • 相关阅读:
    27 Spring Cloud Feign整合Hystrix实现容错处理
    26 Spring Cloud使用Hystrix实现容错处理
    25 Spring Cloud Hystrix缓存与合并请求
    24 Spring Cloud Hystrix资源隔离策略(线程、信号量)
    23 Spring Cloud Hystrix(熔断器)介绍及使用
    22 Spring Cloud Feign的自定义配置及使用
    21 Spring Cloud使用Feign调用服务接口
    20 Spring Cloud Ribbon配置详解
    19 Spring Cloud Ribbon自定义负载均衡策略
    18 Spring Cloud Ribbon负载均衡策略介绍
  • 原文地址:https://www.cnblogs.com/jiy-for-you/p/6892354.html
Copyright © 2011-2022 走看看