将参数从视图传递回另一个视图的 UITableView 单元格
我有两种观点。 第一:FirstViewController 第二:SecondViewController
FirstViewController 是我的 UINavigationController 的根控制器,在 FirstViewController 中我有 UITableView。当单击 UITableView 中的单元格时,视图将导航到 SecondViewController。在 SecondViewController 里面我有 UILabel。我想将此 UILabel 的值分配给在导航栏中单击“后退”按钮时在 FirstViewController 中单击的单元格。我应该做什么来实现这个?
我可以通过创建以下内容将值从 FirstViewController 传递给 SecondViewController:
SecondViewController *sv; sv.somestring = someanotherstring;
但无法在 SecondViewController 中实现此操作以将值传递给 FirstViewController 中的 NSString。
你能帮我吗?
谢谢。 AE
i have got two view.
First: FirstViewController
Second: SecondViewController
FirstViewController is my UINavigationController's root controller and inside FirstViewController I ve got UITableView. When a cell is clicked in UITableView, the view is navigated to SecondViewController. Inside SecondViewController i have UILabel. I want to assign this UILabel's value to the cell which is clicked at FirstViewController when Back button is clicked in Navigation Bar. What am i supposed to do to implement this?
I can pass value to SecondViewController from FirstViewController by creating:
SecondViewController *sv;
sv.somestring = someanotherstring;
but can not implement this at SecondViewController to pass the value to a NSString in FirstViewController.
Can u help me please?
Thank you.
ae
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 iPhone SDK 中处理此问题的典型方法是定义委托协议。例如:
然后您将向您的 SecondViewController 添加一个
delegate
属性,例如:然后您将更新
FirstViewController
以实现委托协议:..并在
FirstViewController
创建SecondViewController
时设置委托字段:最后,在
SecondViewController
中,您实现viewWillDisappear
为大致如下:The typical way to handle this in the iPhone SDK is to define a delegate protocol. For instance:
Then you would add a
delegate
property to yourSecondViewController
, like:Then you would update
FirstViewController
to implement the delegate protocol:...and to set the delegate field when
FirstViewController
creates theSecondViewController
:Finally, in
SecondViewController
you implementviewWillDisappear
to be roughly like:是的,有很多简单的方法来处理这个问题......
使用全局变量
您可以在您的 Delegate.h 文件中
声明您的变量:其次在 Delegate.m 中> 文件
这就完成了。现在您可以在您想要的所有/任何类中使用此字符串变量。
使用此全局变量。
只需导入您的委托文件,使其成为 obj...
现在在您的 class.m
现在您可以通过此对象在类中的任何位置访问它:
只需按照我的小心步骤,我相信这一定会对您有所帮助......
祝您生活轻松,
Ya , there is much a easy way to handle this.....
You can take a Global Variable
In your Delegate.h file declare your variable:
Secondly in Delegate.m file
This is Done .Now you can use this String Variable in All/any class you want..
To use this Global Variable.
Just import you Delegate file make the obj of it....
Now in Your class.m
Now you can access it anywhere in your class by this Object:
Just follow my Steps Carefully , I am sure this is definitely going to help you.....
Have a easy life,
在 appdeleage 中声明一个字符串 (stringVal) 并将其属性设置为非原子并保留,也合成它。在第二个视图控制器中,您可以将标签值设置为 appdelegate string([appdelegate setStringVal:label.text];) 。可以在第一个视图控制器中获取该值并在 table(NSString *localString=appdelegate.stringVal];) 中使用它。
一切顺利。
Declare a string (stringVal)in the appdeleage and set its propert as nonatomic and retain, synthesize it also.In the second view controller you can set the label value to the appdelegate string([appdelegate setStringVal:label.text];) .You can get this value in the first view controller and use it in table(NSString *localString=appdelegate.stringVal];).
All the best.