月度归档:2013年03月

Property List_Persistence

//
//  BIDViewController.h
//  Persistence
//

#import 

@interface BIDViewController : UIViewController

@property (strong, nonatomic) IBOutletCollection(UITextField) NSArray *lineFields;

@end

//
//  BIDViewController.m
//  Persistence
//

#import "BIDViewController.h"
#import "BIDFourLines.h"

static NSString * const kRootKey = @"kRootKey";

@implementation BIDViewController

- (NSString *)dataFilePath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(
                                                         NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:@"data.plist"];
    return [documentsDirectory stringByAppendingPathComponent:@"data.archive"];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    NSString *filePath = [self dataFilePath];
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
        for (int i = 0; i < 4; i++) {
            UITextField *theField = self.lineFields[i];
            theField.text = array[i];
        }
        NSData *data = [[NSMutableData alloc]
                        initWithContentsOfFile:filePath];
        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]
                                         initForReadingWithData:data];
        BIDFourLines *fourLines = [unarchiver decodeObjectForKey:kRootKey];
        [unarchiver finishDecoding];
        
        for (int i = 0; i < 4; i++) {
            UITextField *theField = self.lineFields[i];
            theField.text = fourLines.lines[i];
        }
    }
    
    UIApplication *app = [UIApplication sharedApplication];
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(applicationWillResignActive:)
     name:UIApplicationWillResignActiveNotification
     object:app];
}

- (void)applicationWillResignActive:(NSNotification *)notification
{
    NSString *filePath = [self dataFilePath];
    NSArray *array = [self.lineFields valueForKey:@"text"];
    [array writeToFile:filePath atomically:YES];
    BIDFourLines *fourLines = [[BIDFourLines alloc] init];
    fourLines.lines = [self.lineFields valueForKey:@"text"];
    
    NSMutableData *data = [[NSMutableData alloc] init];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]
                                 initForWritingWithMutableData:data];
    [archiver encodeObject:fourLines forKey:kRootKey];
    [archiver finishEncoding];
    [data writeToFile:filePath atomically:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

//
//  BIDFourLines.h
//  Persistence
//

#import 

@interface BIDFourLines : NSObject 

@property (copy, nonatomic) NSArray *lines;

@end



//
//  BIDFourLines.m
//  Persistence
//

#import "BIDFourLines.h"

static NSString * const kLinesKey = @"kLinesKey";

@implementation BIDFourLines

#pragma mark - Coding

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self) {
        self.lines = [aDecoder decodeObjectForKey:kLinesKey];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder;
{
    [aCoder encodeObject:self.lines forKey:kLinesKey];
}

#pragma mark - Copying

- (id)copyWithZone:(NSZone *)zone;
{
    BIDFourLines *copy = [[[self class] allocWithZone:zone] init];
    NSMutableArray *linesCopy = [NSMutableArray array];
    for (id line in self.lines) {
        [linesCopy addObject:[line copyWithZone:zone]];
    }
    copy.lines = linesCopy;
    return copy;
}

@end

Google技术是越来越进步了,但会不会错杀呢

我们检测到您的网站有部分网页可能使用了违反 Google 网站站长指南的技术。

具体来说,就是查找可能的人为或非自然链接,这些链接指向您的网站且意图操纵 PageRank。例如,购买链接以提升 PageRank 或参与链接方案,这些都属于非自然链接行为。

我们建议您更改自己的网站,从而让它符合我们的质量指南的要求。做出相应更改后,请立即提交您的网站供 Google 重新审核。

如果您发现指向自己的网站且您无法控制或删除的非自然链接,请在您的重新审核请求中提供相关详情。

如果您对如何解决此问题有其他疑问,请访问我们的网站站长帮助论坛,以便获取支持。

此致

Google 搜索质量小组敬上

AppSetting_Utility Application

用模板utility application包括: mainViewController, slipSideViewController

协议protocol 在B定义:(简单点说就是方法的定义和实现,调用)

@procotol BKSlipSideViewControllerDelegate
-(void)slipSideViewControllerDidFinish: (BKSlipSideViewController *)controller;
@end

@Property (nonatomic, weak) id delegate;

在B中使用delegate:
– (IBAction)done:(id)sender
{
[self.delegate flipsideViewControllerDidFinish:self];
}

在A中对protocol进行实现:
– (void)flipsideViewControllerDidFinish:(BIDFlipsideViewController *)controller
{
[self refreshFields];
[self dismissViewControllerAnimated:YES completion:nil];
}

//
//  BIDAppDelegate.m
//  AppSettings
//

#import "BIDAppDelegate.h"
#import "BIDMainViewController.h"

@implementation BIDAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    NSDictionary *defaults = @{
        kWarpDriveKey : @YES,
        kWarpFactorKey : @5,
        kFavoriteSinKey : @"Greed"};
    
    [[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
    return YES;
}
							


@end

//
//  BIDMainViewController.h
//  AppSettings
//

#import "BIDFlipsideViewController.h"

#define kUsernameKey        @"username"
#define kPasswordKey        @"password"
#define kProtocolKey        @"protocol"
#define kWarpDriveKey       @"warp"
#define kWarpFactorKey      @"warpFactor"
#define kFavoriteTeaKey     @"favoriteTea"
#define kFavoriteCandyKey   @"favoriteCandy"
#define kFavoriteGameKey    @"favoriteGame"
#define kFavoriteExcuseKey  @"favoriteExcuse"
#define kFavoriteSinKey     @"favoriteSin"

@interface BIDMainViewController : UIViewController 

@property (weak, nonatomic) IBOutlet UILabel *usernameLabel;
@property (weak, nonatomic) IBOutlet UILabel *passwordLabel;
@property (weak, nonatomic) IBOutlet UILabel *protocolLabel;
@property (weak, nonatomic) IBOutlet UILabel *warpDriveLabel;
@property (weak, nonatomic) IBOutlet UILabel *warpFactorLabel;
@property (weak, nonatomic) IBOutlet UILabel *favoriteTeaLabel;
@property (weak, nonatomic) IBOutlet UILabel *favoriteCandyLabel;
@property (weak, nonatomic) IBOutlet UILabel *favoriteGameLabel;
@property (weak, nonatomic) IBOutlet UILabel *favoriteExcuseLabel;
@property (weak, nonatomic) IBOutlet UILabel *favoriteSinLabel;

- (void)refreshFields;

@end

//
//  BIDMainViewController.m
//  AppSettings
//

#import "BIDMainViewController.h"

@interface BIDMainViewController ()

@end

@implementation BIDMainViewController

- (void)refreshFields {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    self.usernameLabel.text = [defaults objectForKey:kUsernameKey];
    self.passwordLabel.text = [defaults objectForKey:kPasswordKey];
    self.protocolLabel.text = [defaults objectForKey:kProtocolKey];
    self.warpDriveLabel.text = [defaults boolForKey:kWarpDriveKey]
                                ? @"Engaged" : @"Disabled";
    self.warpFactorLabel.text = [[defaults objectForKey:kWarpFactorKey]
                            stringValue];
    self.favoriteTeaLabel.text = [defaults objectForKey:kFavoriteTeaKey];
    self.favoriteCandyLabel.text = [defaults objectForKey:kFavoriteCandyKey];
    self.favoriteGameLabel.text = [defaults objectForKey:kFavoriteGameKey];
    self.favoriteExcuseLabel.text = [defaults objectForKey:kFavoriteExcuseKey];
    self.favoriteSinLabel.text = [defaults objectForKey:kFavoriteSinKey];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self refreshFields];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

  UIApplication *app = [ UIApplication sharedApplication];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:app];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Flipside View

- (void)flipsideViewControllerDidFinish:(BIDFlipsideViewController *)controller
{
    [self refreshFields];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showAlternate"]) {
        [[segue destinationViewController] setDelegate:self];
    }
}

