处理字符串对象和视图控制器
我试图在视图控制器之间来回传递字符串,因此例如,一旦我单击第一个视图中的选项卡栏按钮(+),第二个视图就会打开(PresentModalViewController)并且它有一个文本字段。因此,我输入的任何内容都会将其放入字符串中(该字符串是第一个视图的对象),并且我尝试将该字符串附加到第一个视图中加载的表视图中。
注意: 我的字符串对象声明如下
View1.h
NSString *string
@property (copy) NSString *string;
View1.m
@synthesize string;
在 View 2 中,我传递像这样
View2.m 的
View1 *view1 = [[View1 alloc] initWithNibName:@"View1" bundle:nil];
view1.string = [[NSString alloc] initWithFormat:@"%@", TextField.text];
textField 值问题 - 当我 NSLog 时View2 中的这个值,它从文本字段中获取值,但为了让我加载上一个视图,我需要关闭这个 View2。因此,当我尝试访问视图 1 中的同一字符串对象时,一旦此 View2 被关闭。它说该字符串对象为 null。
问题 - 有人可以告诉我
1. 关闭视图 2 后如何将文本字段值从视图 2 获取到视图 1(关闭时是否真的使其所有对象都为空?)
2. 如何将该字符串附加到 NSMutableArray 的最后一个索引?
I am trying to pass a string back and forth between the view Controllers, so for example as soon as I click on a tab bar button (+) in the first View, second view opens (PresentModalViewController) and it has a Text Field. So anything I type, I take it into a string(this string is an object of the first view) and I am trying to append that string to a tableview loaded in the first View.
Note: My string object is declared like this
View1.h
NSString *string
@property (copy) NSString *string;
View1.m
@synthesize string;
And in the View 2 I am passing the textField Value like this
View2.m
View1 *view1 = [[View1 alloc] initWithNibName:@"View1" bundle:nil];
view1.string = [[NSString alloc] initWithFormat:@"%@", TextField.text];
Problem - When I NSLog this value inside the View2, it grabs the value from the Text Field but in order for me to load the previous view, I need to dismiss this View2. So as soon as this View2 is dismissed when I try to access the same string object in my view 1. It says the string object is null.
Question - Could someone tell me
1. How to get the text Field value from view 2 to view 1 after dismissing View 2 (does it really makes all its objects null when dismissed?)
2. How to append that string to the last index of a NSMutableArray?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个非常好的问题,当我开始为 iOS 编写代码时,我也很难弄清楚。基本上,您不需要初始化新的 view1,因为选项卡控制器已经在其
viewControllers
属性中保存了 view1 对象。另外,在这种情况下,没有必要分配/初始化字符串。因此,您可能需要将其更改为:
更改为这样的:
甚至:
第 2 部分:
This is a very good question that I also had trouble figuring out when I started coding for the iOS. Basically, you don't need to initialize a new view1 because the tabbar controller already holds the view1 object in its
viewControllers
property. Also, alloc/init'ing the string in not necessary in this situation.Therefore, you would want to change this:
To something like this:
Or even:
Part 2:
如果您将字符串发送到的 ViewController 是 tabBarController 的基本控制器,@chown 的答案肯定会起作用。
如果您深入到 NavigationController 堆栈的几个级别,那么您将需要不同的方法。
我建议的方法是创建一个
协议
。您可以在此处创建 view2 的委托
,以便在视图关闭之前将字符串传递回堆栈。Apple 文档和 Internet(包括 StackOverflow)上都有大量此代码的示例,但这里有一个快速运行...
在 View2.h 中
在 View2.m 中,您可以在任何您喜欢的地方调用该委托方法,但这里是一个例子:
然后在 View1.h
和 View1.m
中另一个选择是发布通知,但这更多的是广播场景而不是目标消息,所以我不会为此发布示例代码。
The answer from @chown will definitely work if the ViewController you're sending the string to is the base controller of a tabBarController.
If you were several levels deep into a NavigationController stack, then you'll need a different approach.
The approach I'd recommend would be to create a
protocol
. This is where you create adelegate
of view2 to pass the string back down the stack before the view is dismissed.There are loads of examples of this code both in the Apple Documentation and on the Internet (StackOverflow included) but here's a quick run down...
In View2.h
In View2.m you can call that delegate method where ever you like but here's an example:
Then in View1.h
and View1.m
Another option would be to post a notification, but that is more a broadcast scenario than a targeted message so I won't bother posting example code for that.