iOS.階層アーキテクチャ設計.02.同じプロジェクトベースの階層化(iPad対応)

20950 ワード

1、ケース紹介
2、工事構造
3、Main_iPad.storyboard
4、コード
Layered02DetailViewController.h
#import <UIKit/UIKit.h>

#import "Layered02NoteBL.h"

@class Layered02DetailViewController;



@interface Layered02ViewController : UITableViewController



@property (nonatomic,strong) NSMutableArray *listData;



@property (nonatomic,strong) Layered02DetailViewController *detailViewController;



@end

Layered02DetailViewController.m
#import "Layered02ViewController.h"

#import "Layered02DetailViewController.h"



@interface Layered02ViewController ()



@end



@implementation Layered02ViewController



- (void)viewDidLoad

{

    [super viewDidLoad];

    

    // 1、 

    Layered02NoteBL *noteBL = [[Layered02NoteBL alloc] init];

    self.listData = [noteBL findAll];

    

    // 2、 save , 

    [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(reloadView:)

                                                 name:@"reloadViewNotification"

                                               object:nil];

    

    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    

    // 3、for iPad  detailView, 

    self.detailViewController = (Layered02DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];

    

    // 4、for iPad  

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {

        

        if (self.listData.count == 0) {

            

            self.detailViewController.detailDescriptionLabel.text = @"";

        }else{

            self.detailViewController.detailItem = self.listData[0];

        }

    }

    

}



- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

}



#pragma mark -  

-(void)reloadView:(NSNotification*)notification

{

    NSMutableArray *resList = [notification object];

    self.listData  = resList;

    [self.tableView reloadData];

}



#pragma mark table dataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return self.listData.count;

}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    // 1、 Cell

    static NSString *reUseCell = @"reUseCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reUseCell];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reUseCell];

    }

    

    // 2、 Cell 

    Layered02Note *note = [self.listData objectAtIndex:indexPath.row];

    cell.textLabel.text = note.content;

    cell.detailTextLabel.text = [note.date description];

    return cell;

}



#pragma mark  

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

    if ([[segue identifier] isEqualToString:@"showDetail"]) {

        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

        Layered02Note  *note = self.listData[indexPath.row];

        [[segue destinationViewController] setDetailItem:note];

    }

}



#pragma mark table delegate

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{

    return YES;

}



- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        

        Layered02Note *note = [self.listData objectAtIndex:[indexPath row]];

        Layered02NoteBL *bl = [[Layered02NoteBL alloc] init];

        self.listData = [bl remove: note];

        

        // for iPad

        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {

            if (self.listData.count == 0) {

                

                self.detailViewController.detailDescriptionLabel.text = @"";

            }else{

                self.detailViewController.detailItem = self.listData[0];

            }

        }

        

        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

        

    } else if (editingStyle == UITableViewCellEditingStyleInsert) {

        

    }

}



// for iPad

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {

        Layered02Note  *note = self.listData[indexPath.row];

        self.detailViewController.detailItem = note;

    }

}



@end

Layered02DetailViewController.h
#import <UIKit/UIKit.h>

#import "Layered02Note.h"

#import "Layered02AddViewController.h"



@interface Layered02DetailViewController : UIViewController<UISplitViewControllerDelegate>



@property (nonatomic,strong) id detailItem;



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



@end

Layered02DetailViewController.m
#import "Layered02DetailViewController.h"





@interface Layered02DetailViewController ()

// for iPad

@property (strong, nonatomic) UIPopoverController *masterPopoverController;

@end



@implementation Layered02DetailViewController



- (void)viewDidLoad

{

    [super viewDidLoad];

    

    [self configureView];

    

    // for iPad

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {

        UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];

        self.navigationItem.rightBarButtonItem = addButton;

    }

}



// for iPad

- (void)insertNewObject:(id)sender

{

    

    Layered02AddViewController *modalViewController = [self.storyboard

        instantiateViewControllerWithIdentifier:@"addViewController"];

    

    modalViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

    modalViewController.modalPresentationStyle = UIModalPresentationFormSheet;

    [self presentViewController:modalViewController animated:YES completion:nil];

    

}



- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

}



#pragma mark Managing the detail item

-(void)configureView

{

    if (self.detailItem) {

        Layered02Note *note = self.detailItem;

        self.detailDescriptionLabel.text = note.content;

    }

}

// for iPad

- (void)awakeFromNib{

    [super awakeFromNib];

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {

        self.splitViewController.delegate = self;

    }

}

#pragma mark - Split view

// for iPad

- (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;

}

// for iPad

- (void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem

{

    [self.navigationItem setLeftBarButtonItem:nil animated:YES];

    self.masterPopoverController = nil;

}

// for iPad

- (void)setDetailItem:(id)newDetailItem

{

    if (_detailItem != newDetailItem) {

        _detailItem = newDetailItem;

        

        // Update the view.

        [self configureView];

    }

    

    if (self.masterPopoverController != nil) {

        [self.masterPopoverController dismissPopoverAnimated:YES];

    }

}

@end

Layered02AddViewController.h
#import <UIKit/UIKit.h>

#import "Layered02NoteBL.h"



@interface Layered02AddViewController : UIViewController<UITextFieldDelegate>



@property (nonatomic,weak) IBOutlet UITextView *txtView;

-(IBAction)onclickDone:(id)sender;

-(IBAction)onclickSave:(id)sender;



@end

Layered02AddViewController.m
#import "Layered02AddViewController.h"



@interface Layered02AddViewController ()



@end



@implementation Layered02AddViewController



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}



- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

}



- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}















- (IBAction)onclickDone:(id)sender {

    [self dismissViewControllerAnimated:YES completion:nil];

}



- (IBAction)onclickSave:(id)sender {

    

    Layered02NoteBL *bl = [[Layered02NoteBL alloc] init];

    Layered02Note *note = [[Layered02Note alloc] init];

    note.date = [[NSDate alloc] init];

    note.content = self.txtView.text;

    NSMutableArray *reslist = [bl create: note];

    

    [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadViewNotification" object:reslist userInfo:nil];

    [self.txtView resignFirstResponder];

    [self dismissViewControllerAnimated:YES completion:nil];

    

}





-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

{

    if([text isEqualToString:@"
"]) { [textView resignFirstResponder]; return NO; } return YES; } @end