@end

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    [defaults synchronize];
    [self refreshFields];
    
    
    
}
//
//  BIDFlipsideViewController.h
//  AppSettings
//

#import 

@class BIDFlipsideViewController;

@protocol BIDFlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(BIDFlipsideViewController *)controller;
@end

@interface BIDFlipsideViewController : UIViewController

@property (weak, nonatomic) id  delegate;
@property (weak, nonatomic) IBOutlet UISwitch *engineSwitch;
@property (weak, nonatomic) IBOutlet UISlider *warpFactorSlider;

- (void)refreshFields;
- (IBAction)engineSwitchTapped;
- (IBAction)warpSliderTouched;
- (IBAction)done:(id)sender;

@end

//
//  BIDFlipsideViewController.m
//  AppSettings
//

#import "BIDFlipsideViewController.h"
#import "BIDMainViewController.h"

@interface BIDFlipsideViewController ()

@end

@implementation BIDFlipsideViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.

    [self refreshFields];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)refreshFields {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    self.engineSwitch.on = [defaults boolForKey:kWarpDriveKey];
    self.warpFactorSlider.value = [defaults floatForKey:kWarpFactorKey];
    
}

- (IBAction)engineSwitchTapped {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:self.engineSwitch.on forKey:kWarpDriveKey];
}

- (IBAction)warpSliderTouched {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setFloat:self.warpFactorSlider.value forKey:kWarpFactorKey];
}

#pragma mark - Actions

- (IBAction)done:(id)sender
{
    [self.delegate flipsideViewControllerDidFinish:self];
}

@end

Master_Details template

//
//  BKAppDelegate.m
//  Empty-Msater
//
//  Created by xushao on 13-3-25.
//  Copyright (c) 2013年 xushao. All rights reserved.
//

#import "BKAppDelegate.h"

@implementation BKAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    BKViewController *viewController =  [[BKViewController alloc] init];
    
    UINavigationController *controller =  [[UINavigationController alloc] initWithRootViewController:viewController];
    
    self.window.rootViewController = controller;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}
//
//  BKAppDelegate.m
//  Empty-Msater
//
//  Created by xushao on 13-3-25.
//  Copyright (c) 2013年 xushao. All rights reserved.
//

#import "BKAppDelegate.h"

@implementation BKAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    BKViewController *viewController =  [[BKViewController alloc] init];
    
    UINavigationController *controller =  [[UINavigationController alloc] initWithRootViewController:viewController];
    
    self.window.rootViewController = controller;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}
//
//  BKViewController.m
//  Empty-Msater
//
//  Created by xushao on 13-3-25.
//  Copyright (c) 2013年 xushao. All rights reserved.
//

#import "BKViewController.h"
#import "BKDetailsViewController.h"
static NSString *CellIdentifier = @"Cell";


@interface BKViewController ()

@end

@implementation BKViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
        self.title = @"View Controller";
        
        self.navigationItem.leftBarButtonItem = self.editButtonItem;
        
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(dateAdd:)];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellIdentifier];
    

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

