如何将文本字段数据添加到单元格
我有一个数组,现在用它来填充 2 个单元格的文本。在不同的视图中,我有一个文本字段、一个文本视图等。 如何将用户在文本字段中输入的数据移动到不同控制器中的数组中。
这是我所拥有的:
- (void)viewDidLoad
{
tabledata = [[NSArray alloc] initWithObjects:@"Franklin", @"delossantos", nil];
[super viewDidLoad];
}
这是文本字段的视图控制器:
NewEntryController.m
#import "NewEntryViewController.h"
@implementation NewEntryViewController
@synthesize titleTextfield;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
#pragma mark - View lifecycle
- (void)viewDidUnload
{
[self setTitleTextfield:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
@end
I have an array which I'm using to populate 2 cells with text for now. In a different view I have a textfield, a textview etc..
How can I move the data input by user in the textfield into an array in a different controller.
here's what I have:
- (void)viewDidLoad
{
tabledata = [[NSArray alloc] initWithObjects:@"Franklin", @"delossantos", nil];
[super viewDidLoad];
}
Here is the view controller of the textfields:
NewEntryController.m
#import "NewEntryViewController.h"
@implementation NewEntryViewController
@synthesize titleTextfield;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
#pragma mark - View lifecycle
- (void)viewDidUnload
{
[self setTitleTextfield:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想有两种选择:
NSObject
类中,并使其成为两个视图控制器的共享数据模型。当用户输入内容时写入它。另外,您声明了一个不可变数组,您将无法更改它。您必须重新创建它或将其声明为可变的。
有关第一个选项的更多信息
为了创建共享模型,您必须创建一个单独的类。
添加一些属性来存储数据,例如
@property (strong, nonatomic) NSMutableArray *userStrings;
添加
@synthesize
并在init
中初始化它你的新课程的方法。创建它的一个实例并将其分配给两个视图控制器中的某个属性,例如:
@property (strong, nonatomic) NSObject *dataModel;
分配后,您将能够访问它从你的视图控制器内部。在那里,当用户输入某些内容时,您需要通过添加、更改或删除模型数组中的元素来更新
dataModel
的内容。它将以类似的方式完成:这样您就可以拥有共享数据源。
可能的更简单的解决方案
可能是这样的情况,您不需要在两个控制器之间同时共享模型,并且也许您仅在用户完成输入并从第一个控制器创建它后才打开第二个模型。
在这种情况下,您不必创建和共享模型,您只需根据用户输入创建一个数组并将其传递到第二个控制器中即可。您将创建第二个控制器并设置它的属性
tableData
。当您的下一个控制器出现时,它只会从该属性中读取并显示数据。如果您需要有关此类方法的更多信息,我建议您观看一些有关在 Objective-C 中使用 MVC 方法的视频教程。例如,斯坦福大学的 iOS 编程课程非常棒。它可以在 iTunes U 中免费获取。
I guess there are two options:
NSObject
class and make it a shared data model for both of your view controllers. Write to it when user inputs something.Also, you declare an immutable array, you won't be able to change it. You will have to either recreate or declare it as mutable.
More on the first option
In order to create a shared model, you have to create a separate class.
Add some property to store data there, e.g.
@property (strong, nonatomic) NSMutableArray *userStrings;
Add
@synthesize
and initialise it in theinit
method of your new class.Create an instance of it and assign it to some property in both view controllers, say, to:
@property (strong, nonatomic) NSObject *dataModel;
After you assign it, you will be able to access it from inside your view controllers. There, when your user inputs something, you will need to update contents of your
dataModel
by adding, changing or removing elements in the model's array. It will be done in some similar way:This way you can have a shared datasource.
Possible easier solution
It may be the case that you don't need the model to be shared simultaneously between two controllers, and maybe you open second one only after user has finished inputing and you create it from your first controller.
In that case you don't have to create and share the model, you can just create an array based on user input and pass it in your second controller. You would create that second controller and set it's property
tableData
. When your next controller appears it'd just read from that property and show the data.If you need more info on such approaches, I'd suggest you watch some video tutorials on using MVC approach in Objective-C. For example, Stanford's course on iOS Programming is awesome. It is available for free in the iTunes U.