UIViewController

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];
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,319评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,801评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,567评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,156评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,019评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,090评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,500评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,192评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,474评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,566评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,338评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,212评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,572评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,890评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,169评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,478评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,661评论 2 335

推荐阅读更多精彩内容