Objective-C:: UIKit: 视图控制器、UILabel、UIButton

来源:互联网 发布:大庆八一农垦大学淘宝 编辑:程序博客网 时间:2024/05/16 12:56

1.第一个iOS应用程序

这里写图片描述
这里写图片描述
这里写图片描述

1.1 第三个参数: 必须是UIApplication或其子类的名字,它代表着当前应用iPhone程序本身,这个参数会去读info.plist文件获取配置信息,如果该参数为nil,则默认为@“UIApplication”

1.2 第四个参数:
1.2.1 UIApplication的代理对象,也就是应用程序代理,负责控制程序的运行

1.3 UIWindow 对象
1.3.1显示界面的基础,一个视图都放在window中显示

1.4 UIViewController 视图控制器
1.4.1 控制界面的核心类,负责管理视图,及视图用用户的交互响应

1.5 UIView 视图
1.5.1 一起界面上看的见外观,需要显示的元素的分类,UIWindow其实也是继承了UIView,只是比较特殊
1.5.2 视图有一个重要的特点:层级性 — 即UIView可以作为一个大的容器,内部包含其他的UIView或其子类

AppDelegate.h文件

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    //创建window的    self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];    //直接使用 匿名对象    self.window.rootViewController=[[CZViewController alloc]init];    //将window显示出来    [self.window makeKeyAndVisible];    return YES;}

2.UILabel : 标签

2.1 作用是显示文本
2.2 核心属性

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{     //跳转到 MyViewController2 界面    CZViewController2 *mvc2=[[CZViewController2 alloc]init];    [self presentViewController:mvc2 animated:YES completion:nil];}**************************************************************//另一个类中的代码-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    //跳转回上一个页面    [self dismissViewControllerAnimated:YES completion:nil];}**************************************************************//当视图控制器管理的视图加载完成时自动调用- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor=[UIColor redColor];    //创建标签控件    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(50, 50, 200,400)];    //设置标签内容    label.text=@"这是这是这是这是这是这是这是这是";    //设置标签背景颜色    label.backgroundColor=[UIColor whiteColor];    //设置字体    label.font= [UIFont systemFontOfSize:36];    //设置文本颜色    label.textColor=[UIColor blueColor];    //设置文本的对其方法    label.textAlignment=NSTextAlignmentCenter;    //设置最大的行数默认为1。如果设置为0会自动换行到结束    label.numberOfLines=2;    /*     NSLineBreakByWordWrapping = 0,  // 整个单词换行     NSLineBreakByCharWrapping,     //  拆开单词按字母换行     NSLineBreakByClipping,         //   超出部分直接剪掉     NSLineBreakByTruncatingHead,   //  ...在前面     NSLineBreakByTruncatingTail,   // ...在后面  默认     NSLineBreakByTruncatingMiddle  // ...在中间  "ab...yz"     */    label.lineBreakMode=NSLineBreakByWordWrapping;    //设置阴影颜色    label.shadowColor =[UIColor redColor];    //设置阴影的偏移量    label.shadowOffset=CGSizeMake(3, 3);//    //fram属性中的内容不能单独修改//    fram.origin=CGPointMake(100, 100);//    label.frame=fram;    //将标签控件添加到控制器视图中    [self.view addSubview:label];    NSLog(@"加载完成时");}//视图将要显示,自动调用-(void)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];    NSLog(@"视图将要显示");}//视图已经显示,自动调用-(void)viewDidAppear:(BOOL)animated{    [super viewDidAppear:animated];    NSLog(@"视图已经显示");}//视图将要消失,自动调用-(void)viewWillDisappear:(BOOL)animated{    [super viewWillDisappear:animated];    NSLog(@"视图将要消失");}//视图已经消失,自动调用-(void)viewDidDisappear:(BOOL)animated{    [super viewDidDisappear:animated];    NSLog(@"视图已经消失");}//当内存占用过大,发出警告时自动调用该方法,我们在方法中,把不需要的对象清理一下- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}

3.UIButton

3.1 UIButton 是 UIControl的子类,UIControl 是 UIView的子类
3.2 作用:与用户交互,用户可以点击,并提供响应
3.3 核心属性,高级事件

#import "CZViewController.h"@interface CZViewController ()@property UIButton *button1;@end@implementation CZViewController- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor=[UIColor whiteColor];   /*     UIButtonTypeCustom  自定义样式    UIButtonTypeSystem  系统样式  一张图片用体验较好    UIButtonTypeDetailDisclosure,   圆圈i    UIButtonTypeInfoLight,       圆圈i    UIButtonTypeInfoDark,        圆圈i    UIButtonTypeContactAdd,      圆圈+    */    //创建button    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];    self.button1=button;    button.frame=CGRectMake(100, 100, 200, 50);    [button setTitle:@"正常" forState:UIControlStateNormal];    [button setTitle:@"按下" forState:UIControlStateHighlighted];    //设置背景图片    [button setBackgroundImage:[UIImage imageNamed:@"BTN"] forState:UIControlStateNormal];    [button setBackgroundImage:[UIImage imageNamed:@"disable"] forState:UIControlStateDisabled];    [button setBackgroundImage:[UIImage imageNamed:@"BTN2"] forState:UIControlStateHighlighted];    /*选中状态 和 失效状态必须通过代码修改*/    //设置选中时的背景图片    [button setBackgroundImage:[UIImage imageNamed:@"BTN2"] forState:UIControlStateSelected];    [button setImage:[UIImage imageNamed:@"stop" ]forState:UIControlStateSelected];    //让Button失效   // button.enabled = NO;    //设置button图片    [button setImage:[UIImage imageNamed:@"Play-btn" ]forState:UIControlStateNormal];    [button setImage:[UIImage imageNamed:@"stop" ]forState:UIControlStateHighlighted];    //为button添加事件方法    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];    //tag是父视图的一个标识     button.tag=100;     [self.view addSubview:button];    UIButton *button2=[UIButton buttonWithType:UIButtonTypeSystem];    button2.frame=CGRectMake(100, 200, 200, 50);    [button2 setTitle:@"失效" forState:UIControlStateNormal];    [button2 setBackgroundColor:[UIColor blueColor]];    [button2 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];    //[button2 setTitle:@"激活" forState:UIControlStateSelected];//        button2.tag=200;    [self.view addSubview:button2];}//两个button都用该方法-(void)buttonClick:(UIButton*)button{    //button1    if(button.tag==100){        button.selected =!button.selected;        return;    }    //button2    NSString *title=[button.titleLabel.text isEqualToString:@"失效"]?@"激活":@"失效";   //获取button的title    [button setTitle:title forState:UIControlStateNormal];    //控制前面的button激活或者失效    //self.button1.selected =NO;    self.button1.enabled =[button.titleLabel.text isEqualToString:@"失效"]?NO:YES;}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {    // Get the new view controller using [segue destinationViewController].    // Pass the selected object to the new view controller.}*/@end
0 0
原创粉丝点击