Iphone 切换ViewController时如何保留UITableView内容

Iphone 切换ViewController时如何保留UITableView内容,iphone,uitableview,uiviewcontroller,nsmutablearray,imageview,Iphone,Uitableview,Uiviewcontroller,Nsmutablearray,Imageview,我在这里和其他网站上查阅了许多代码示例,但我找不到与此特定情况相关的示例,因此我想我会尝试询问 我的应用程序正在使用实用程序应用程序模板主视图和翻转侧视图,并启用了自动参考计数弧 在flipside视图中,我有一个空的UITableView,其中有一个导航栏,带有一个“完成”按钮,可返回主视图和一个“添加”按钮。单击Add按钮创建一个空行并激活UIImagePicker。从UIImagePicker中选择照片后,该照片将传递到新单元格的imageView中 我的问题是,在向表中添加新行后,当我切

我在这里和其他网站上查阅了许多代码示例,但我找不到与此特定情况相关的示例,因此我想我会尝试询问

我的应用程序正在使用实用程序应用程序模板主视图和翻转侧视图,并启用了自动参考计数弧

在flipside视图中,我有一个空的UITableView,其中有一个导航栏,带有一个“完成”按钮,可返回主视图和一个“添加”按钮。单击Add按钮创建一个空行并激活UIImagePicker。从UIImagePicker中选择照片后,该照片将传递到新单元格的imageView中

我的问题是,在向表中添加新行后,当我切换到主视图,然后再切换回翻转侧视图时,表再次为空

我认为这与我如何将图像写入NSMutableArray有关

如果有人知道我如何在切换视图时避免UtableView被删除,那就太棒了

提前感谢,, 史蒂芬

AppDelegate.h MainViewController.h FlipsideViewController.h
您需要将选择信息保存在独立于flipview的持久位置。返回flipview时,首先检查是否有保存的选择,然后根据该选择加载表格。

您需要问自己一些问题,因为这是一个逻辑问题

如何在MainViewController中创建FlipsideViewController

您是否每次都创建一个新的,如果是或否,原因是什么

你是如何保存这些数据的

至少有3到4种可能发生的方式,主要是在我们的MainViewController中

如果这些问题对您没有帮助,请发布MainViewController代码

问题在于对象的生命周期

如果您认为用户会经常访问您的FlipsideView,您可以创建它一次,并将其保存在MainView的属性中。 如果没有,您可以在MainView中缓存阵列,并在创建阵列后将其传递给Flipside。 在这两种情况下,如果希望在应用程序启动之间保持持久性,则需要将可变阵列保存到磁盘。如果您将其保存到磁盘,它也可能是直接到磁盘获取信息的另一面

我建议任何一个新的苹果程序员学习和理解内存管理的保留和释放。 因为ARC不是魔法,它代表着自动参考计数。如果您了解了“保留”和“释放”是如何工作的,您将更好地了解ARC是如何工作的,并且能够清楚地看到对象的生命周期。
实际上,ARC正在进行发动机罩下的保留和释放

谢谢您的回复!这是我第一次申请,如果我没有正确回答你的问题,我深表歉意。如何在MainViewController中创建FlipsideViewController?我已经发布了我的AppDelegate和MainViewConroller代码。MainViewController从nib文件加载FlipsideViewController。你是如何保存这些数据的?我在FlipviewController中有一个NSMutableArray,我正在其中存储UITableView数据,但正如我所提到的,我认为我没有正确地写入/管理它,因为在切换视图控制器时会删除这些行。感谢您的帮助!我最终用UITableView放弃了我的反向视图,只使用了一个视图,但您帮助我找到了使用持久性保存数据的正确方法。现在,我不再将从UIImagePickerController拾取的图像存储在视图控制器的数组中,而是将图像保存到应用程序的Documents文件夹中,并将图像名称保存在plist文件中。要加载图像,我从plist文件中读取图像名称,并在Documents文件夹中调用具有相同名称的图像。再次感谢!谢谢你的回复!我理解你提出的概念,但我该如何实施呢?我应该将NSMutableArray存储在单独的类中吗?
#import <UIKit/UIKit.h>

@class MainViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) MainViewController *mainViewController;

@end
#import "AppDelegate.h"

#import "MainViewController.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize mainViewController = _mainViewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.mainViewController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
    self.window.rootViewController = self.mainViewController;
    [self.window makeKeyAndVisible];
    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
#import "FlipsideViewController.h"

@interface MainViewController : UIViewController <FlipsideViewControllerDelegate>

- (IBAction)showInfo:(id)sender;

@end
#import "MainViewController.h"

@implementation MainViewController

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

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

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

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

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

#pragma mark - Flipside View

- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)showInfo:(id)sender
{    
    FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideViewController" bundle:nil];
    controller.delegate = self;
    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:controller animated:YES];
}

@end
#import <UIKit/UIKit.h>

@class FlipsideViewController;

@protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
@end

@interface FlipsideViewController : UIViewController <UIActionSheetDelegate, UIAlertViewDelegate, UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
NSMutableArray *dataArray;
IBOutlet UITableView *tableView;
IBOutlet UIBarButtonItem *addPhotoButton;
UITableViewCell *addedCell;
UIImage *image;
UIImagePickerController *imagePicker;
}

@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) IBOutlet UITableView *tableView;
@property (nonatomic, strong) IBOutlet UIBarButtonItem *addPhotoButton;
@property (nonatomic, strong) UITableViewCell *addedCell;
@property (nonatomic, strong) UIImage *image;
@property (nonatomic, strong) UIImagePickerController *imagePicker;

@property (weak, nonatomic) IBOutlet id <FlipsideViewControllerDelegate> delegate;

- (IBAction)addPhoto:(id)sender;
- (IBAction)done:(id)sender;
@end
#import "FlipsideViewController.h"
#import "MainViewController.h"

@implementation FlipsideViewController

@synthesize delegate = _delegate;
@synthesize dataArray, tableView, addPhotoButton, addedCell, image, imagePicker;

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
[super viewDidLoad];

self.dataArray = [[NSMutableArray alloc] init];
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.allowsEditing = NO;    
self.imagePicker.delegate = self;   
self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}

#pragma mark - UITableView delegate methods

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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *kCellID = @"cellID";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.showsReorderControl = YES;
}

return cell;
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}

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

- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

#pragma mark - Actions

- (IBAction)addPhoto:(id)sender
{
[self presentModalViewController:self.imagePicker animated:YES];
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
// AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
// appDelegate.pickerImage = img;

image = img;
[self dismissModalViewControllerAnimated:YES];

[self.tableView beginUpdates]; 
[self.dataArray addObject:@""];
NSArray *paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:(self.dataArray.count - 1) inSection:0]];
[self.tableView insertRowsAtIndexPaths:paths withRowAnimation:NO];
[self.tableView endUpdates];

addedCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:(self.dataArray.count - 1) inSection:0]];
addedCell.imageView.image = image;
[self.tableView reloadData];
}

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

- (IBAction)done:(id)sender
{
[self.delegate flipsideViewControllerDidFinish:self];
}
@end
- (IBAction)showInfo:(id)sender  // this is trigger by IB
{    
//This is created each time
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideViewController" bundle:nil];  
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

// HERE the 'controller' get retain by self and you don't have reference to it anymore,
[self presentModalViewController:controller animated:YES]; 

// So when the ModalView is finish your controller will be trash with all it's data
// If you don't hold on to something it will go away, thanks to ARC
}