Using the value of a variable of a viewcontoller from another viewcontroller
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 *al开发者_JAVA百科ert =
[[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!
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!
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/
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!
精彩评论