2012-02-15 77 views
0

我遇到的問題是,在將文本輸入到文本框後,我必須離開頁面並繼續選擇某些選項。然後當(在第二頁上)點擊完成時,您將返回到第一頁。在第一頁上,我想創建一個tablesubview,我可以從第一頁上的文本字段和第二頁上的選項中存儲信息。下面是該方法的代碼...如何使用輸入的文本字段填充NSMutableArray

#import "EnteringCoursesViewController.h" 
#import "SelectRotationController.h" 


@implementation EnteringCoursesViewController 

@synthesize classField; 
@synthesize indicatedClass; 
@synthesize labelClassTitle; 
@synthesize selectRotationController; 
@synthesize classesEntered; 


- (IBAction)chooseType { 
    UIActionSheet *typeSheet = [[UIActionSheet alloc] 
           initWithTitle:@"Class types" 
           delegate:self 
           cancelButtonTitle:nil 
           destructiveButtonTitle:nil 
           otherButtonTitles:@"Core Class", @"Elective", nil]; 
    [typeSheet showInView:self.view]; 
    [typeSheet release]; 
} 

- (void)actionSheet:(UIActionSheet *)typeSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex == 0) { 
     self.indicatedClass = classField.text; 
     NSString *indicatedString = indicatedClass; 
     NSString *greeting = [[NSString alloc] 
           initWithFormat:@"%@ meets 6 times per rotation", indicatedString]; 
     labelClassTitle.text = greeting; 
     labelClassTitle.hidden = NO; 
     [greeting release]; 
     [indicatedClass release]; 
    } 
    else if (buttonIndex == 1) { 
     self.indicatedClass = classField.text; 
     NSString *indicatedString = indicatedClass; 
     NSString *greeting = [[NSString alloc] 
           initWithFormat:@"%@ meets 3 times per rotation", indicatedString]; 
     labelClassTitle.text = greeting; 
     labelClassTitle.hidden = NO; 
     [greeting release]; 
     [indicatedClass release]; 
    } 

} 

- (IBAction)chooseFirstMeeting:(id)sender {  
    SelectRotationController *selectView = [[SelectRotationController alloc] 
               initWithNibName:@"SelectRotationController" 
               bundle:[NSBundle mainBundle]]; 
    [selectView.navigationItem setTitle:@"First Period Day Choose"]; 

    [self.navigationController pushViewController:self.selectRotationController animated:YES]; 
    self.selectRotationController = selectView; 
    [selectView release]; 
} 

- (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)viewDidLoad { 
    NSMutableArray *array = [[NSMutableArray alloc] 
         //help here!// 
    self.classesEntered = array; 
    [array release]; 

    self.navigationItem.hidesBackButton = YES; 
    [super viewDidLoad]; 

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


- (void)dealloc { 
    [classField release]; 
    [labelClassTitle release]; 
    [indicatedClass release]; 
    [selectRotationController release]; 
    [classesEntered release]; 
    [super dealloc]; 
} 


@end 

回答

1

如果我正確理解你的問題,那麼下面將是很好的做法:

在第一個控制器,創建既持有的所有信息的模型對象輸入頁面。在推第二個控制器之前,請將其屬性設置爲模型對象。第二個控制器然後用用戶信息填充模型對象。

模型對象可以是您自己的類,它可以像NSMutableArray一樣簡單,或者它甚至可以是第一個視圖控制器本身。

+0

我想使用NSMutableArray,但我不知道如何從第二個控制器訪問信息......? – mentorship 2012-02-15 17:13:25

+0

將屬性添加到第二個控制器,並使用NSMutableArray進行設置。就像「@property(nonatomic,assign)NSMutableArray * data;」會做。然後,在你推動第二個視圖控制器之前,將其設置爲「secondViewController.data = array;」 – fishinear 2012-02-16 11:00:29