天天看点

通过appdelegate存值,改值,传值--tableview传过去改变后返回来可以用到

在AppDelegate中定义变量
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.books = [NSMutableArray arrayWithObjects:@"ios",@"andrils",@"xml",@"php", nil];
    self.details = [NSMutableArray arrayWithObjects:@"这个很好看",@"这个还行",@"这个不好看",@"我觉得这个正合我意",nil];
    
    return YES;
}
然后可以在其他任何界面取到值,如:从一个界面(这个界面展示数组中的内容)到另一个界面,然后另一个界面改变某个值显示在上一个界面
这里就可以用到上面的方法,在另一个界面改变的其实是上面方法中的数组中的值,然后再第一个界面刷新
下面是调用方法
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    AppDelegate *aappDelegate;
}
---------------------------
aappDelegate = [UIApplication sharedApplication].delegate ;
cell.textLabel.text = [aappDelegate.books objectAtIndex:indexPath.row];
           
下方主要要说明的是在appdelegate拥有viewcontroller,然后viewcontroller拥有数组,从viewcontroller跳转到另一个控制器EditViewController(将viewcontroller的值传过去),然后再editViewcontroller中利用appdelegate改变数组中的值,然后发送过去。间接的通过appdelegate改变值。和上面的基本是一个原理。
           
​@class ViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property(strong,nonatomic)ViewController *viewController;
@property(strong,nonatomic)UINavigationController *naviController;
@end
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.viewController = [[ViewController alloc]initWithStyle:UITableViewStyleGrouped];
    self.naviController = [[UINavigationController alloc]initWithRootViewController:self.viewController];
    self.window.rootViewController = self.naviController;
    [self.window makeKeyAndVisible];
    self.window.backgroundColor = [UIColor whiteColor];
    // Override point for customization after application launch.
    return YES;
}
ViewController
#import <UIKit/UIKit.h>
@interface ViewController : UITableViewController
@property(nonatomic,strong)NSMutableArray *books;
@property(nonatomic,strong)NSMutableArray *details;
@end
#import "ViewController.h"
#import "EditViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize books;
@synthesize details;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    books = [[NSMutableArray alloc]initWithObjects:@"美丽的厉害的 语文讲义",@"美丽的厉害的 数学讲义",@"美丽的厉害的 英语讲义",@"美丽的厉害的 政治讲义",@"美丽的厉害的汉语 讲义",@"美丽的厉害的 日语讲义",@"美丽的厉害的 物理讲义",@"美丽的厉害的 化学讲义",@"美丽的厉害的 哈哈讲义",@"美丽的厉害的噗嗤 讲义",@"美丽的厉害的 搁置讲义", nil];
    details = [[NSMutableArray alloc]initWithObjects:@"厉害",@"我真的好想灾祸五百年",@"这都是爱",@"爱的代价",@"精华总包裹",@"金戈铁马",@"半壁江山",@"芈月传",@"lizilizi",@"我的妈呀",@"", nil];
    self.navigationItem.title = @"图书列表";
}
EditViewController:
   AppDelegate *aoodelegate = [UIApplication sharedApplication].delegate;
        [aoodelegate.viewController.books replaceObjectAtIndex:self.rowNo withObject:self.booktext.text];
        [aoodelegate.viewController.details replaceObjectAtIndex:self.rowNo withObject:self.detailstext.text];