父子控件(控件可以内嵌其他控件,形参父子关系)

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UISwitch *customSw;
@property (weak, nonatomic) IBOutlet UIView *redView;
@property (weak, nonatomic) IBOutlet UIView *greenView;
@property (weak, nonatomic) IBOutlet UISegmentedControl *customSmt;

@end

@implementation ViewController

// 只要程序已启动就会调用该方法/ 和main函数一样, 该方法是由系统调用的
// 只要UI界面创建好了就会调用该方法
// 只要控制器对应的那个UIView属性对应的控件创建好了就会调用
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view, typically from a nib.
    /*
    // 注意: 虽然在Storyboard中不能将某个控件添加到另一个控件中, 但是只要通过代码, 所以继承于UIView的控件都可以添加子控件
    UIButton *btn = [[UIButton alloc] init];
    btn.frame = CGRectMake(100, 100, 200, 300);
    btn.backgroundColor = [UIColor redColor];
    [self.view addSubview:btn];
    
    UILabel *lab = [[UILabel alloc] init];
    lab.text = @"abdc";
    lab.frame = CGRectMake(0, 0, 100, 100);
    lab.backgroundColor = [UIColor greenColor];
    [btn addSubview:lab];
     */
    /*
//    NSLog(@"redView = %@", self.redView);
    // superview是用于获取父控件
    // 谁调用superview, 那么获取的就是谁的父控件
//    NSLog(@"superView = %@", self.customSw.superview);
    
    // subviews是用于获取所有的子控件
    // 谁调用subviews, 那么就是获取谁的子控件
    NSLog(@"%@", self.redView.subviews);
    NSLog(@"--------------------");
    NSLog(@"%@", self.greenView.subviews);
    
    // 给redView打了一个标记, 将来就可以通过这个标记找到redView
//    self.redView.tag = 110;
//     NSLog(@"redView = %@", self.redView);
//    NSLog(@"greenView = %@", self.greenView);
    NSLog(@"customSmt = %@", self.customSmt);
  */
    /*
    //1.通过代码创建一个控件
    UISwitch *sw = [[UISwitch alloc] init];
    sw.tag = 998;
    // 2.将sw添加到控制器的view中
    // 谁掉用就会添加到谁里面
    [self.view addSubview:sw];
     */
    
    // 大部分UI控件是没有默认的尺寸的, 所以看不见UIButton
    UIButton *btn = [[UIButton alloc] init];
    btn.backgroundColor = [UIColor redColor];
    // frame是以父控件的左上角为0,0
//    btn.frame = CGRectMake(100, 100, 100, 100);
    // 注意:bounds是以自己的左上角为00, 所以一般情况下设置bounds的x/y无效
    
    // 了解: 默认情况下控件会用锚点来对其左上角, 锚点默认就是center的位置, 所以才导致了只看到了控件的一部分, 并没有完全展示
    // 一般情况使用bounds来修改尺寸
    btn.bounds = CGRectMake(0, 0, 100, 100);
    // 中心点就是控件宽高的交点, 宽高的一半的位置
    // 一般情况下使用center来修改位置
    btn.center = CGPointMake(200, 200);
    
    [self.view addSubview:btn];
    
//    UIView *customView = [[UIView alloc] init];
//    customView.backgroundColor = [UIColor purpleColor];
//    customView.frame = CGRectMake(0, 200, 300, 300);
//    [self.view addSubview:customView];
    
//    [customView addSubview:btn];
 
}

// touchesBegan方法可以监听用户触摸屏幕
- (void)touchesBegan:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
{
    /*
    UIView *sw = [self.view viewWithTag:998];
    // 从父控件中移除
    // 谁调用就移除谁
    [sw removeFromSuperview];
     */
    
    // 注意:viewWithTag只能查找当前控件和当前控件的子控件, 不能查找兄弟控件, 或者父控件
//    UIView *res = [self.greenView viewWithTag:666];
    UIView *res = [self.redView viewWithTag:777];
    NSLog(@"res = %@", res);
    
}

- (IBAction)customBtnClick
{
    // 利用tag获取对应tag的值
    // viewWithTag会到调用该方法的view以及它所有的子控件中查找, 有没有标记是110的控件, 如果有就返回标记是110的控件, 如果没有就返回nil
//    UIView *res = [self.view viewWithTag:110];
    // 查找顺序: 1.先找自己 2.找子控件 3.找间接子控件(孙子..)
    UIView *res = [self.view viewWithTag:110];
    NSLog(@"res = %@", res);
}

/*
- (UIView *)customViewWithTag:(NSInteger)tag
{
    if (self.view.tag == tag) {
        return self.view;
    }
    
    for (UIView *subView in self.view.subviews) {
        if (subView.tag == tag) {
            return subView;
        }
        // 递归
    }
}
 */


@end

 

posted @ 2015-07-10 00:42  J了个P  阅读(341)  评论(0编辑  收藏  举报