-(void)dateAdd:(id)sender{
    
    if (!self.dates) {
        self.dates = [[NSMutableArray alloc] init];
    }
    [self.dates insertObject:[NSDate date] atIndex:0 ];
    NSIndexPath *indexpath = [NSIndexPath indexPathForRow:0 inSection:0];
    
    [self.tableView insertRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationAutomatic];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.dates.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    // Configure the cell...
    
    NSDate *object = self.dates[indexPath.row];
    cell.textLabel.text = [object description];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BKDetailsViewController *detailController = [[BKDetailsViewController alloc] init];
    NSDate *object = self.dates[indexPath.row];
    detailController.message =[object description];
    
    [self.navigationController pushViewController:detailController animated:YES];
}

@end

#import 

@interface BKDetailsViewController : UIViewController

@property(nonatomic, copy)NSString *message;

@end


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 400, 30) ];
    
    [self.view addSubview:label];
    
    label.text = self.message;
	// Do any additional setup after loading the view.
}

IOS深浅拷贝copywithzone的深入分析

Apple官方文档如下:https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Collections/Articles/Copying.html
最近用Core Data做数据管理,Fetch到NSArray类型的结果数组,为了能够进行增、删、改,我把它MutableCopy到一个NSMutableArray数组中。增加、删除用manageModelsContext中的接口操作即可,修改则把NSArray结果中需要修改的实例取出来进行更改,然后用manageModelsContext保存即可完成修改到sqlite文件。
可是问题是我发现用MutableCopy得到的数组中取出的元素直接修改后保存,也能完成修改操作。于是就产生了这样的一个疑问:MutableCopy是浅拷贝??再下来我就发现copy相关的东西我还完全没有搞清楚。
 
首先关于copy和mutableCopy的行为:不管是NSString这种元素类、还是NSArray这样的容器类、还是Mutable和非Mutable类,copy和mutableCopy调用后表现的行为到底是什么样完成取决于类本身NSCopying和NSMutableCopying协议是如何实现的。
想要正常调用copy和mutableCopy两个函数,那么类就一定要实现对应的协议。
1.      元素数据的copy和mutableCopy。
常用的NSString类,示例代码如下:
 
[cpp] view plaincopy 
NSString* string = @”a”;  
NSString* stringCopy = [string copy];// stringCopy与string地址相同,retainCount+ 1  
NSMutableString* stringMCopy = [string mutablecopy];// stringMCopy与string地址不同  
   
NSMutableString* stringM1 = [stringMCopy copy];//地址与stringMCopy不同,且为不可修改  
NSMutableString* stringM2 = [stringMCopy mutablecopy];//地址与stringMCopy不同,可修改   
 
可以基本推出NSString和NSMutableString中两个协议的实现
 
[cpp] view plaincopy 
NSString:  
- (id)copywithZone:(NSZone*)zone  
{  
  return self;  
}  
   
- (id)mutableCopywithZone:(NSZone*)zone  
{  
  NSMutableString* copy =[[NSMutableString alloc] initxxxxxx];  
  ....  
  return copy;  
}  
NSMutableString:  
- (id)copywithZone:(NSZone*)zone  
{  
  NSString* copy = [[NSStringalloc] initxxxxxx];  
  ....  
  return copy;//所以不可修改  
}  
   
- (id)mutableCopywithZone:(NSZone*)zone  
{  
  NSMutableString* copy =[[NSMutableString alloc] initxxxxxx];  
  ....  
  return copy;  
}  

 
2.      容器类的copy和mutableCopy。
常用类NSArray和NSMutableArray,看如下示例代码:
 
[cpp] view plaincopy 
Class1* obj1= ....;//正常初始化  
NSArray* array = [[NSArray alloc] initWithObjects:obj1, nil];  
NSArray* arrayCopy = [array copy];//地址不变,retaincount+1  
NSMutableArray* arrayMCopy = [array mutableCopy];//地址改变,但是数组中成员指针和obj1相同,浅拷贝  
  
NSMutableArray* arrayM1 = [arrayMCopy Copy];//地址改变,但是数组中成员指针和obj1相同,浅拷贝。arrayM1为NSArray不可修改  
NSMutableArray* arrayM2 = [arrayMCopy mutableCopy];//地址改变,但是数组中成员指针和obj1相同,浅拷贝  
[cpp] view plaincopy 
//推断  
[cpp] view plaincopy 
NSArray:  
- (id)copywithZone:(NSZone*)zone  
{  
  //伪码  
  return [self retain];  
}  
  
- (id)mutableCopywithZone:(NSZone*)zone  
{  
  NSMutableArray* copy = [[NSMutableString alloc] initxxxxxx];  
  for (id element in self) {  
    [copy addObject:element];//element retian count + 1  
    ....  
  }  
  return copy;  
}  
  
NSMutableArray:  
- (id)copywithZone:(NSZone*)zone  
{  
  NSArray* copy = [[NSArray alloc] initXXX];  
    
  ....  
  return copy;  
}  
  
- (id)mutableCopywithZone:(NSZone*)zone  
{  
  NSMutableArray* copy = [[NSMutableString alloc] initxxxxxx];  
  for (id element in self) {  
    [copy addObject:element];//element retian count + 1  
    ....  
  }  
  return copy;  
}  

 
3.      深拷贝
上面提到的官方文档中介绍两种实现深拷贝的方法:
a.      用Array的initWithArray:  copyItems函数,如下:
NSArray *deepCopyArray=[[NSArray alloc] initWithArray: someArraycopyItems: YES];
调用后,会对原NSArray中的每个元素调用其copy函数,并把返回的id加入到新的数组中。所以这是依赖于Obj对象类实现的深拷贝,如果- (id)copywithZone:(NSZone*)zone是重新分配一块内存赋值后返回,那么就是真正的深拷贝。如果直接返回自身,那么它只是浅拷贝。
b.      用archiver方式:
NSArray* trueDeepCopyArray = [NSKeyedUnarchiverunarchiveObjectWithData: [NSKeyedArchiver archivedDataWithRootObject:oldArray]];
这是真正意义上的深拷贝,不依赖于实际类Copying协议的实现。
 
