* 如何添加 a new file to your project? 首先highlight the group you want to add the file to (usually the project code group),然后点击Xcode左下角的 + button
* 如果添加一个existing file to your project? 只需要drag existing file(s) from "finder" to the group you want to add the file and copy them into the project
* 如何添加一个resource file (e.g. image file) to project? 只需要click and drag the file from its location into the project code group in Xcode
* 在project里获得各class的method or property的最方便方法是:在Xcode左边的navigator panel里使用Symbol Navigator (它有点象eclipse outline view)
* 如果要search text in your project则是在Xcode左边的navigator panel里使用Search Navigator
* 如果要在编辑器当前打开的fle里search text,那么就用(Command+F)
* Pragma marks do not add any features to your application; instead, they create logical sections within your code
* 如何为你的project做backup? 就要用到Xcode “snapshot” feature (like eclipse project history)。To take a snapshot, select menu "File > Create Snapshot". To view (and possibly restore) an available snapshot, select menu "File > Restore Snapshot"
* 要build and run app,就click the Run button (or Command+R). 要只build不run,就choose Command+B. 不过要只build不run,最好使用Analyze (Command+Shift+B),因为它不仅会指出build errors,还会检查你的app logo是否有潜在的问题导致你的app崩溃。
*Supporting Files/<project_name>-Info.plist file: 这是你的app的configuration file,例如application’s icons, launch images, supported device orientations, main storyboard等设置。尽管你可以直接在这个plist file里直接编辑这些设置,但Xcode提供了一个GUI来进行设置:在左边navigation panel里的project navigator中,选中你的project root node,然后在editor area的左边就会出现"TARGETS" ITEM,选中该item,就可以对你的app进行各种设置。
* property知识点:
推荐做法是NSString用copy,delegate用assign(且一定要用assign,不要问为什么,只管去用就是了,以后你会明白的),非objc数据类型,比如int,float等基本数据类型用assign(默认就是assign),而其它objc类型,比如NSArray,NSDate用retain (不过arc不能使用retain了,应该用什么?)。
@interface myClass : myParent <myProtocol> {
- NSString *myString;
- IBOutlet UILabel *myLabel;
- }
@property (strong, nonatomic) NSString *myString;
@property (strong, nonatomic) NSString *myOtherString;
@property (strong, nonatomic) IBOutlet UILabel *myOtherLabel;
@end
property定义的东东会和interface里定义的变量对应,实际上就是为变量定义getter/setter方法,当然还要通过在implementation里使用@synthesize
通常变量的name和property的name一样(例如都是myString),那么使用@synthesize时相对简单,只需要:
@synthesize myString;
如果变量的name和property的name不一样(例如变量name为myLabel,而property name是myOtherLabel),那么使用@synthesize时则需要:
@synthesize myOtherLabel = myLabel;
格式为
@synthesize <property name> = <variable name>;
还有一点很重要:其实不需要定义变量,而直接声明property即可,它会隐式定义一个对应变量。比如上面例子声明的property "myOtherString就没有定义对应的变量。注意由property隐式定义的变量名是在property name前加一个"_"。
例如 property name is "myString",隐式定义的变量名是"_myString",由于两者名字不同,因此@synthesize的写法是:
@synthesize myString = _myString;
或者使用
@synthesize myString;
两者其实是等价的!(ref link:http://stackoverflow.com/questions/7463291/ios-objective-c-question-about-properties)
另一个典型的例子就是AppDelegate.h里声明了
@property (strong,nonatomic)UIWindow *window;
在AppDelegate.m里有
@synthesize window =_window;
* Class Type Casting格式
anImportantClass *myKnownObject=(anImportantClass *)unknownObject;
* Block????
* iOS app life cycle
* 常用的一些Class
Core Application Classes
NSObject
Root class. 该class定义了一些all classes都会继承的方法,如alloc and init.
UIApplication
每一个iOS application都会实现implements a subclass of UIApplication. 该class用于handles events (例如notification of when an application has finished loading) 以及application configuration (例如controlling the status bar and setting badges (the little red numbers that can appear on application icons)).注意:你不需要写代码来创建该class的实例,只需要意识到它的存在即可。
UIWindow
UIWindow class提供了一个container for views的管理和显示。A view is more like a typical desktop application “window,” whereas an instance of UIWindow is just a container that holds the view. You usually use only a single UIWindow instance, and it is created automatically in the project templates that Xcode provides.
UIView
UIView class定义了一个长方形的区域并管理着显示在该区域里的所有控件。我们把components看作是uiview instance的subviews, 而uiview instance则看作是component的superview. Most of your applications will start by adding a view to an instance of UIWindow.
UIResponder
表示一个可以接收触摸屏上的触摸事件的对象,通俗一点的说,就是表示一个可以接收事件的对象.所有显示在界面上的对象都是从 UIResponder直接或间接继承的
Because multiple objects could potentially respond to an event, iOS passes events up what is referred to as achain of responders.The responder instance that can handle the event is called the first responder. When you’re editing a field, for example, the field has first responder status because it is actively handling user input. When you leave the field, it “resigns” first responder status. For most of your iOS development work, you won’t be directly managing responders in code.
UIControl
该class是继承UIView,它也是所有控件的superclass,such as buttons, fields, and sliders。
UIViewController
UIViewController class用于manage the contents of your views. You use a UIViewController subclass, for example, to determine what to do when a user taps a button.View controller用于loads and inter- acts with a storyboard scene in your running application.
Data Type Classes
Strings (NSString/NSMutableString)
NSString and NSMutableString的区别在于NSMutableString创建的string是可以修改的(mutable),例如可以insert, shorten, replace。
Arrays (NSArray/NSMutableArray)
examples:
NSArray *myMessages = [[NSArray alloc] initWithObjects: @”Good Job!”,@”Bad job!”,nil];
[myMessages objectAtIndex: 1];
Dictionaries (NSDictionary/NSMutableDictionary)
相当于java的HashMap
examples:
NSDictionary *myMessages = [[NSDictionary alloc] initwithObjectsAndKeys:@”Good Job!”,
@”positive”,@”Bad Job!”,@”negative”,nil];
[myMessages objectForKey:@”negative”]
Numbers (NSNumber/NSDecimalNumber)
虽然已经有了基本类型int, float。但NSNumber/NSDecimalNumber的作用在于把int and float当作Object来用。
example:
myNumberObject = [[NSNumber alloc] numberWithInt: 100];
Dates (NSDate)
examples:
myDate=[NSDate date];
[myDate earlierDate: userDate]
URLs (NSURL)
examples:
myUrl=[[NSURL alloc] initWithString:
@”http://www.floraphotographs.com/index.html”];
[myUrl host]
Interface Classes
Labels (UILabel)
Buttons (UIButton)
Switches (UISwitch)
Segmented Control (UISegmentedControl)
Sliders (UISlider)
Steppers (UIStepper)
Text Fields (UITextField/UITextView)
Pickers (UIDatePicker/UIPicker)
Popovers (UIPopoverController) : for example web browser menu
* 使用xcode,其实你可以手动添加控件 (当然,更简单直接的方法就是在Interface Builder (IB)上设计界面),例如:
UILabel *myMessage;
myMessage=[[UILabel alloc]
initWithFrame:CGRectMake(30.0,50.0,300.0,50.0)];
myMessage.font=[UIFont systemFontOfSize:48];
myMessage.text=@”Hello Xcode”;
myMessage.textColor = [UIColor colorWithPatternImage:
[UIImage imageNamed:@”Background.png”]];
[self.window addSubview:myMessage];