这是我正在开发的应用程序。 http://twitpic.com/yrzpo它有一个模式的观点,我想让用户应用程序的东西到例程列表。 http://twitpic.com/yrzs3从模态视图控制器编辑一个类中定义的NSMutable阵列
第一页上的表视图有一个NSMutableArray的数据源。在第二页上,我想通过输入顶部文本字段添加到该数组中,因此当模式视图弹出时,字段中输入的内容被添加到列表中。
当然有一种方法可以做到这一点。
请记住,我的这个应用程序模板是一个标签栏应用程序。 FirstViewController.h
@interface FirstViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
NSMutableArray *routines;
}
@property (nonatomic, retain) NSMutableArray *routines;
- (IBAction)showNewEventViewController;
@end
FirstViewController.m
#import "FirstViewController.h"
#import "NewEventViewController.h"
@implementation FirstViewController
@synthesize routines;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [routines count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
NSString *cellValue = [routines objectAtIndex:indexPath.row];
[cell.textLabel setText:cellValue];
return cell;
}
- (IBAction)showNewEventViewController {
NewEventViewController *controller = [[NewEventViewController alloc] initWithNibName:@"NewEventView" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:controller animated:YES];
[controller release];
}
- (void)viewDidLoad {
routines = [[NSMutableArray alloc] init];
[routines addObject:@"Hello"];
[routines addObject:@"Temp"];
[routines addObject:@"Temp2"];
[routines addObject:@"Temp3"];
[routines addObject:@"Temp4"];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[routines release];
[super dealloc];
}
@end
NewEventViewController.h
#import <UIKit/UIKit.h>
@interface NewEventViewController : UIViewController {
IBOutlet UITextField *RoutineTitle;
IBOutlet UITextField *RoutineInvolvment;
}
-(IBAction)done;
@end
NewEventViewController.m
#import "NewEventViewController.h"
#import "FirstViewController.h"
@implementation NewEventViewController
-(IBAction)done{
[RoutineTitle resignFirstResponder];
[RoutineInvolvment resignFirstResponder];
NSString *myString = RoutineTitle.text;
FirstViewController *FirstView = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
NSMutableArray *routines;
NSLog(@"Log the String: %@", myString);
FirstView.routines = routines;
[routines addObject:myString];
NSLog(@"Log Array :%@", FirstView.routines);
[self dismissModalViewControllerAnimated:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
请有看看代码并告诉我我做错了什么。我是这个游戏的新手(特别是那些不是单一视图的应用程序)。
我想补充的另一件事。如果你想跟踪多个项目(标题和参与),那么你需要一个数据模型(类)或一个NSMutableDictionary来跟踪你的UITableView中的每个项目。上面的例子使用了单项数组。您可能需要更多的信息来跟踪每次锻炼的细节,或者其他任何内容。 – Jordan 2010-01-19 13:56:46
好的,所以现在所有的工作都正确无误,除了表格视图没有重新加载。 (使用新数据)。一些日志记录显示新项目在例程数组中都有显示,但不在桌面上。 – 2010-01-19 21:56:45
需要检查的两件事:1)您的IBOutlet tableView是否在IB中正确连接? 2)是否从 - (void)viewWillAppear:(BOOL)动画调用[self.myTableView reloadData]? – Jordan 2010-01-20 14:24:09