[课堂笔记]斯坦福大学公开课:IOS 7应用开发 lecture6

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[课堂笔记]斯坦福大学公开课:IOS 7应用开发 lecture6相关的知识,希望对你有一定的参考价值。

1.Abstract means that this class cannot be instantiated and used,it’s only useful as a superclass for other classes that are concrete. (04:00)
 
2.And I also like to document,even on my implementation any methods that basically are abstract.Any method that is required that you override this method.And also any abstract method,you’ve got to make them public.(05:00)
 
3.How do we do it?It’s very simple actually,you just go to the object palette — the same place you drag a button or a slider out of,and you drag a view controller,and when you drag it out,it’ll be a generic view controller.In other words,its class will be UIViewController.then you go create your subclass of UIViewController.In this case,in the demo I just did I created a subclass of a class I already had,but you can create this direct subclass of UIViewController,and then you go and inspect it with the attributes inspector and you set its class.(13:00)
 
4.So you got all these MVCs,how do you present them to the user?Well,there’s a number of ways to do it,but the fundamental answer is you use a controller whose view is other MVCs.(14:00)
 
5.A UINavigationController looks like this.It’s a MVC,its controller is the UINavigationController,its view is this kind of white rectangular thing with a gray bar at the top.and possibly a gray bar at the bottom,and it includes a rootViewController,which is a special outlet that comes out that points to the controller of the MVC that’s going to appear in the navigation controller when it starts up, it’s rootViewController.So when you set that to be this MVC,the navigation controller is going to look like this.It‘s gonna put the view of that root view controller’s MVC into — embedded inside of its view.Now,if you then have some button or something inside this view or maybe in the bar at the top that wants to give that more detail and have other MVC provides,you click on that button and it slides in the view for this other MVC,and puts a back button there as you see.Now again,when the navigation controller’s in this state,the MVC that’s on the left kind of wants to be quite.It doesn’t want to be doing anything.The MVC on the right is completely in control.Now,of course,that little back button which automatically appears when you do this little back button,when you press it,it’s obviously just going to move back to the original one,then this MVC is now live again.What happened to the MVC on the right when we clocked the back button?The answer is ,it got deallocated from the heap.That MVC on the right,got unfreeze-dried from the storyboard when we put it onscreen,and when it went bank off screen,it got deallocated.(23:00)
 
6.Segue is  very important to understand in this class.A segue is just when you’re going to move or segue from one MVC to another.And the segue that’s involved in a UINavigationController is called a push segue.We kind think of the navigation controller as we push a new MVC into the front,and maybe push another one,and then we pop back and pop back when we hit back button.
 
7.Okay.So there’s a problem here though.If I were to run my application in this state,it would not work.Clicking that button would not segue.Why is that?Because this whole thing has to be in a navigation controller.(31:00)
 
8.But you might ask,how do I get ahold of my UINavigationController to send it this message and an awesome thing about this way is all set up is,if you are a view controller and you are inside a navigation controller — anywhere on its stack,you can get the property on yourself called navigationController and it will return you the navigation controller you’re in.(37:00)
 
9.So you need to prepare that day MVC to come onscreen by telling it what day. Once you tell it what day,it’s on its own.It just goes off,shows the day,you scroll around,you know,it’s the one that pushes again to push in the event detail one,but you’ve got to prepare it.So MVCs need to be prepared.Well,who does the preparation?Obviously,it’s the MVC that you clicked in to cause this other MVC to slide in.So in the example we just did,the MVC that has the button in it,it should have a method in it,prepareForSegue,that prepares the next one over — the one on the right to come onscreen.The month one has to have a prepareForSegue that prepares a day view controller for coming onscreen.(39:00)
 
10.So when you get this preparedForSegue,the first thing you’re going to do is look at the identifier of the segue to see which segue we’re talking about.Then the next thing we do is we’re going to say what MVC are we segueing to?Usually we are going to know what class of thing it is or think we know,because we know the identifier of the segue.So it only makes sense if you’re in the month view and you click on that day,that thing it;s segueing to better be a day view controller.But you still,even so,will usually do segue.destinationViewController isKindOfClass,the view controller we expect.Then we’ll make a little local variable with it,and then we’re going to do doVC which is our local variable,dot neededInfo equals,that’s kind of pseudo code for preparing that day view controller. (40:00)
 
11.If the relationship between these two MVCs is “more detail”,we use a UINavigationController to let them share the screen.But it’s special because we can set its rootViewController outlet to another MVC.Then a UI element in this view(e.g. a UIButton)can segue to the other MVC and its View will now appear in the UINavigationController instead.技术分享
 
12.”push” is the kind of segue you use when the two controllers are inside a UINavigationController.
 
13.Sometimes it makes sense to segue directly when a button is touched,but not always.For example,what if you want to conditionally segue?
You can programmatically invoke segues using method in UIViewController:
-(void)performSegueWithIdentifier:(NSString *)segueId sender:(id)sender;
The segueId is set in the attributes inspector in Xcode.
The sender is the initiator of the segue(a UIButton or yourself(UIViewController)usually).
-(IBAction)rentEquipment{
     if(self.snowTraversingTalent == Skiing){
     [self performSegueWithIdentifier:@“AskAboutSkis” sender:self];
}
else{
     [self performSegueWithIdentifier:@“AskAboutSnowboard” sender:self];
    }
}

 

 
14.When a segue happens,what goes on in my code?
The Segue offers the source VC the opportunity to ”prepare” the new VC to come on screen.
This method is sent to the VC that contains the button that initiated the segue:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
     if([segue,identifier isEqualToString:@“DoSomething”]){
     if([segue destinationViewController isKindOfClass:[DoSomethingVC class]]){
     DoSomethingVC *doVC = (DoSomethingVC *)segue.destinationViewController;
doVC.neededInfo = …;
        }
    }
}

You should pass data the new VC needs here and “let it run”.

Think of the new VC as part of the View of the controller that initiates the segue.
It must play by the same rules as a view.For example,it should not talk back to you(except through blind communication like delegation).
 
15
.技术分享技术分享

以上是关于[课堂笔记]斯坦福大学公开课:IOS 7应用开发 lecture6的主要内容,如果未能解决你的问题,请参考以下文章

[课堂笔记]斯坦福大学公开课:IOS 7应用开发 lecture5

[课堂笔记]斯坦福大学公开课:IOS 7应用开发 lecture7

斯坦福大学公开课:iPad和iPhone应用开发(iOS5) 学习笔记 2

斯坦福大学公开课:IOS 7应用开发 lecture10

斯坦福大学公开课:IOS 7应用开发 lecture11

iOS 开发学习笔记