如何使用输入的文本字段填充 NSMutableArray
Posted
技术标签:
【中文标题】如何使用输入的文本字段填充 NSMutableArray【英文标题】:How to populate an NSMutableArray with entered Text Field 【发布时间】:2012-02-15 14:12:26 【问题描述】:我遇到的问题是,在我将文本输入文本字段后,我必须离开页面并继续选择某些选项。然后,当(在第二页上)您单击完成时,您将返回第一页。在第一页上,我想创建一个 tablesubview,我可以在其中存储第一页文本字段和第二页选项中的信息both。下面是方法代码...
#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 一样简单,甚至可以是第一个视图控制器本身。
【讨论】:
我想使用 NSMutableArray 但我不知道如何从第二个控制器访问信息...? 将属性添加到第二个控制器并使用 NSMutableArray 进行设置。类似于“@property (nonatomic, assign) NSMutableArray *data;”会做。然后,就在你推送第二个视图控制器之前,用“secondViewController.data = array;”设置它以上是关于如何使用输入的文本字段填充 NSMutableArray的主要内容,如果未能解决你的问题,请参考以下文章