使用另一个视图控制器的视图控制器变量的值
我想使用点击 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;
感谢您提前的答复!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
让我澄清一下我做了什么以及我想做什么。
这是我在 FourthViewController 下创建的 UItableView 和 UITableViewCell;
以下是在 FourthViewController 中点击其中一个单元格后发生的情况;
fCountry 和 fRate 是我在其中存储点击的单元格数据的字符串。它们被正确合成。我想使用存储在另一个 ViewController(名为 FirstViewController)中的文本字段中的这两个字符串中的单元格信息。
以下是我尝试在 FirstViewController 中使用 FourthViewController 中的字符串的方法;
在 FourthViewController.m 中;
我实现了所有必要的数据并正确连接了 IBOutlet,但我确信我遗漏了一些非常小的东西。
再次感谢您!
Let me clarify what I did and what I want to do.
Here is my UItableView and UITableViewCell creation under FourthViewController;
Here is what happens after one of the cell is tapped in FourthViewController;
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;
In the FourthViewController.m;
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!
您将整个 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/
为了给您一个明确的答案,我需要有关您的设计的更多信息。让我根据您的代码尝试猜测问题可能是什么 -
“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!