使用另一个视图控制器的视图控制器变量的值

发布于 2024-12-09 21:24:08 字数 1311 浏览 0 评论 0 原文

我想使用点击 UITableCellView 后收到的值。我将值存储到数组中,并定义了 UITableCellView 的 ViewController 实例以访问这些数组。尽管我可以从其他 ViewController 访问数组,但它总是将“0”写入我想要获取数据的文本字段。我需要存储在数组中的确切数据。这是我尝试执行此操作的代码;

(fourthViewController)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSString *msg = [[NSString alloc] initWithFormat:
                 @"%@ rate for %@ has been selected",
                 [rates objectAtIndex:[indexPath row]],
                 [countries objectAtIndex:[indexPath row]]];
UIAlertView *alert = 
[[UIAlertView alloc] initWithTitle:@"New country selected"
                           message:msg 
                          delegate:self 
                 cancelButtonTitle:@"OK" 
                 otherButtonTitles:nil];    

[alert show];     
[alert release];
[msg release]; 

fCountry = [countries objectAtIndex:indexPath.row];
fRate = [rates objectAtIndex:indexPath.row];
}

我正在尝试使用数组的 FirstViewController;

@interface FirstViewController : UIViewController{
@public

...

FourthViewController *fourthViewController;
}

...

@property (nonatomic, assign) FourthViewController *fourthViewController;

@synthesize fourthViewController;

感谢您提前的答复!

I want to use the value that I received after tapping a UITableCellView. I stored the value into arrays, and defined the instance of the ViewController of UITableCellView in order to reach those arrays. Even though I can reach the arrays from the other ViewController, it always write "0" as a result to the textfield I want to have the data. I need the exact data stored in the array. Here is the code how I tried to do it;

(fourthViewController)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSString *msg = [[NSString alloc] initWithFormat:
                 @"%@ rate for %@ has been selected",
                 [rates objectAtIndex:[indexPath row]],
                 [countries objectAtIndex:[indexPath row]]];
UIAlertView *alert = 
[[UIAlertView alloc] initWithTitle:@"New country selected"
                           message:msg 
                          delegate:self 
                 cancelButtonTitle:@"OK" 
                 otherButtonTitles:nil];    

[alert show];     
[alert release];
[msg release]; 

fCountry = [countries objectAtIndex:indexPath.row];
fRate = [rates objectAtIndex:indexPath.row];
}

The FirstViewController I'm trying to use the arrays;

@interface FirstViewController : UIViewController{
@public

...

FourthViewController *fourthViewController;
}

...

@property (nonatomic, assign) FourthViewController *fourthViewController;

@synthesize fourthViewController;

Thank you for your answers in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

送你一个梦 2024-12-16 21:24:08

让我澄清一下我做了什么以及我想做什么。

这是我在 FourthViewController 下创建的 UItableView 和 UITableViewCell;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *countryIdentifier = @"countryIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:countryIdentifier];   

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"countryIdentifier"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    NSString *cellValue = [NSString stringWithFormat:@"%@, %@", [countries objectAtIndex:indexPath.row], [rates objectAtIndex:indexPath.row]];
    cell.textLabel.text = cellValue;
    cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:16];
    cell.selectionStyle = UITableViewCellSelectionStyleGray;

    return cell;
}

以下是在 FourthViewController 中点击其中一个单元格后发生的情况;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *msg = [[NSString alloc] initWithFormat:
                     @"%@ rate for %@ has been selected",
                     [rates objectAtIndex:[indexPath row]],
                     [countries objectAtIndex:[indexPath row]]];
    UIAlertView *alert = 
    [[UIAlertView alloc] initWithTitle:@"New country selected"
                               message:msg 
                              delegate:self 
                     cancelButtonTitle:@"OK" 
                     otherButtonTitles:nil];    

    [alert show];     
    [alert release];
    [msg release]; 

    fCountry = [countries objectAtIndex:indexPath.row];
    fRate = [rates objectAtIndex:indexPath.row];
}

fCountry 和 fRate 是我在其中存储点击的单元格数据的字符串。它们被正确合成。我想使用存储在另一个 ViewController(名为 FirstViewController)中的文本字段中的这两个字符串中的单元格信息。

以下是我尝试在 FirstViewController 中使用 FourthViewController 中的字符串的方法;

#import "FourthViewController.h"

@interface FirstViewController : UIViewController{

    ...

    IBOutlet UITextField *taxRateField;
    FourthViewController *fourthViewController;
}

...

@property (nonatomic, retain) IBOutlet UITextField *taxRateField;
@property (nonatomic, assign) FourthViewController *fourthViewController;
 ...

@end

