iOS.分层架构设计.01.基于同一工程的分层(仅支持iphone)

1、案例介绍,如下图:

2、工程如下图:

3、Main_iPhone.storyboard

4、代码

 Layered01ViewController.h

#import <UIKit/UIKit.h>
#import "Layered01NoteBL.h"
@class Layered01DetailViewController;

@interface Layered01ViewController : UITableViewController

@property (nonatomic,strong) NSMutableArray *listData;

@property (nonatomic,strong) Layered01DetailViewController *detailViewController;

@end

Layered01ViewController.m

#import "Layered01ViewController.h"
#import "Layered01DetailViewController.h"

@interface Layered01ViewController ()

@end

@implementation Layered01ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 1、初始化数据
    Layered01NoteBL *noteBL = [[Layered01NoteBL alloc] init];
    self.listData = [noteBL findAll];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(reloadView:)
                                                 name:@"reloadViewNotification"
                                               object:nil];
    
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    
}

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








#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数据
    Layered01Note *note = [self.listData objectAtIndex:indexPath.row];
    cell.textLabel.text = note.content;
    cell.detailTextLabel.text = [note.date description];
    return cell;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        Layered01Note  *note = self.listData[indexPath.row];
        [[segue destinationViewController] setDetailItem:note];
    }
}
#pragma mark - 处理通知
-(void)reloadView:(NSNotification*)notification
{
    NSMutableArray *resList = [notification object];
    self.listData  = resList;
    [self.tableView reloadData];
}


- (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) {
        
        Layered01Note *note = [self.listData objectAtIndex:[indexPath row]];
        Layered01NoteBL *bl = [[Layered01NoteBL alloc] init];
        self.listData = [bl remove: note];
        
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        
    }
}


@end

Layered01DetailViewController.h

#import <UIKit/UIKit.h>
#import "Layered01Note.h"

@interface Layered01DetailViewController : UIViewController

@property (nonatomic,strong) id detailItem;

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

@end

Layered01DetailViewController.m

#import "Layered01DetailViewController.h"


@interface Layered01DetailViewController ()

@end

@implementation Layered01DetailViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self configureView];
}

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

-(void)configureView
{
    if (self.detailItem) {
        Layered01Note *note = self.detailItem;
        self.detailDescriptionLabel.text = note.content;
    }
}

@end

Layered01AddViewController.h

#import <UIKit/UIKit.h>
#import "Layered01NoteBL.h"

@interface Layered01AddViewController : UIViewController<UITextFieldDelegate>

@property (nonatomic,weak) IBOutlet UITextView *txtView;
-(IBAction)onclickDone:(id)sender;
-(IBAction)onclickSave:(id)sender;

@end

Layered01AddViewController.m

#import "Layered01AddViewController.h"

@interface Layered01AddViewController ()

@end

@implementation Layered01AddViewController

- (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 {
    
    Layered01NoteBL *bl = [[Layered01NoteBL alloc] init];
    Layered01Note *note = [[Layered01Note 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
原文地址:https://www.cnblogs.com/cqchen/p/3783086.html