今天算是不错的开始吧,读完了薄薄的《The iOS Apprentice Getting Started》
虽然薄,但基础知识还是蛮多的,也使我理解了许多以前看视频中没有的细节介绍,感觉看文档比看视频学的知识要仔细,而且效果好。
local variables, as they only come into existence when some method action is performed and cease to exist when the action is done.
instance variable (or ivar for short), because its scope is the same as the scope of the object instance it belongs to. In other words, you use instance variables if you want to keep a certain value around, from one event to the next.
To make a distinction between the two types of variables, so that it’s always clear at a glance how long they will live, the names of instance variables are often prefixed with an underscore. That’s why you wrote _currentValue instead of just currentValue. This is not a requirement, but it is customary among Objective-C programmers.
I would like to point out one more time the difference between local variables and instance variables (or “ivars”). As you should know by now, a local variable only exists for the duration of the method that it is defined in, while an instance variable exists as long as the view controller (the object that owns it) exists.
Local variables are easy to recognize, because the first time they are used inside a method their name is preceded with a datatype, in this case int, NSString and UIAlertView.This syntax creates a new variable. Because the variable is created inside the method, it is restricted to that method only and does not exist outside of it. As soon as the method is done, the local variables cease to exist. The next time that method is invoked, these local variables are created anew.
The instance variables,you can use them from any method, without the need to declare them again.
To make the distinction between instance variables and local variables extra clear, it is a convention among Objective-C programmers to begin the names of instance variables with an underscore. That’s why you keep track of the total score in a variable named _score, so that it does not get confused for a local variable.
Tip: Xcode will give a warning when you attempt to use the name of an instance variable for a local variable. It will say something to the effect of, “Local declaration of '_score' hides instance variable”. It’s not recommended to have a local variable that is named the same as an instance variable, because the local one takes precedence over the instance variable and effectively “hides” it. If you’re unaware of this overlap, you could be sneaking bugs into your app that will be very hard to find. It is always a good idea to pay attention to the warnings of Xcode. They may save you from doing things you did not really intend to. Small mistakes are easy to make!
If you’ve played with Objective-C before, you may have heard that you need to “synthesize” your properties before you can use them. That used to be true but with the latest versions of Xcode, adding the @synthesize directive for your properties is no longer necessary. Simply add the @property line, connect the outlet in the storyboard, and you’re all set.
3.Properties VS. instance variables 属性和实例变量
Properties and instance variables have a lot in common. In fact, when you create a property, it is “backed” by an instance variable. That means your slider property stores its value in an instance variable named _slider (note the leading underscore). This instance variable was automatically added to the view controller by the Objective-C compiler.
Why? Well, a property needs to store its value somewhere and an instance variable is a good place for that. You can tell the difference between the two because properties are always accessed using self.
This uses the backing instance variable directly:
So what is the added benefit of using a property over an instance variable? There are several reasons but mainly instance variables are supposed to be used only by the insides of an object. Other objects aren’t intended to see them or use them.
Properties, however, can be accessed by other objects that are outside of your view controller.
4.Action method VS. normal methods
So what is the difference between an action method and a regular method? Nothing. An action method is really just the same as any other method. The only special thing is the (IBAction) specifier. This allows Interface Builder to see the method so you can hook it up to your buttons, sliders, and so on.
Other methods, such as viewDidLoad, do not have the IBAction specifier. This is a good thing because all kinds of mayhem would occur if you hooked them up to your buttons.
This is the simple form of an action method:
You can also ask for a reference to the object that triggered this action:
- (IBAction)buttonTapped:(UIButton *)button;
5.Delegates in three steps
Delegates are used everywhere in the iOS SDK, so it’s good to remember that it always takes three steps to become someone’s delegate.
1) You declare yourself capable of being a delegate. Here, you did that by including <UIAlertViewDelegate> in your @interface line. This tells the
compiler that the view controller can actually handle the notification messages that are sent to it.
2) You let the object in question, in this case the UIAlertView, know that you wish to become its delegate. If you forget to tell the alert view that it has a delegate, then it will never send you any notifications.
3) Implement the delegate methods. It makes no point to become a delegate if you’re not responding to the messages you’re being sent! Often, delegate methods are optional, so you don’t need to implement all of them. For example, UIAlertViewDelegate actually declares seven different methods but for this app you only care about alertView:didDismissWithButtonIndex:.
6.Note that instance variables have a default value of 0
8. listener pattern VS. observer pattern
9.Storyboards have a neat trick for this: segues (pronounced “seg-way” like the silly scooters). A segue is a transition from one screen to another and they are really easy to add.
Go to the Project Settings screen and under Deployment Info, Status Bar Style, check the option Hide during application launch.
- (BOOL)prefersStatusBarHidden {
11.It’s only a small detail but the difference between a mediocre app and a great app is that great apps do all the small details right.
12.Images should be in PNG format
13.You can either make the labels larger by dragging their handles to resize them manually, or you can use the Size to Fit Content option (⌘=). I prefer the latter because it’s less work.