将参数从视图传递回另一个视图的 UITableView 单元格

发布于 2024-11-06 09:42:31 字数 683 浏览 0 评论 0原文

我有两种观点。 第一: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 技术交流群。

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

发布评论

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

评论(3

我不咬妳我踢妳 2024-11-13 09:42:31

在 iPhone SDK 中处理此问题的典型方法是定义委托协议。例如:

@protocol SecondViewControllerDelegate
- (void) viewControllerWillDisappearWithLabelText: (NSString*)text;
@end

然后您将向您的 SecondViewController 添加一个 delegate 属性,例如:

//in the .h file
@interface SecondViewController : UIViewController {
    //declare instance variables
}
@property(nonatomic, assign) id<SecondViewControllerDelegate> delegate;
@end

//in the .m file
@implementation SecondViewController

@synthesize delegate;

//[code...]
@end

然后您将更新 FirstViewController 以实现委托协议:

//in the .h file
@interface FirstViewController : UIViewController<SecondViewControllerDelegate> {
    //[instance variables]
}
//[methods and properties]
@end

//in the .m file
@implementation FirstViewController
//[code...]

- (void) viewControllerWillDisappearWithLabelText: (NSString*)text {
    //do whatever you need to do with the text
}

//[code...]
@end

..并在 FirstViewController 创建 SecondViewController 时设置委托字段:

SecondViewController* sv = [[SecondViewController alloc] init]; 
sv.somestring = someanotherstring;
sv.delegate = self;

最后,在 SecondViewController 中,您实现 viewWillDisappear 为大致如下:

- (void) viewWillDisappear: (bool)animated {
    [super viewWillDisappear:animated];
    if (self.delegate) {
        [self.delegate viewControllerWillDisappearWithLabelText: myLabel.text];
    }
}

The typical way to handle this in the iPhone SDK is to define a delegate protocol. For instance:

@protocol SecondViewControllerDelegate
- (void) viewControllerWillDisappearWithLabelText: (NSString*)text;
@end

Then you would add a delegate property to your SecondViewController, like:

//in the .h file
@interface SecondViewController : UIViewController {
    //declare instance variables
}
@property(nonatomic, assign) id<SecondViewControllerDelegate> delegate;
@end

//in the .m file
@implementation SecondViewController

@synthesize delegate;

//[code...]
@end

Then you would update FirstViewController to implement the delegate protocol:

//in the .h file
@interface FirstViewController : UIViewController<SecondViewControllerDelegate> {
    //[instance variables]
}
//[methods and properties]
@end

//in the .m file
@implementation FirstViewController
//[code...]

- (void) viewControllerWillDisappearWithLabelText: (NSString*)text {
    //do whatever you need to do with the text
}

//[code...]
@end

...and to set the delegate field when FirstViewController creates the SecondViewController:

SecondViewController* sv = [[SecondViewController alloc] init]; 
sv.somestring = someanotherstring;
sv.delegate = self;

Finally, in SecondViewController you implement viewWillDisappear to be roughly like:

- (void) viewWillDisappear: (bool)animated {
    [super viewWillDisappear:animated];
    if (self.delegate) {
        [self.delegate viewControllerWillDisappearWithLabelText: myLabel.text];
    }
}
陌伤ぢ 2024-11-13 09:42:31

是的,有很多简单的方法来处理这个问题......

使用全局变量

您可以在您的 Delegate.h 文件中

@interface Smoke_ApplicationAppDelegate : NSObject {

  UIWindow *window;
  UINavigationController *navigationController;
  NSString *messageString;  //This would be your String Variable
}
 @property(nonatomic,retain)NSString *messageString;

声明您的变量:其次在 Delegate.m 中> 文件

@implementation Smoke_ApplicationAppDelegate

@synthesize window; 
@synthesize navigationController; 
@synthesize messageString; // Synthesize it over here..

这就完成了。现在您可以在您想要的所有/任何类中使用此字符串变量。

使用此全局变量。

只需导入您的委托文件,使其成为 obj...

import "DelegateFile.h"

@implementation About

DelegateFile *appDel;

现在在您的 class.m

-(void)viewDidLoad { [super viewDidLoad];

appDel=[[UIApplication sharedApplication]delegate];

}

现在您可以通过此对象在类中的任何位置访问它:

appDel.messageString

只需按照我的小心步骤,我相信这一定会对您有所帮助......

祝您生活轻松,

Ya , there is much a easy way to handle this.....

You can take a Global Variable

In your Delegate.h file declare your variable:

@interface Smoke_ApplicationAppDelegate : NSObject {

  UIWindow *window;
  UINavigationController *navigationController;
  NSString *messageString;  //This would be your String Variable
}
 @property(nonatomic,retain)NSString *messageString;

Secondly in Delegate.m file

@implementation Smoke_ApplicationAppDelegate

@synthesize window; 
@synthesize navigationController; 
@synthesize messageString; // Synthesize it over here..

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....

import "DelegateFile.h"

@implementation About

DelegateFile *appDel;

Now in Your class.m

-(void)viewDidLoad { [super viewDidLoad];

appDel=[[UIApplication sharedApplication]delegate];

}

Now you can access it anywhere in your class by this Object:

appDel.messageString

Just follow my Steps Carefully , I am sure this is definitely going to help you.....

Have a easy life,

我只土不豪 2024-11-13 09:42:31

在 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.

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