4. 用Category实现自定义的深拷贝deepmutableCopy,如:
 
[cpp] view plaincopy 
- (NSMutableArray *)mutableDeepCopy  
{  
    NSMutableArray *ret = [[NSMutableArrayalloc] initWithCapacity:[self count]];  
    for (id value in self)  
    {  
        id oneCopy = nil;  
        if ([value respondsToSelector:@selector(mutableDeepCopy)])  
            oneCopy = [value mutableDeepCopy];  
        else if ([value respondsToSelector:@selector(mutableCopy)])  
            oneCopy = [value mutableCopy];  
        if (oneCopy == nil)  
            oneCopy = [value copy];  
        [ret addObject: oneCopy];  
    }  
    return ret;  
}  

BKMasterViewController

//
//  BKAppDelegate.h
//  Presidents
//
//  Created by xushao on 13-3-21.
//  Copyright (c) 2013年 xushao. All rights reserved.
//

#import 

@interface BKAppDelegate : UIResponder 

@property (strong, nonatomic) UIWindow *window;

@end

//
//  BKAppDelegate.m
//  Presidents
//
//  Created by xushao on 13-3-21.
//  Copyright (c) 2013年 xushao. All rights reserved.
//

#import "BKAppDelegate.h"

@implementation BKAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
    UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
    splitViewController.delegate = (id)navigationController.topViewController;
    return YES;
}
							
- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

//
//  BKMasterViewController.h
//  Presidents
//
//  Created by xushao on 13-3-21.
//  Copyright (c) 2013年 xushao. All rights reserved.
//

#import 

@class BKDetailViewController;

@interface BKMasterViewController : UITableViewController

@property (strong, nonatomic) BKDetailViewController *detailViewController;

@end

//
//  BKMasterViewController.m
//  Presidents
//
//  Created by xushao on 13-3-21.
//  Copyright (c) 2013年 xushao. All rights reserved.
//

#import "BKMasterViewController.h"

#import "BKDetailViewController.h"

@interface BKMasterViewController () {
    NSMutableArray *_objects;
}
@end

@implementation BKMasterViewController

