2010-01-19 66 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

相關問題