iOS开发之高级视图—— UINavigationController(三)结合编辑功能

   AppDelegate.h

  

//
//  AppDelegate.h
//  UINavigationControllerDemo
//
//  Created by Apple on 16/5/26.
//  Copyright © 2016年 Apple. All rights reserved.
//

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

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) HeroViewController* viewController;
@property (strong, nonatomic) UINavigationController* naviController;

@end
   

     AppDelegate.m


//
//  AppDelegate.m
//  UINavigationControllerDemo
//
//  Created by Apple on 16/5/26.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "AppDelegate.h"
#import "HeroViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    
    // 创建视图控制器
    self.viewController = [[HeroViewController alloc]
                           initWithStyle:UITableViewStyleGrouped];
    
    // 该UINavigationController以self.viewController为视图栈最底层控件
    self.naviController = [[UINavigationController alloc]
                           initWithRootViewController:self.viewController];
    
    // 设置窗口以self.naviController为根视图控制器
    self.window.rootViewController = self.naviController;
    
    [self.window makeKeyAndVisible];
    
    return YES;
}
@end

     HeroViewController.h


//
//  HeroViewController.h
//  UINavigationControllerDemo
//
//  Created by Apple on 16/5/26.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface HeroViewController : UITableViewController
// 定义2个NSMutableArray对象,分别保存英雄名和英雄详情
@property(nonatomic,retain) NSMutableArray* heroes;
@property(nonatomic,retain) NSMutableArray* details;


@end

    HeroViewController.m


//
//  HeroViewController.m
//  UINavigationControllerDemo
//
//  Created by Apple on 16/5/26.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "HeroViewController.h"
#import "EditViewController.h"

@interface HeroViewController ()

@end

@implementation HeroViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 创建、并初始化NSArray对象。
    self.heroes = [NSMutableArray arrayWithObjects:@" 李青",
                  @"提莫", @"杰斯" , @"瑞兹", nil];
    // 创建、并初始化NSArray对象。
    self.details = [NSMutableArray arrayWithObjects:
                    @"一个名叫盲僧的瞎子",
                    @"我不是蘑菇怪,才不是呢!",
                    @"哼,有什么不开心的就打一炮就好了" ,
                    @"我,只是一个会法术的光头罢了", nil];
    // 设置当前视图关联的导航项的标题
    self.navigationItem.title = @"英雄列表";
    
}

// 视图即将可见时调用
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // 刷新数据
    [self.tableView reloadData];
}


#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.heroes count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier"];
    // 如果取出的表格行为nil
    if(!cell)
    {
        // 创建一个UITableViewCell对象,使用UITableViewCellStyleSubtitle风格
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleSubtitle
                reuseIdentifier:@"reuseIdentifier"];
    }
    
    // 从IndexPath参数中获取当前行的行号
    NSUInteger rowNo = indexPath.row;
    // 取出books中索引为rowNo的元素作为UITableViewCell的文本标题
    cell.textLabel.text = [self.heroes objectAtIndex:rowNo];
    // 取出details中索引为rowNo的元素作为UITableViewCell的详细内容
    cell.detailTextLabel.text = [self.details objectAtIndex:rowNo];;
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    
    return cell;
}

// UITableViewDelegate定义的方法,当表格行右边的附件按钮被单击时激发该方法
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
    
    NSInteger rowNo = indexPath.row;
    EditViewController* editController = [[EditViewController alloc]
                                          init];
    // 将被单击表格行的数据传给editController控制器对象
    editController.name = [self.heroes objectAtIndex:rowNo];
    editController.detail = [self.details objectAtIndex:rowNo];
    editController.rowNo = rowNo;
    // 将editController压入UINavigationController管理的控制器栈中
    [self.navigationController pushViewController:editController
                                         animated:YES];
    
    
}


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

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [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;
 }
 */

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

     EditViewController.h


//
//  EditViewController.h
//  UINavigationControllerDemo
//
//  Created by Apple on 16/5/26.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface EditViewController : UIViewController
@property (nonatomic,retain) NSString* name;
@property (nonatomic,retain) NSString* detail;
@property (nonatomic,assign) NSInteger rowNo;


@end

    EditViewController.m


//
//  EditViewController.m
//  UINavigationControllerDemo
//
//  Created by Apple on 16/5/26.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "EditViewController.h"
#import "AppDelegate.h"

@interface EditViewController ()

@end

UITextField* nameField;
UITextView* detailField;


@implementation EditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel* nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 90, 100, 30)];
    nameLabel.text = @"英雄";
    [self.view addSubview:nameLabel];
    
    nameField = [[UITextField alloc] initWithFrame:CGRectMake(80, 90, 200, 30)];
    nameField.text = self.name;
    [nameField setBorderStyle:UITextBorderStyleRoundedRect];
    [self.view addSubview:nameField];
    
    UILabel* detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 120, 100, 50)];
    detailLabel.text = @"信息";
    [self.view addSubview:detailLabel];
    
    detailField = [[UITextView alloc] initWithFrame:CGRectMake(80, 130, 200, 100)];
    detailField.text = self.detail;
    [self.view addSubview:detailField];
    
    // 设置默认不允许编辑
    nameField.enabled = NO;
    detailField.editable = NO;
    // 设置边框
    detailField.layer.borderWidth = 1.5;
    detailField.layer.borderColor = [[UIColor grayColor] CGColor];
    // 设置圆角
    detailField.layer.cornerRadius = 4.0f;
    detailField.layer.masksToBounds = YES;
    
    // 创建一个UIBarButtonItem对象,作为界面的导航项右边的按钮
    UIBarButtonItem* rightBn = [[UIBarButtonItem alloc]
                                initWithTitle:@"编辑"
                                style:UIBarButtonItemStylePlain
                                target:self action:@selector(beginEdit:)];
    self.navigationItem.rightBarButtonItem = rightBn;
    
}
- (void) beginEdit:(id)	sender{
    // 如果该按钮的文本为“编辑”
    if([[sender title] isEqualToString:@"编辑"])
    {
        // 设置nameField、detailField允许编辑
        nameField.enabled = YES;
        detailField.editable = YES;
        // 设置按钮文本为“完成”
        self.navigationItem.rightBarButtonItem.title = @"完成";
    }
    else
    {
        // 获取应用程序委托对象
        AppDelegate* appDelegate = [UIApplication
                                    sharedApplication].delegate;
        // 使用用户在第一个文本框中输入的内容替换viewController
        // 的books集合中指定位置的元素
        [appDelegate.viewController.heroes replaceObjectAtIndex:
         self.rowNo withObject:nameField.text];
        // 使用用户在第一个文本框中输入的内容替换viewController
        // 的details集合中指定位置的元素
        [appDelegate.viewController.details replaceObjectAtIndex:
         self.rowNo withObject:detailField.text];
        // 放弃作为第一响应者
        [nameField resignFirstResponder];
        [detailField resignFirstResponder];
        // 设置nameField、detailField不允许编辑
        nameField.enabled = NO;
        detailField.editable = NO;
        // 设置按钮文本为“编辑”
        self.navigationItem.rightBarButtonItem.title = @"编辑";
    }
    
}


/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

     效果图如下:


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值