我在两个视图之间进行通信时遇到问题。我的目标是向NSMutableArray
添加一个新对象。感谢stackoverflow上可爱的人们修复了。现在我有另一个问题。
即使我能够将对象添加到我的NSMutableArray
,该表也不会更新自己以使用新数据重新填充。这是一些代码:
@interface FirstViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>{
IBOutlet UITableView *mytableview;
NSMutableArray *mytableinfo;
@property (nonatomic,retain) IBOutlet UITableView *mytableview;
@property (nonatomic,retain) NSMutableArray *mytableinfo;`
-(IBAction)addShift:(id)sender{
SecondViewController *secondViewController = [[SecondViewController alloc]init];
[self presentModalViewController:secondViewController animated:YES];
}
- (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 [mytableinfo count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
cell.textLabel.text =[mytableinfo objectAtIndex:indexPath.row];
NSLog(@"Data was written to table");
return cell;
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
mytableinfo = [[NSMutableArray alloc]init];
mytableview = [[UITableView alloc]init];
[mytableinfo addObject:@"Hello world1"];
[self.mytableview reloadData];
}
- (void)viewDidLoad
{
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addShift:)];
self.navigationItem.rightBarButtonItem = addButton;
[super viewDidLoad];
UIBarButtonItem *refresh = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(reloadTableData:)];
self.navigationItem.leftBarButtonItem = refresh;
mytableinfo = [[NSMutableArray alloc]init];
[mytableinfo addObject:@"First Shift"];
[self.mytableview reloadData];
// Do any additional setup after loading the view from its nib.
}
#import "SecondViewController.h"
#import "FirstViewController.h"
-(IBAction)saveShift:(id)sender{
FirstViewController *firstViewController = [[FirstViewController alloc]init];
firstViewController.mytableinfo = [[NSMutableArray alloc]init];
[firstViewController.mytableinfo addObject:@"SecondViwController Dismissed"];
NSLog(@"%@",[firstViewController.mytableinfo objectAtIndex:0]);
[self dismissModalViewControllerAnimated:YES];
}
因此,当saveShift
被调用时,我应该回到主视图 - 我这样做 - 然后重新填充我的tableview
。这就像我摆脱了一个小问题只是跳进另一个 - 不是一个非常好的感觉!
干杯,
萨姆
答案 0 :(得分:2)
在您的saveShift:(id)sender
方法中,您需要编写很多代码。
只需执行以下操作:
[self.myTableInfo addObject:@"SecondViewConroller dimissed"];
[self dismissModalViewControllerAnimated:YES];
在该代码中,我假设您已在第二个视图中传递了对NSMutableArray的引用。
我记得你从NSMutableArray发来的帖子,你正在从FirstView呈现SecondView(如果我的记忆力很好)。
但是在你的代码中,在第二个视图的saveShift
中,你正在创建一个brand new
FirstView,它与你来自哪里不一样。因此,当您关闭模态视图时,您的“全新”firstView会丢失并返回原始的firstView。最后一个人没有听说过你的对象。
嗨Sam,你 看看我在你的NSMutableArray线程中给你的最后一个答案,几乎你需要解决这个问题。
好的,这里有更多的快速修复,以便可以正常工作
// SecondViewController.m
#import "SecondViewController.h"
#import "FirstViewController.h"
@implementation SecondViewController
@synthesize dateformatter,mydatepicker,startingTime;
// HERE Add a new property that match this and a variable in the .h
@synthesize array4Test;
// HERE make an init that will make this UIViewController know about your NSMutableArray
- (id)initWithMutableArray:aMutableArray
{
self = [self initWithNibName:nil bundle:nil];
if (self)
{
self.array4Test = aMutableArray;
}
return self;
}
- (void)dealloc
{
// HERE clean up
self.array4Test = nil;
[super dealloc];
}
-(IBAction)saveShift:(id)sender{
// HERE remove this code
//FirstViewController *firstViewController = [[FirstViewController alloc]init];
//[firstViewController.mytableinfo addObject:@"Hello world"];
//NSLog(@"%@",[firstViewController.mytableinfo objectAtIndex:0]);
// HERE add an object to the mutableArray that is store in your firstViewController, the one you've passed the reference in
[self.array4Test addObject:@"This is a String"];
[self dismissModalViewControllerAnimated:YES];
}
我想我刚刚添加了一个NSLog()。
// FirstViewController.m
#import "FirstViewController.h"
#import "SecondViewController.h"
@implementation FirstViewController
@synthesize mytableinfo,mytableview;
@synthesize goneToOtherView;
-(IBAction)addShift:(id)sender{
SecondViewController *secondViewController = [[SecondViewController alloc] initWithMutableArray:self.mytableinfo];
[self presentModalViewController:secondViewController animated:YES];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.mytableview reloadData];
NSString *aString = [mytableinfo lastObject];
if (aString)
{
NSLog(@"This just came back from the second View\n%@", aString);
}
}
答案 1 :(得分:2)
这里至少有几个问题。
第一个问题:您在FirstViewController
中创建了一个全新的saveShift:
,而不是在现有FirstViewController
中添加对象。那不行。在addShift:
中,当您创建secondViewController
时,您应该将现有的FirstViewController
作为参数传递,如下所示:
SecondViewController *secondViewController = [[SecondViewController alloc] initWithFirstViewController:self];
您需要修改SecondViewController
以使用initWithFirstViewController:
方法,该方法应将FirstViewController
存储在名为firstViewController
的属性中。
第二个问题:你没有告诉关于新行的表格视图。您应该向FirstViewController
添加方法:
- (void)reallyAddShift:(NSString *)newShift
{
[self.mytableinfo addObject:newShift];
NSIndexPath ip = [NSIndexPath indexPathForRow:[self.mytableinfo count]-1 inSection:0];
[self.mytableview insertRowsAtIndexPaths:[NSArray arrayWithObject:ip] withRowAnimation: UITableViewRowAnimationFade];
}
然后,在saveShift:
中,你可以这样做:
-(IBAction)saveShift:(id)sender{
[self.firstViewController reallyAddShift:@"SecondViwController Dismissed"];
[self dismissModalViewControllerAnimated:YES];
}
答案 2 :(得分:1)
简单地分配mytableinfo = [[NSMutableArray alloc]init];
将不会保留它。制作self.mytableinfo = [[NSMutableArray alloc]init];
。