• ios基础之入门(一)


    最近找到了一个可以接触ios开发的职位,可以系统的学习和练习了。先从最基本的开始:

    一、获取控件的两种方式

    1)第一种,也是经常使用的一种,通过IBOutlet方式。直接按住control键,将控件和ViewController建立联系,然后就可以通过Controller的属性来获取控件

    2)第二种,通过设置控件的tag属性。

    先设置控件的tag属性为一个integer类型的值,然后在代码中获取

    1 UILabel *lbl2 = (UILabel*)[self.view viewWithTag:2];

    二、事件处理的三种方式

    1)第一种,最常使用的是通过IBAction的方式

    2)第二种,通过代码设置事件处理方法

    在viewDidLoad事件中调用addTarget:action:forControlEvents来添加处理事件,

    addTarget:表明以改对象的某个方法来处理事件,例如当前controller

    action:代表处理事件的方法

    forControlEvents:要处理的事件类型,一个UIControlEvents的枚举值

    例子:

    - (void) viewDidLoad
    {
        [super viewDidLoad];
        NSLog(@"页面加载完成");
        AppDelegate *ad = [UIApplication sharedApplication].delegate;
        NSLog(@"打印代理类的lycname属性%@", ad.LycName);
        
        //给控件添加事件
        [self.btnMeet addTarget:self action:@selector(btnMeet_click:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    -(void) btnMeet_click:(UIButton *) sender
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"我是遇见按钮"  delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
    }   

    3)通过委托对象来处理事件

    上两种事件处理针对的都是主动控件,即继承自UIControl:UIView的控件。

    而如果是UITextView这种控件,是继承自UIScrollView:UIView,所以它本身并不具备一些常见事件,在这种情况下,就需要委托对象来处理特殊事件。

    例如刚刚提到的UITextView控件,它的默认事件有:

    -textViewShouldBeginEditing:将要开始编辑时触发

    -textViewDidBeginEditing:开始编辑后触发

    -textViewShouldEndEditing:将要结束编辑时触发

    -textViewDidEndEditing:结束编辑后触发

    -textViewDidChange:文本内容发生改变后触发

    要实现上面的事件处理,必须要实现UITextViewDelegate协议

    1 @interface UITextFieldViewController : UIViewController<UITextViewDelegate>
    2 //属性
    3 @property (strong,nonatomic) UIBarButtonItem *doneRightBI;//右侧导航条按钮-完成
    4 @property (strong,nonatomic) UIBarButtonItem *commitRightBI;//右侧导航条按钮-提交
    5 
    6 @end
    @implementation UITextFieldViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        self.txtContent.delegate = self;
        
        if (self.navigationItem != nil) {
            [self.navigationItem setTitle:@"文本框演示"];
            
            //初始化右侧导航按钮-提交按钮
            UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStyleDone target:self action:@selector(registerUserHandler)];
            
            self.commitRightBI = rightBtn;
            //设置当前右侧按钮为提交按钮
            [self.navigationItem setRightBarButtonItem:self.commitRightBI];
            
            //初始化右侧导航按钮-完成按钮
            UIBarButtonItem *rightDoneBtn = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(resignFirstReponderHandler)];
            self.doneRightBI = rightDoneBtn;
            
        }
        
    }
    //上面的按钮处理事件(registerUserHandlerresignFirstReponderHandler)就不列出来了
    @end
     1 //文本框开始编辑时触发
     2 -(void) textViewDidBeginEditing:(UITextView *)textView
     3 {
     4     NSLog(@"开始编辑本文域");
     5     if (self.navigationItem != nil)
     6     {
     7         [self.navigationItem setRightBarButtonItem:self.doneRightBI animated:NO];
     8     }
     9 }
    10 
    11 //文本框结束编辑时触发
    12 -(void) textViewDidEndEditing:(UITextView *)textView
    13 {
    14     NSLog(@"结束编辑本文域");
    15     [self resignFirstReponderHandler];
    16     if (self.navigationItem != nil)
    17     {
    18         [self.navigationItem setRightBarButtonItem:self.commitRightBI animated:NO];
    19     }
    20 }

    这样,通过上面的代码,当UITextView在不同状态时,右侧的导航按钮也会相应的改变

    三、通过代码创建控件

    1、创建UI控件

    2、调用addSubView将创建的控件添加到其他view中

    3、多次调用控件的setter方法,设置外观、行为等

    例子:

        //用代码创建一个控件
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        //设置button的大小
        button.frame=CGRectMake(120, 100, 120, 40);
        [button setTitle:@"我是代码创建的" forState:UIControlStateNormal];
        [self.view addSubview:button];
     
     
  • 相关阅读:
    PPP点对点协议
    Wireshark包过滤
    asp.net mvc 简单项目框架的搭建过程(一)对Bll层和Dal层进行充分解耦
    sql学习笔记(三)—— 联表查询
    Sql学习笔记(二)—— 条件查询
    bzoj 1696: [Usaco2007 Feb]Building A New Barn新牛舍 ——中位数排序
    汕头市队赛 SRM19 字符题
    bzoj 2037: [Sdoi2008]Sue的小球——dp
    bzoj 1914: [Usaco2010 OPen]Triangle Counting 数三角形——极角排序
    【BZOJ】3302: [Shoi2005]树的双中心 && 2103: Fire 消防站 && 2447: 消防站
  • 原文地址:https://www.cnblogs.com/chengzi/p/4398189.html
Copyright © 2020-2023  润新知