在 FourthViewController.m 中;

#import "FirstViewController.h"
#import "FourthViewController.h"

@implementation FirstViewController

...

@synthesize taxRateField;
@synthesize fourthViewController;

- (void)viewWillAppear:(BOOL)animated {
    ...
    taxRateField.text = fourthViewController.fRate;
}

我实现了所有必要的数据并正确连接了 IBOutlet,但我确信我遗漏了一些非常小的东西。

再次感谢您!

Let me clarify what I did and what I want to do.

Here is my UItableView and UITableViewCell creation under FourthViewController;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *countryIdentifier = @"countryIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:countryIdentifier];   

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"countryIdentifier"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    NSString *cellValue = [NSString stringWithFormat:@"%@, %@", [countries objectAtIndex:indexPath.row], [rates objectAtIndex:indexPath.row]];
    cell.textLabel.text = cellValue;
    cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:16];
    cell.selectionStyle = UITableViewCellSelectionStyleGray;

    return cell;
}

Here is what happens after one of the cell is tapped in FourthViewController;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *msg = [[NSString alloc] initWithFormat:
                     @"%@ rate for %@ has been selected",
                     [rates objectAtIndex:[indexPath row]],
                     [countries objectAtIndex:[indexPath row]]];
    UIAlertView *alert = 
    [[UIAlertView alloc] initWithTitle:@"New country selected"
                               message:msg 
                              delegate:self 
                     cancelButtonTitle:@"OK" 
                     otherButtonTitles:nil];    

    [alert show];     
    [alert release];
    [msg release]; 

    fCountry = [countries objectAtIndex:indexPath.row];
    fRate = [rates objectAtIndex:indexPath.row];
}

fCountry and fRate are the strings that I'm storing the tapped cell data inside. They are synthesized properly. And I want to use the cell info stored into those two strings in a textfield placed in another ViewController, named FirstViewController.

Here is how I tried to use the strings from FourthViewController in FirstViewController;

#import "FourthViewController.h"

@interface FirstViewController : UIViewController{

    ...

    IBOutlet UITextField *taxRateField;
    FourthViewController *fourthViewController;
}

...

@property (nonatomic, retain) IBOutlet UITextField *taxRateField;
@property (nonatomic, assign) FourthViewController *fourthViewController;
 ...

@end

In the FourthViewController.m;

#import "FirstViewController.h"
#import "FourthViewController.h"

@implementation FirstViewController

...

@synthesize taxRateField;
@synthesize fourthViewController;

- (void)viewWillAppear:(BOOL)animated {
    ...
    taxRateField.text = fourthViewController.fRate;
}

I implemented every necessary data and made connections of IBOutlets properly but I'm sure that I'm missing something really small.

Thank you again!

一生独一 2024-12-16 21:24:08

您将整个 ViewController 声明为属性。只需使用您的数组作为属性即可。

一般来说,有一个关于传递数据的好方法: http://www.theappcodeblog.com/2011/04/15/passing-data- Between-views-tutorial-using-a-protocol-delegate-in-your-iphone-app/

You are declaring the whole ViewController as a property. Just use your arrays as properties.

Generally, there is a great tut for passing data: http://www.theappcodeblog.com/2011/04/15/passing-data-between-views-tutorial-using-a-protocol-delegate-in-your-iphone-app/

夜夜流光相皎洁 2024-12-16 21:24:08

为了给您一个明确的答案,我需要有关您的设计的更多信息。让我根据您的代码尝试猜测问题可能是什么 -
“fCountry”和“fRate”是您要访问的属性吗?如果是这样,它们是否正确设置和综合?
您提到您正在尝试从 UITextField 获取数据 - 这是 UITableViewCell 的元素吗?如果是这样,请确保它正确连接到 IBOutlet,尽管此设计中的某些内容对我来说似乎不正确。我建议将您需要的任何数据插入 NSMutableArray 并从那里获取它,而不是尝试从单元格本身获取它(不正确的 MVC)。这可以通过委托给“FourthViewController”轻松完成。希望这能给你一个方向。祝你好运!

In order to give you a clear answer, I will need more information about your design. Let me try and guess, based on your code, what the problem could be -
Are "fCountry" and "fRate" the properties you're trying to access? If so, are they properly set up and synthesized?
You mention you are trying to get data from a UITextField - is this an element of the UITableViewCell? If so, Make sure it is properly connected to an IBOutlet, although something in this design doesn't seem right to me. I would recommend inserting any data you need as it comes into an NSMutableArray and fetch it from there instead of trying to get it from the cell itself (Not proper MVC). This could be done easily by delegation to your "FourthViewController". Hope this gives you a direction. Good luck!

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