- (void)awakeFromNib
{
    self.clearsSelectionOnViewWillAppear = NO;
    self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
    [super awakeFromNib];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
    self.navigationItem.rightBarButtonItem = addButton;
    self.detailViewController = (BKDetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)insertNewObject:(id)sender
{
    if (!_objects) {
        _objects = [[NSMutableArray alloc] init];
    }
    [_objects insertObject:[NSDate date] atIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _objects.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    NSDate *object = _objects[indexPath.row];
    cell.textLabel.text = [object description];
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [_objects removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
}

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDate *object = _objects[indexPath.row];
    self.detailViewController.detailItem = object;
}

@end

//
//  BKDetailViewController.h
//  Presidents
//
//  Created by xushao on 13-3-21.
//  Copyright (c) 2013年 xushao. All rights reserved.
//

#import 

@interface BKDetailViewController : UIViewController 

@property (strong, nonatomic) id detailItem;

@property (weak, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
@end

//
//  BKDetailViewController.m
//  Presidents
//
//  Created by xushao on 13-3-21.
//  Copyright (c) 2013年 xushao. All rights reserved.
//

#import "BKDetailViewController.h"

@interface BKDetailViewController ()
@property (strong, nonatomic) UIPopoverController *masterPopoverController;
- (void)configureView;
@end

@implementation BKDetailViewController

#pragma mark - Managing the detail item

- (void)setDetailItem:(id)newDetailItem
{
    if (_detailItem != newDetailItem) {
        _detailItem = newDetailItem;
        
        // Update the view.
        [self configureView];
    }

    if (self.masterPopoverController != nil) {
        [self.masterPopoverController dismissPopoverAnimated:YES];
    }        
}

- (void)configureView
{
    // Update the user interface for the detail item.

    if (self.detailItem) {
        self.detailDescriptionLabel.text = [self.detailItem description];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    [self configureView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Split view

- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
{
    barButtonItem.title = NSLocalizedString(@"Master", @"Master");
    [self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
    self.masterPopoverController = popoverController;
}

- (void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
    // Called when the view is shown again in the split view, invalidating the button and popover controller.
    [self.navigationItem setLeftBarButtonItem:nil animated:YES];
    self.masterPopoverController = nil;
}

@end

Navigation Controller-Part3

//
//  BIDPresident.h
//  Nav
//

#import 

@interface BIDPresident : NSObject 

@property (assign, nonatomic) NSInteger number;
@property (copy, nonatomic) NSString *name;
@property (copy, nonatomic) NSString *fromYear;
@property (copy, nonatomic) NSString *toYear;
@property (copy, nonatomic) NSString *party;

@end

//
//  BIDPresident.m
//  Nav
//

#import "BIDPresident.h"

static NSString * const kPresidentNumberKey = @"President";
static NSString * const kPresidentNameKey   = @"Name";
static NSString * const kPresidentFromKey   = @"FromYear";
static NSString * const kPresidentToKey     = @"ToYear";
static NSString * const kPresidentPartyKey  = @"Party";

@implementation BIDPresident

#pragma mark - NSCoding
- (void)encodeWithCoder:(NSCoder *)coder
{
    [coder encodeInt:self.number forKey:kPresidentNumberKey];
    [coder encodeObject:self.name forKey:kPresidentNameKey];
    [coder encodeObject:self.fromYear forKey:kPresidentFromKey];
    [coder encodeObject:self.toYear forKey:kPresidentToKey];
    [coder encodeObject:self.party forKey:kPresidentPartyKey];
}

- (id)initWithCoder:(NSCoder *)coder
{
    if (self = [super init]) {
        self.number = [coder decodeIntForKey:kPresidentNumberKey];
        self.name = [coder decodeObjectForKey:kPresidentNameKey];
        self.fromYear = [coder decodeObjectForKey:kPresidentFromKey];
        self.toYear = [coder decodeObjectForKey:kPresidentToKey];
        self.party = [coder decodeObjectForKey:kPresidentPartyKey];
    }
    return self;
}

#pragma mark - NSCopying

- (id)copyWithZone:(NSZone *)zone;
{
    BIDPresident *copy = [[BIDPresident alloc] init];
    copy.number = self.number;
    copy.name = self.name;
    copy.fromYear = self.fromYear;
    copy.toYear = self.toYear;
    copy.party = self.party;
    return copy;
}

@end

//
//  BIDPresidentsViewController.h
//  Nav
//

#import "BIDSecondLevelViewController.h"
#import "BIDPresidentDetailViewController.h"

@interface BIDPresidentsViewController : BIDSecondLevelViewController


@property (strong, nonatomic) NSMutableArray *presidents;

@end

//
//  BIDPresidentsViewController.m
//  Nav
//

#import "BIDPresidentsViewController.h"
#import "BIDPresident.h"
#import "BIDPresidentDetailViewController.h"

static NSString *CellIdentifier = @"Cell";

@interface BIDPresidentsViewController ()

@end

@implementation BIDPresidentsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"Detail Edit";
        self.rowImage = [UIImage imageNamed:@"detailEditIcon.png"];

        NSString *path = [[NSBundle mainBundle] pathForResource:@"Presidents"
                                                         ofType:@"plist"];
        NSData *data = [[NSData alloc] initWithContentsOfFile:path];
        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
        self.presidents = [unarchiver decodeObjectForKey:@"Presidents"];
        [unarchiver finishDecoding];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [self.tableView registerClass:[UITableViewCell class]
           forCellReuseIdentifier:CellIdentifier];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table View Data Source Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.presidents count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    BIDPresident *president = self.presidents[indexPath.row];
    cell.textLabel.text = president.name;
    
    return cell;
}

#pragma mark - Table View Delegate Methods

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BIDPresident *president = self.presidents[indexPath.row];
    BIDPresidentDetailViewController *controller =
        [[BIDPresidentDetailViewController alloc] init];
    controller.president = president;
    controller.delegate = self;
    controller.row = indexPath.row;
    [self.navigationController pushViewController:controller animated:YES];
}

#pragma mark - President Detail View Delegate Methods

- (void)presidentDetailViewController:(BIDPresidentDetailViewController *)controller
                   didUpdatePresident:(BIDPresident *)president
{
    [self.presidents replaceObjectAtIndex:controller.row withObject:president];
    [self.tableView reloadData];
}

@end

//
//  BIDPresidentDetailViewController.h
//  Nav
//

#import 

@class BIDPresident;
@protocol BIDPresidentDetailViewControllerDelegate;

@interface BIDPresidentDetailViewController : UITableViewController 

@property (copy, nonatomic) BIDPresident *president;
@property (weak, nonatomic) id delegate;
@property (assign, nonatomic) NSInteger row;

@property (strong, nonatomic) NSArray *fieldLabels;

- (IBAction)cancel:(id)sender;
- (IBAction)save:(id)sender;
- (IBAction)textFieldDone:(id)sender;

@end

@protocol BIDPresidentDetailViewControllerDelegate 

- (void)presidentDetailViewController:(BIDPresidentDetailViewController *)controller
                   didUpdatePresident:(BIDPresident *)president;

@end

//
//  BIDPresidentDetailViewController.m
//  Nav
//

#import "BIDPresidentDetailViewController.h"
#import "BIDPresident.h"

#define kNumberOfEditableRows         4
#define kNameRowIndex                 0
#define kFromYearRowIndex             1
#define kToYearRowIndex               2
#define kPartyIndex                   3

#define kLabelTag                     2048
#define kTextFieldTag                 4094

@interface BIDPresidentDetailViewController ()

@end

@implementation BIDPresidentDetailViewController {
    NSString *initialText;
    BOOL hasChanges;
}

- (void)cancel:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)save:(id)sender
{
    [self.view endEditing:YES];
    if (hasChanges) {
        [self.delegate presidentDetailViewController:self
                                  didUpdatePresident:self.president];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)textFieldDone:(id)sender
{
    [sender resignFirstResponder];
    UITextField *senderField = sender;
    NSInteger nextRow = (senderField.superview.tag + 1) % kNumberOfEditableRows;
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:nextRow
                                                inSection:0];
    UITableViewCell *nextCell = [self.tableView cellForRowAtIndexPath:indexPath];
    UITextField *nextField = (id)[nextCell viewWithTag:kTextFieldTag];
    [nextField becomeFirstResponder];
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {
        // Custom initialization
        self.fieldLabels = @[@"Name:", @"From:", @"To:", @"Party:"];
        self.navigationItem.leftBarButtonItem =
            [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
                                                          target:self
                                                          action:@selector(cancel:)];
        self.navigationItem.rightBarButtonItem =
            [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave
                                                          target:self
                                                          action:@selector(save:)];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.allowsSelection = NO;
}


#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return kNumberOfEditableRows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                             CellIdentifier];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:CellIdentifier];
        UILabel *label = [[UILabel alloc] initWithFrame:
                          CGRectMake(10, 10, 75, 25)];
        label.tag = kLabelTag;
        label.textAlignment = NSTextAlignmentRight;
        label.font = [UIFont boldSystemFontOfSize:14];
        [cell.contentView addSubview:label];
        
        UITextField *textField = [[UITextField alloc] initWithFrame:
                                  CGRectMake(90, 12, 200, 25)];
        textField.tag = kTextFieldTag;
        textField.clearsOnBeginEditing = NO;
        textField.delegate = self;
        [textField addTarget:self
                      action:@selector(textFieldDone:)
            forControlEvents:UIControlEventEditingDidEndOnExit];
        [cell.contentView addSubview:textField];
    }
    
    UILabel *label = (id)[cell viewWithTag:kLabelTag];
    label.text = self.fieldLabels[indexPath.row];
    
    UITextField *textField = (id)[cell viewWithTag:kTextFieldTag];
    textField.superview.tag = indexPath.row;
    switch (indexPath.row) {
        case kNameRowIndex:
            textField.text = self.president.name;
            break;
        case kFromYearRowIndex:
            textField.text = self.president.fromYear;
            break;
        case kToYearRowIndex:
            textField.text = self.president.toYear;
            break;
        case kPartyIndex:
            textField.text = self.president.party;
            break;
        default:
            break;
    }
    return cell;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    initialText = textField.text;
}


- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (![textField.text isEqualToString:initialText]) {
        hasChanges = YES;
        switch (textField.superview.tag) {
            case kNameRowIndex:
                self.president.name = textField.text;
                break;
            case kFromYearRowIndex:
                self.president.fromYear = textField.text;
                break;
            case kToYearRowIndex:
                self.president.toYear = textField.text;
                break;
            case kPartyIndex:
                self.president.party = textField.text;
                break;
            default:
                break;
        }
    }
}


@end

Navigation Controller-Part2

获取具体某行的cell用:UITableViewCell *cell = [TableView cellforRowAtIndexPath:indexpath]
Checklist利用的是cell的accessoryType: UITableViewCellAccessoryCheckMark,

nil针对的是object;
NSNotfound针对的是数值;

//
//  BIDCheckListViewController.h
//  Nav
//

#import "BIDSecondLevelViewController.h"

@interface BIDCheckListViewController : BIDSecondLevelViewController

@property (copy, nonatomic) NSArray *snacks;
@property (assign, nonatomic) NSUInteger selectedSnack;

@end

//
//  BIDCheckListViewController.m
//  Nav
//

#import "BIDCheckListViewController.h"

static NSString *CellIdentifier = @"Cell";

@implementation BIDCheckListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"Check One";
        self.rowImage = [UIImage imageNamed:@"checkmarkControllerIcon.png"];
        self.snacks = @[@"Who Hash", @"Bubba Gump Shrimp Étouffée",
            @"Who Pudding", @"Scooby Snacks", @"Everlasting Gobstopper",
            @"Green Eggs and Ham", @"Soylent Green", @"Hard Tack",
            @"Lembas Bread", @"Roast Beast", @"Blancmange"];
        self.selectedSnack = NSNotFound;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [self.tableView registerClass:[UITableViewCell class]
           forCellReuseIdentifier:CellIdentifier];
}

#pragma mark - Table View Data Source Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.snacks count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    cell.textLabel.text = self.snacks[indexPath.row];
    if (self.selectedSnack == indexPath.row) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    return cell;
}

