美文网首页
UIViewController

UIViewController

作者: 画个完美句号 | 来源:发表于2016-02-21 16:13 被阅读49次

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    创建VC

    RootViewController *rootVC = [[RootViewController alloc]init];

    根视图

    self.window.rootViewController = rootVC;

    [rootVC release];

    [_window release];

    指派初始化方法 无论怎么都会被执行

    -(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if(self){

    NSLog(@"初始化“);

    }

    return self;

    }

    加载视图

    -(void)loadView{

    重写时一定要写super

    loadView方法 负责重建self.view

    [super loadView];

    NSLog(@"加载视图”);

    }

    视图已经加载

    -(viod)viewDidLoad{

    [super viewDidLoad];

    NSLog(@"视图已经加载“);

    self.view.backgroundColor = [UIColor grayColor];

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];

    btn.frame = CGRectMak(100,100,100,100);

    btn.backgroundColor = [UIColor blackColor];

    [btn addTarget:self action:@selecter(go)forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    }

    -(void)go{

    NSLog(@"点击”);

    跳转页面(模态)

    TwoViewController *twoVC = [[TwoViewController alloc]init];

    跳转

    [self presentViewController:twoVC animated:YES completion:^{

    }];

    }

    self.view.backgroundColor = [UIColor yellowColor];

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];

    btn.frame = CGRectMake(100,100,100,100);

    btn.backgroundColor = [UIColor purpieColor];

    [btn addTarget:self action:@selector(back)forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

    用户名

    LTView *user = [[LTView alloc]initWithFrame:CGRectMake(0,250,375,60)];

    user.backgroundColor = [UIColor cyanColor];

    [self.view addSubview:user];

    [user release];

    user.label.text = @"用户名”;

    }

    -(void )back{

    NSLog(@"返回“);

    返回上一页

    [self dismissViewControllerAnimated:YES completion:^{

    }];

    }

    @interface LTView : UIView

    @property(nonatomic,retain)UILabel *label;
    @property(nonatomic,retain)UITextField *tf;


    - (instancetype)initWithFrame:(CGRect)frame
    {
    self = [super initWithFrame:frame];
    if (self) {
    //label
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 40)];
    self.label.backgroundColor = [UIColor blueColor];
    [self addSubview:self.label];
    [_label release];
           
           
            //tf
            self.tf = [[UITextField alloc] initWithFrame:CGRectMake(130, 10, 100, 40)];
            self.tf.backgroundColor = [UIColor greenColor];
            [self addSubview:self.tf];
            [_tf release];
           

    鉴定协议

    @interface RootViewController () <UIScrollViewDelegate>
    @property (nonatomic, retain) UIScrollView *scrollView;
    @property (nonatomic, retain) UIPageControl *page;
    @property (nonatomic,retain) NSTimer *timer;
    @end

    @implementation RootViewController

    - (void)dealloc
    {
        [_scrollView release];
        [_page release];

    }

    当手指拖拽时 停止定时器
    -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
        [self.timer invalidate];
    }
    //当手指离开屏幕 重新创建定时器
        [super dealloc];
    }

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        self.view.backgroundColor = [UIColor whiteColor];
       
        // scrollView
        self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
        self.scrollView.backgroundColor = [UIColor yellowColor];
        [self.view addSubview:self.scrollView];
        [_scrollView release];
        // 代理人
        self.scrollView.delegate = self;
        // 滚动范围
        self.scrollView.contentSize = CGSizeMake(VIEW_WIDTH * 7, VIEW_HEIGHT);
        // 整页
        self.scrollView.pagingEnabled = YES;
        // 添加图片
        for (NSInteger i = 1; i <= 6; i++) {
            UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake((i-1)*VIEW_WIDTH, 0, VIEW_WIDTH, VIEW_HEIGHT)];
            // 图片名
            NSString *name = [NSString stringWithFormat:@"S%ld.jpg", i];
            imgView.image = [UIImage imageNamed:name];
            [self.scrollView addSubview:imgView];
            [imgView release];
           
            //在最后添加最后一页
            UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(VIEW_WIDTH*6, 0, VIEW_WIDTH, VIEW_HEIGHT)];
            img.image = [UIImage imageNamed:@"S1.jpg"];
            [self.scrollView addSubview:img];
            [img release];
           
           
           
        }
        // UIPageControl
        self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
        self.page.backgroundColor = [UIColor blackColor];
        // 添加父视图
        // 加载self.view保证视图滑动时 依然存在
        [self.view addSubview:self.page];
        [_page release];
        self.page.numberOfPages = 6;
        self.page.center = CGPointMake(self.view.center.x, VIEW_HEIGHT-50);
        [self.page addTarget:self action:@selector(page:) forControlEvents:UIControlEventValueChanged];
        self.page.tag = 1000;
        定时器 实现自动轮播
        多长时间翻页
        self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(autoRoll) userInfo:nil repeats:YES];

    定时器方法
    -(void)autoRoll{
        //翻页所用的时间
        [UIView animateWithDuration:0.5 animations:^{
           
            //当前x+图片宽度
            self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x + VIEW_WIDTH, 0);
           
        } completion:^(BOOL finished) {
            //动画完成之后 要做的事(没有动画)
           
            if (self.scrollView.contentOffset.x/VIEW_WIDTH == 6) {
                self.scrollView.contentOffset = CGPointZero;
            }
        }];
    }

    //当使用定时器滚动时 没有拖拽和减速阶段 需要在scrollViewDidscroll方法中修改
    -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
       
       
    //    if (scrollView.contentOffset.x / VIEW_WIDTH == 6) {
    //        scrollView.contentOffset = CGPointMake(0, 0);
    //    }
     self.page.currentPage = scrollView.contentOffset.x/VIEW_WIDTH;
           
           


    }


    //当手指离开屏幕(结束减速时) 重新创建定时器

    // 结束减速
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
    {
       
       //当滚动到最后一页时 跳到第一页
    //    if (scrollView.contentOffset.x / VIEW_WIDTH == 7) {
    //        scrollView.contentOffset = CGPointMake(0, 0);
    //       
           //动画播放
         //   [scrollView setContentOffset:CGPointZero animated:NO];
     //   }
       
       
        // 修改小圆点
        self.page.currentPage = scrollView.contentOffset.x/VIEW_WIDTH;
        //新建定时器
        [self createTimer];
       //1s之后 执行[self createTimer];
    //     [self performSelector:@selector(createTimer) withObject:nil afterDelay:1];
       
       
       
    }
        //新建定时器
    -(void)createTimer{
            self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(autoRoll) userInfo:nil repeats:YES];
       
       
       
    }

    #pragma mark - page方法
    - (void)page:(UIPageControl *)page
    {
        // 通过动画滚动
        [UIView animateWithDuration:0.5 animations:^{
            self.scrollView.contentOffset = CGPointMake(VIEW_WIDTH*page.currentPage, 0);
        }];
    }

    :签协议
    @interface RootViewController ()<PassDelegate>
    @property(nonatomic,retain) UIButton *but;
    @property(nonatomic,retain) UITextField *textField;

    (void)dealloc
    {
        [_but release];
        [_textField release];
        [super dealloc];
    }

    self.view.backgroundColor = [UIColor cyanColor];
        self.title = @"首页";
       
       
        self.but = [UIButton buttonWithType:UIButtonTypeSystem];
        self.but.backgroundColor = [UIColor grayColor];
        self.but.frame = CGRectMake(50, 250, 300, 40);
        [self.view addSubview:_but];
        [self.but addTarget:self action:@selector(goTwo) forControlEvents:UIControlEventTouchUpInside];
        [_but release];
       
        self.textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 150, 300, 40)];
        self.textField.backgroundColor = [UIColor lightGrayColor];
        [self.view addSubview:_textField];
        _textField.layer.borderWidth = 1;
        _textField.layer.borderColor = [UIColor blackColor].CGColor;
        _textField.layer.cornerRadius = 10;
        [_textField release];
       
       
       
       
       
    }
    -(void)goTwo{
        TwoViewController *twoVC = [[TwoViewController alloc] init];
    #warning 属性2:在push页面之前传值(创建对象之后 push之前)
        twoVC.string = self.textField.text;
       
    #warning 协议5:设置代理人
        //为了保证 设置代理人对象和push的对象是同一个 在创建对象之后 push之前 设置delegate
        twoVC.delegate = self;
        [self.navigationController pushViewController:twoVC animated:YES];
        [twoVC release];
    }

    #warning 协议6:实现协议方法
    -(void)passValue:(NSString *)string{
        //把收到的string赋值给输入框
        self.textField.text = string;
       
       
    }

    声明协议(定义一个带参数的方法)
    @protocol PassDelegate <NSObject>
    //@required(默认)
    //@optional
    -(void)passValue:(NSString *)string;//需要传什么写什么此处为字符串

    在第二页声明一个属性 用来保存数据
    @property (nonatomic,copy) NSString *string;

    #warning 协议2: 定义代理人属性

    @property(nonatomic,assign)id<PassDelegate>delegate;

    @property(nonatomic,retain) UIButton *but;
    @property(nonatomic,retain) UITextField *tf;

    self.view .backgroundColor = [UIColor cyanColor];
       
        self.title = @"第二页";
       
        self.but = [UIButton buttonWithType:UIButtonTypeSystem];
        self.but.backgroundColor = [UIColor grayColor];
        self.but.frame = CGRectMake(50, 250, 300, 40);
        [self.view addSubview:_but];
        [_but release];
        [self.but addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
      _tf = [[UITextField alloc] initWithFrame:CGRectMake(50, 150, 300, 40)];
        _tf.backgroundColor = [UIColor lightGrayColor];
        [self.view addSubview:_tf];
        _tf.layer.borderWidth = 1;
        _tf.layer.borderColor = [UIColor blackColor].CGColor;
        _tf.layer.cornerRadius = 10;
        [_tf release];
     #warning 属性3:通过属性给当前页面内容赋值
        self.tf.text = self.string;
       
    }
    -(void)back{
    #warning 协议3:返回上一页之前 让代理人调用协议方法
        [self.delegate passValue:self.tf.text];
        [self.navigationController popViewControllerAnimated:YES];
    }

    相关文章

      网友评论

          本文标题:UIViewController

          本文链接:https://www.haomeiwen.com/subject/maovkttx.html