斯坦福公开课第6课笔记

faceview里加入委托,让faceview可以获得数据,也就是笑脸的程度。同时controller把自己设为被委托者,实现faceviewdatasource协议里smileforfaceview的方法,并将自己作为返回值发送给faceviewdatasource,然后faceview开始重绘笑脸。

float smile=[self.dataSource smileForFaceView:self];//这里接收controller传来的数据

@interface HappinessViewController ()<FaceViewDataSource>//协议的私有实现

[self.faceView addGestureRecognizer:[[UIPinchGestureRecognizer alloc] initWithTarget:self.faceView action:@selector(pinch:)]];//拉伸手势识别

    [self.faceView addGestureRecognizer:[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleHappinessGesture:)]];//拖拽手势识别,调用下面的handlehappinessgesture方法,计算happiness的值,用来发送给faceview,也就是委托者。

    self.faceView.dataSource=self;//这里就是设置委托

@class FaceView; //前向引用


下半部分讲到多视图,不能由一个controller控制,所以如果想添加新功能到程序中就需要添加新的MVC,而要在屏幕上切换不用的MVC视图就需要一个控制controllers的controller,最简单的就是导航栏,UINavigationController,控制MVC转换就要用到Segue,

首先,按住control,从想要发起segue的按钮处拖拽,拖到其他你想要实例化并放到屏幕上的控制器,所以xcode中,这些新控制器并不真的存在直到被segued,

而且segue总是实例化一个视图控制器,不会segue一个已经存在于堆上的,总是创建新的,离开的时候大部分都会消失,控制器生命周期短暂,所以内存消耗很低。

segue有两个重要属性,一个是identifier,赋予segue名字,在代码中通过名字来执行segue,

另一个是style,其中push is the kind of segue you use when the two controllers are inside a UINavigationController. 还有modal和custom。modal相当于确认框,轻易不要用。

the segue offers the source VC the opportunity tp prepare the new VC to come on screen. this method below is sent to the VC that contains the button that initiated the segue.

-(void)prepareForSegue:(UIStoryboardSegue *)segue

{

     if([segue.identifier isEqualToString:@"DoAParticularThing"])

          {  UIViewController *newController=segue.destinationViewController;

              //send messages to newcontroller to prepare it to appear on screen

             //the segue will do the work of putting the new controller on screen

          }      

}

You should pass data the new VC needs here and let it run;

接管视图的VIEW不能回头跟你说话,只能通过TARGETACTION或者委托。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值