#pragma mark - Table View Delegate Methods

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row != self.selectedSnack) {
        if (self.selectedSnack != NSNotFound) {
            NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:self.selectedSnack
                                                           inSection:0];
            UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:oldIndexPath];
            oldCell.accessoryType = UITableViewCellAccessoryNone;
        }
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        self.selectedSnack = indexPath.row;
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end

//
//  BIDDeleteMeViewController.h
//  Nav
//

#import "BIDSecondLevelViewController.h"

@interface BIDDeleteMeViewController : BIDSecondLevelViewController

@property (strong, nonatomic) NSMutableArray *computers;
// 这里如何改成copy,则运行出错
@end

//
//  BIDDeleteMeViewController.m
//  Nav
//

#import "BIDDeleteMeViewController.h"

static NSString *CellIdentifier = @"Cell";

@implementation BIDDeleteMeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"Delete Me";
        self.rowImage = [UIImage imageNamed:@"deleteMeIcon.png"];
        NSString *path = [[NSBundle mainBundle]
                          pathForResource:@"computers" ofType:@"plist"];
        self.computers = [[NSMutableArray alloc]
                          initWithContentsOfFile:path];
        self.navigationItem.rightBarButtonItem = self.editButtonItem;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [self.tableView registerClass:[UITableViewCell class]
           forCellReuseIdentifier:CellIdentifier];
}

