2010-01-19 65 views
0

这是我正在开发的应用程序。 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 

请有看看代码并告诉我我做错了什么。我是这个游戏的新手(特别是那些不是单一视图的应用程序)。

回答

1

开始查看代码并耗尽时间。有一些问题。

这个想法是将指向常规数组的指针传递给NewEventViewController,添加NewEventViewController,然后在“完成”关闭NewEventViewController,并用现在修改的例程数组中的数据重新加载UITableView。

在NewEventViewController.h您需要定义的NSMutableArray指向程序数组你FirstViewController.h有

@interface NewEventViewController : UIViewController { 

    IBOutlet UITextField *RoutineTitle; 
    IBOutlet UITextField *RoutineInvolvment; 
    NSMutableArray *routines; 

    } 

    @property(nonatomic, retain) NSMutableArray *routines; 
    -(IBAction)done; 

    @end 

在NewEventViewController.m您需要添加以下内容:

@implementation NewEventViewController

@synthesize routines; 

-(IBAction)done{ 

    // ...you can get the string directly 
[routines addObject:RoutineTitle.text]; 
[self dismissModalViewControllerAnimated:YES]; 


} 


- (void)dealloc { 

[super dealloc]; 
[routines release]; 

} 

添加到FirstViewController如下:

IBOutlet UITableView *myTableView; 

@property (nonatomic, retain) NSMutableArray *routines; 
@property (nonatomic, retain) UITableView *myTableView; 

和FirstViewController,添加以下内容:

@synthesize routines, myTableView; 

    - (void)viewWillAppear:(BOOL)animated 
     { 
     [self.myTableView reloadData]; 

     } 

    - (IBAction)showNewEventViewController {  



NewEventViewController *controller = [[NewEventViewController alloc] initWithNibName:@"NewEventViewController" bundle:nil]; 
controller.routines=routines; 
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 
[self presentModalViewController:controller animated:YES]; 

[controller release]; 
} 

     - (void)dealloc { 
     [routines release]; 
     [myTableView release]; 
     [super dealloc]; 
     } 

确保您删除所有这些东西......不需要。您已经有了一个指向您在推送ViewController时传递的NSMutableArray *例程的指针。

NSString *myString = RoutineTitle.text; 
FirstViewController *FirstView = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil]; 
NSMutableArray *routines; 


NSLog(@"Log the String: %@", myString); 

FirstView.routines = routines; 
+0

我想补充的另一件事。如果你想跟踪多个项目(标题和参与),那么你需要一个数据模型(类)或一个NSMutableDictionary来跟踪你的UITableView中的每个项目。上面的例子使用了单项数组。您可能需要更多的信息来跟踪每次锻炼的细节,或者其他任何内容。 – Jordan 2010-01-19 13:56:46

+0

好的,所以现在所有的工作都正确无误,除了表格视图没有重新加载。 (使用新数据)。一些日志记录显示新项目在例程数组中都有显示,但不在桌面上。 – 2010-01-19 21:56:45

+0

需要检查的两件事:1)您的IBOutlet tableView是否在IB中正确连接? 2)是否从 - (void)viewWillAppear:(BOOL)动画调用[self.myTableView reloadData]? – Jordan 2010-01-20 14:24:09

相关问题