如何将文本字段数据添加到单元格

发布于 2025-01-07 09:09:32 字数 863 浏览 2 评论 0原文

我有一个数组,现在用它来填充 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬 2025-01-14 09:09:32

我想有两种选择:

  1. 将数据分离到另一个 NSObject 类中,并使其成为两个视图控制器的共享数据模型。当用户输入内容时写入它。
  2. (非常糟糕的选择)保持一切不变,确保第一个控制器中的数组可公开访问,并且只需从另一个控制器获取数据即可。

另外,您声明了一个不可变数组,您将无法更改它。您必须重新创建它或将其声明为可变的。

有关第一个选项的更多信息

为了创建共享模型,您必须创建一个单独的类。
添加一些属性来存储数据,例如

@property (strong, nonatomic) NSMutableArray *userStrings;

添加 @synthesize 并在 init 中初始化它你的新课程的方法。

创建它的一个实例并将其分配给两个视图控制器中的某个属性,例如:

@property (strong, nonatomic) NSObject *dataModel;

分配后,您将能够访问它从你的视图控制器内部。在那里,当用户输入某些内容时,您需要通过添加、更改或删除模型数组中的元素来更新 dataModel 的内容。它将以类似的方式完成:

[self.dataModel setUserStrings:[NSMutableArray arrayWithObjects: [self.textField text], [self.textView text], nil]];
// or
[[self.dataModel userStrings] addObject:[self.textField text]];

这样您就可以拥有共享数据源。

可能的更简单的解决方案

可能是这样的情况,您不需要在两个控制器之间同时共享模型,并且也许您仅在用户完成输入并从第一个控制器创建它后才打开第二个模型。

在这种情况下,您不必创建和共享模型,您只需根据用户输入创建一个数组并将其传递到第二个控制器中即可。您将创建第二个控制器并设置它的属性tableData。当您的下一个控制器出现时,它只会从该属性中读取并显示数据。


如果您需要有关此类方法的更多信息,我建议您观看一些有关在 Objective-C 中使用 MVC 方法的视频教程。例如,斯坦福大学的 iOS 编程课程非常棒。它可以在 iTunes U 中免费获取。

I guess there are two options:

  1. Separate your data in another NSObject class and make it a shared data model for both of your view controllers. Write to it when user inputs something.
  2. (Really bad option) Keep everything as is, make sure the array in the first controller is publicly accessible and simply get the data from another controller.

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 the init 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:

[self.dataModel setUserStrings:[NSMutableArray arrayWithObjects: [self.textField text], [self.textView text], nil]];
// or
[[self.dataModel userStrings] addObject:[self.textField text]];

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文