#pragma mark - Table Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
    return [self.computers count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [tableView
                             dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.textLabel.text = self.computers[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.computers removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                     withRowAnimation:UITableViewRowAnimationAutomatic];
}

@end

//
//  BIDRowControlsViewController.h
//  Nav
//

#import "BIDSecondLevelViewController.h"

@interface BIDRowControlsViewController : BIDSecondLevelViewController

@property (copy, nonatomic) NSArray *characters;
- (IBAction)tappedButton:(UIButton *)sender;

@end

//
//  BIDRowControlsViewController.m
//  Nav
//

#import "BIDRowControlsViewController.h"

static NSString *CellIdentifier = @"Cell";

@implementation BIDRowControlsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"Row Controls";
        self.rowImage = [UIImage imageNamed:@"rowControlsIcon.png"];
        self.characters = @[@"R2-D2", @"C3PO", @"Tik-Tok", @"Robby",
            @"Rosie", @"Uniblab", @"Bender", @"Marvin",
            @"Lt. Commander Data", @"Evil Brother Lore", @"Optimus Prime",
            @"Tobor", @"HAL", @"Orgasmatron"];
    }
    return self;
}

- (void)tappedButton:(UIButton *)sender
{
    NSInteger row = sender.tag;
    NSString *character = self.characters[row];
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"You tapped the button"
                          message:[NSString stringWithFormat:
                                   @"You tapped the button for %@", character]
                          delegate:nil
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil];
    [alert show];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [self.tableView registerClass:[UITableViewCell class]
           forCellReuseIdentifier:CellIdentifier];
}

#pragma mark - Table View Data Source Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.characters count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    cell.textLabel.text = self.characters[indexPath.row];
    if (cell.accessoryView == nil) {
        UIImage *buttonUpImage = [UIImage imageNamed:@"button_up.png"];
        UIImage *buttonDownImage = [UIImage imageNamed:@"button_down.png"];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setBackgroundImage:buttonUpImage
                          forState:UIControlStateNormal];
        [button setBackgroundImage:buttonDownImage
                          forState:UIControlStateHighlighted];
        [button setTitle:@"Tap" forState:UIControlStateNormal];
        [button sizeToFit];
        [button addTarget:self
                   action:@selector(tappedButton:)
         forControlEvents:UIControlEventTouchUpInside];
        cell.accessoryView = button;
    }
    cell.accessoryView.tag = indexPath.row;
    
    return cell;
}

#pragma mark - Table View Delegate Methods

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *character = self.characters[indexPath.row];
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"You tapped the row."
                          message:[NSString
                                   stringWithFormat:@"You tapped %@.", character]
                          delegate:nil
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil];
    [alert show];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end

assign: 简单赋值,不更改索引计数
copy: 建立一个索引计数为1的对象,然后释放旧对象
retain:释放旧的对象,将旧对象的值赋予输入对象,再提高输入对象的索引计数为1

Copy其实是建立了一个相同的对象,而retain不是:
比如一个NSString对象,地址为0×1111,内容为@”STR”
Copy到另外一个NSString之 后,地址为0×2222,内容相同,新的对象retain为1, 旧有对象没有变化

retain到另外一个NSString之 后,地址相同(建立一个指针,指针拷贝),内容当然相同,这个对象的retain值+1

也就是说,retain是指针拷贝,copy是内容拷贝。在拷贝之前,都会释放旧的对象。

* 使用assign: 对基础数据类型 (NSInteger)和C数据类型(int, float, double, char,等)
* 使用copy: 对NSString
* 使用retain: 对其他NSObject和其子类

1.readonly表示这个属性是只读的,就是只生成getter方法,不会生成setter方法.
2.readwrite,设置可供访问级别
3.retain,是说明该属性在赋值的时候,先release之前的值,然后再赋新值给属性,引用再加1。
4.nonatomic,非原子性访问,不加同步,多线程并发访问会提高性能。注意,如果不加此属性,则默认是两个访问方法都为原子型事务访问。

retain和copy还有assign的区别

1. 假设你用malloc分配了一块内存,并且把它的地址赋值给了指针a,后来你希望指针b也共享这块内存,于是你又把a赋值给(assign)了b。此时a和b指向同一块内存,请问当a不再需要这块内存,能否直接释放它?答案是否定的,因为a并不知道b是否还在使用这块内存,如果a释放了,那么b在使用这块内存的时候会引起程序crash掉。

2. 了解到1中assign的问题,那么如何解决?最简单的一个方法就是使用引用计数(reference counting),还是上面的那个例子,我们给那块内存设一个引用计数,当内存被分配并且赋值给a时,引用计数是1。当把a赋值给b时引用计数增加到2。这时如果a不再使用这块内存,它只需要把引用计数减1,表明自己不再拥有这块内存。b不再使用这块内存时也把引用计数减1。当引用计数变为0的时候,代表该内存不再被任何指针所引用,系统可以把它直接释放掉。

3. 上面两点其实就是assign和retain的区别,assign就是直接赋值,从而可能引起1中的问题,当数据为int, float等原生类型时,可以使用assign。retain就如2中所述,使用了引用计数,retain引起引用计数加1, release引起引用计数减1,当引用计数为0时,dealloc函数被调用,内存被回收。
4. copy是在你不希望a和b共享一块内存时会使用到。a和b各自有自己的内存。

Navigation Controller–TableView结合

双层的Tableview利用navigation来引导,

在TableView的didSelectRowAtIndexPath利用:[self.navigation pushViewController: animated:];

ddddddddddddddfe

//
//  BIDAppDelegate.m
//  Nav
//

#import "BIDAppDelegate.h"
#import "BIDFirstLevelViewController.h"

@implementation BIDAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    BIDFirstLevelViewController *first = [[BIDFirstLevelViewController alloc] init];
    UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:first];
    self.window.rootViewController = navigation;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

@end
//
//  BIDFirstLevelControllerViewController.h
//  Nav
//

#import 

@interface BIDFirstLevelViewController : UITableViewController

@property (copy, nonatomic) NSArray *controllers;

@end

//
//  BIDFirstLevelControllerViewController.m
//  Nav
//

#import "BIDFirstLevelViewController.h"
#import "BIDSecondLevelViewController.h"
#import "BIDDisclosureButtonViewController.h"
#import "BIDCheckListViewController.h"
#import "BIDRowControlsViewController.h"
#import "BIDMoveMeViewController.h"
#import "BIDDeleteMeViewController.h"
#import "BIDPresidentsViewController.h"

static NSString *CellIdentifier = @"Cell";

@implementation BIDFirstLevelViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
        self.title = @"First level";
        self.controllers = @[
            [[BIDDisclosureButtonViewController alloc] init],
            [[BIDCheckListViewController alloc] init],
            [[BIDRowControlsViewController alloc] init],
            [[BIDMoveMeViewController alloc] init],
            [[BIDDeleteMeViewController alloc] init],
            [[BIDPresidentsViewController alloc] init]
        ];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.tableView registerClass:[UITableViewCell class]
           forCellReuseIdentifier:CellIdentifier];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.controllers count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    // Configure the cell...
    BIDSecondLevelViewController *controller = self.controllers[indexPath.row];
    cell.textLabel.text = controller.title;
    cell.imageView.image = controller.rowImage;
    
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BIDSecondLevelViewController *controller = self.controllers[indexPath.row];
    [self.navigationController pushViewController:controller animated:YES];
}

@end

//
//  BIDSecondLevelViewController.h
//  Nav
//

#import 

@interface BIDSecondLevelViewController : UITableViewController

@property (strong, nonatomic) UIImage *rowImage;

@end

//
//  BIDSecondLevelViewController.m
//  Nav
//

#import "BIDSecondLevelViewController.h"

@implementation BIDSecondLevelViewController

@end

//
//  BIDDisclosureButtonViewController.h
//  Nav
//

#import "BIDSecondLevelViewController.h"

@class BIDDisclosureDetailViewController;

@interface BIDDisclosureButtonViewController : BIDSecondLevelViewController

@property (copy, nonatomic) NSArray *movies;
@property (strong, nonatomic) BIDDisclosureDetailViewController *detailController;

@end

//
//  BIDDisclosureButtonViewController.m
//  Nav
//

#import "BIDDisclosureButtonViewController.h"
#import "BIDDisclosureDetailViewController.h"

static NSString *CellIdentifier = @"Cell";

@implementation BIDDisclosureButtonViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"Disclosure Buttons";
        self.rowImage = [UIImage imageNamed:@"disclosureButtonControllerIcon.png"];

        self.movies = @[@"Toy Story", @"A Bug's Life", @"Toy Story 2",
        @"Monsters, Inc.", @"Finding Nemo", @"The Incredibles",
        @"Cars", @"Ratatouille", @"WALL-E", @"Up",
        @"Toy Story 3", @"Cars 2", @"Brave"];
        self.detailController = [[BIDDisclosureDetailViewController alloc] init];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [self.tableView registerClass:[UITableViewCell class]
           forCellReuseIdentifier:CellIdentifier];
}


#pragma mark - Table View Data Source Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.movies count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    cell.textLabel.text = self.movies[indexPath.row];
    
    return cell;
}

#pragma mark - Table View Delegate Methods

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Hey, do you see the disclosure button?"
                          message:@"Touch that to drill down instead."
                          delegate:nil
                          cancelButtonTitle:@"Won't happen again"
                          otherButtonTitles:nil];
    [alert show];
}

- (void)tableView:(UITableView *)tableView
accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    self.detailController.title = @"Disclosure Button Pressed";
    NSString *selectedMovie = self.movies[indexPath.row];
    NSString *detailMessage = [[NSString alloc]
                               initWithFormat:@"This is details for %@.",
                               selectedMovie];
    self.detailController.message = detailMessage;
    self.detailController.title = selectedMovie;
    [self.navigationController pushViewController:self.detailController
                                         animated:YES];
}

@end

//
//  BIDDisclosureDetailViewController.h
//  Nav
//

#import 

@interface BIDDisclosureDetailViewController : UIViewController

@property (weak, readonly, nonatomic) UILabel *label;
@property (copy, nonatomic) NSString *message;

@end

//
//  BIDDisclosureDetailViewController.m
//  Nav
//

#import "BIDDisclosureDetailViewController.h"

@implementation BIDDisclosureDetailViewController

- (UILabel *)label;
{
    return (id)self.view;
}

- (void)loadView;
{
    UILabel *label = [[UILabel alloc] init];
    label.numberOfLines = 0;
    label.textAlignment = NSTextAlignmentCenter;
    self.view = label;
}

- (void)viewWillAppear:(BOOL)animated;
{
    [super viewWillAppear:animated];
    self.label.text = self.message;
}

@end

@property(strong, nonatomic)UILabel *label;



- (void)viewDidLoad
{
    [super viewDidLoad];
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 30)];
    
    self.label.text = self.message;
    self.label.numberOfLines = 0;
    self.label.textAlignment = NSTextAlignmentCenter;
    self.label.center = self.view.center;
    
    [self.view addSubview:(id)self.label];
	// Do any additional setup after loading the view.
}


@end