How to send data to a detail vc from callOutAccessoryControlTapped: method - iphone

I'm using the didSelectRowAtIndexPath: to pass some data from a table vc to a detail vc using the code below that works fine.
What I'm trying to do is to also send this data from a map callOutAccessoryControlTapped: method but I am unsure how to send the data that way.
How would I send
detailViewController.descriptionTextViewString = [[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"short_description"];
using the
callOutAccessoryControlTapped: method?
The objectAtIndex:indexPath.row is unrecognized from within the map callOutAccessoryControlTapped: method.
Here's my didSelectRowAtIndexPath: code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
ScrollView_ExampleViewController *detailViewController = [[ScrollView_ExampleViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
detailViewController.latStr = [[[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"address"] objectForKey:#"lat"];
detailViewController.lngStr = [[[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"address"] objectForKey:#"lng"];
detailViewController.addressStr = [[[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"address"] objectForKey:#"address"];
detailViewController.titleStr = [[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"title"];
detailViewController.mainImageUrl = [[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"image"];
detailViewController.listingId = [[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"id"];
detailViewController.descriptionTextViewString = [[publicDataArray objectAtIndex:indexPath.row] objectForKey:#"short_description"];
[self.navigationController pushViewController:detailViewController animated:YES];
}
Here's my map annotation calloutAccessoryControlTapped: method
- (void)mapView:(MKMapView *)mv annotationView:(MKAnnotationView *)pin calloutAccessoryControlTapped:(UIControl *)control {
ScrollView_ExampleViewController *detailViewController = [[ScrollView_ExampleViewController alloc] initWithNibName:#"ScrollView_ExampleViewController" bundle:nil];
MyAnnotation *theAnnotation = (MyAnnotation *) pin.annotation;
detailViewController.titleStr = theAnnotation.title;
detailViewController.addressStr = theAnnotation.subtitle;
// detailViewController.latStr = theAnnotation.latString;
// detailViewController.lngStr = theAnnotation.lngString;
// detailViewController.url = theAnnotation.theUrl;
[self.navigationController pushViewController:detailViewController animated:YES];
}

You can use the following method to get the tableView's selected index:
NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow];

Related

Open new UIView when select a row

I have a problem that is proving difficult to resolve.
I should open a new UITableView when clicked a cell in the table above.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ChoiceChampionViewController *choiceChampionViewController = [[ChoiceChampionViewController alloc] initWithNibName:#"ChoiceChampionViewController" bundle:nil];
NSString * ind = #".....";
NSString * number = ..;
ind = [ind stringByReplacingOccurrencesOfString:#"XX" withString:number];
[[NSUserDefaults standardUserDefaults] setValue:number forKey:#"Region"];
choiceChampionViewController.url = ind;
[self.view addSubview: choiceChampionViewController.view];
}
ChoiceChampionViewController is structured as follows:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
champion = [[NSMutableArray alloc] init ];
self.navigationItem.title = #"Here is some text to make the navBar big";
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
[self performSelectorOnMainThread:#selector(fetchedData:) withObject:data waitUntilDone:YES];
});
}
- (void)fetchedData:(NSData *)responseData {
Method for process data
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [champion count];
}
- (UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
League * app = [[League alloc] init];
app = [champion objectAtIndex:[indexPath row]];
cell.textLabel.text = app.title;
return cell;
}
But it keeps giving me this error:
[ChoiceChampionViewController tableView:cellForRowAtIndexPath:]: message sent to deallocated instance.
Can we help me?
open that new UITableViewController or UIViewController with use of pushViewController for go in to new UIViewController like bellow...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;{
ChoiceChampionViewController * choiceChampionViewController = [[ChoiceChampionViewController alloc]initWithNibName:#"ChoiceChampionViewController" bundle:nil];
UIBarButtonItem *_backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.backBarButtonItem = _backButton;
[_backButton release],
_backButton = nil;
[self.navigationController pushViewController:choiceChampionViewController animated:YES];
[choiceChampionViewController release];
}
UPDATE :
also use this bellow structure for cellForRowAtIndexPath: method...
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
League * app = [[League alloc] init];
app = [champion objectAtIndex:[indexPath row]];
cell.textLabel.text = app.title;
return cell;
}

How to reload a uitable when the row of a different uitable's row is selected

I'm trying to reload a UITableView when the row of a different UITableView's row is selected. One of the problems I'm having is that both the UITableView's are in the same view. I would like a selection in Table1 to change the NSMutableArray that's used to populate Table2. And then reload the table.
At the moment it works fine, but only when the app re-lauches (or the view is popped off the stack and then re-visited) and viewWillAppear is called again
Here's my code:
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:NO];
[self.navigationController setNavigationBarHidden:YES animated:NO];
self.navigationController.toolbarHidden = YES;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
selectedTableString = [prefs stringForKey:#"selectedTableString"];
if ([selectedTableString isEqualToString:#"Italy"]) {
jsonStringCountry = #"http://****/v1/public/cities?country=it";
}
else if ([selectedTableString isEqualToString:#"Spain"]) {
jsonStringCountry = #"http://****/v1/public/cities?country=es";
}
else if ([selectedTableString isEqualToString:#"UK"]) {
jsonStringCountry = #"http://****/v1/public/cities?country=uk";
}
else if ([selectedTableString isEqualToString:#"Brazil"]) {
jsonStringCountry = #"http://****/v1/public/cities?country=br";
}
NSLog(#"from two t selectedTableString %#",selectedTableString);
// Download the yoodeal JSON
NSString *jsonString = [NSString stringWithContentsOfURL:[NSURL URLWithString:jsonStringCountry] encoding:NSStringEncodingConversionAllowLossy|NSUTF8StringEncoding error:nil];
NSLog(#"jsonStringCountry is %#", jsonStringCountry);
NSMutableArray *itemsTMP = [[NSMutableArray alloc] init];
// Create parser for the yoodeal api
SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *results = [parser objectWithString:jsonString error:nil];
itemsTMP = [results objectForKey:#"results"];
self.displayItems = [itemsTMP copy];
}
My UITableView methods:
- (int)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [displayItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MainCell"];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MainCell"];
}
// Get item from tableData
NSDictionary *item = (NSDictionary *)[displayItems objectAtIndex:indexPath.row];
cell.textLabel.text = [item objectForKey:#"name"];
[cell.textLabel setFont:[UIFont fontWithName: #"Asap-Bold" size: 14.0f]];
return cell;
}
You'll need to use the delegate method of UITablveView. The method is:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
This methods gets called when you click on the row of UITableView. Use this in following manner:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == Table1)
{
//do the changes in array you want to do.
[table2 reloadData];
}
else
{
//do the changes in array you want to do.
[table1 reloadData];
}
}
Create outlet for the table view
[self.yourTableView reloadData];
You should create a property of the UITableView called secondTableView and then use [secondTableView reloadData].
Use [tablename reloadData]; onclick.
When you are updating tableview contents, it will not refresh the view. You can only see changes once the view controller is reloaded.
For updating the table instantly you need to call the above code

Loading an html files to a web view

I have a table view with a lot of items
when i click on a cell in the table it loads a view with a web view outlet, in the web view in viewdidload i have this line of code to load the html file wich is in the project file
NSString *name = pagename.text;
[WebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:name ofType:#"html"]isDirectory:NO]]];
the code to push from the selected cell to that web view with is called Test is
if (indexPath.row == 0)
{
Test *test = [[Test alloc] initWithNibName:nil bundle:nil];
test.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:test animated:YES];
NSString *name1 = #"ba";
[[test pagename] setText:[NSString stringWithString:(NSString *)name1]];
}
else if (indexPath.row == 1)
{
Test *test = [[Test alloc] initWithNibName:nil bundle:nil];
test.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:test animated:YES];
NSString *name1 = #"bb";
[[test pagename] setText:[NSString stringWithString:(NSString *)name1]];
}
else if (indexPath.row == 2)
{
Test *test = [[Test alloc] initWithNibName:nil bundle:nil];
test.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:test animated:YES];
NSString *name1 = #"bc";
[[test pagename] setText:[NSString stringWithString:(NSString *)name1]];
}
the html files are called ba,bb,bc and bd .html
the problem is when i select any cell of the four cells i have in the table it shows the same html file "ba.html"
i tried everything , cleaning the project, deleting the files and copying them back, changed the simulator, restarted the lap and even changed the files names.
any help ?
thanks in advance
update :
This is the hole code
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CustomCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[[cell textLabel] setTextAlignment:UITextAlignmentRight];
cell.textLabel.text = [array objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont systemFontOfSize:18];
cell.textLabel.numberOfLines = 3;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.row == 0)
{
Test *test = [[Test alloc] initWithNibName:nil bundle:nil];
test.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:test animated:YES];
NSString *name1 = #"ba";
[[test pagename] setText:name1];
}
else if (indexPath.row == 1)
{
Test *test = [[Test alloc] initWithNibName:nil bundle:nil];
test.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:test animated:YES];
NSString *name1 = #"bb";
[[test pagename] setText:name1];
}
else if (indexPath.row == 2)
{
Test *test = [[Test alloc] initWithNibName:nil bundle:nil];
test.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:test animated:YES];
NSString *name1 = #"bc";
[[test pagename] setText:name1];
}
else if (indexPath.row == 3)
{
Test *test = [[Test alloc] initWithNibName:nil bundle:nil];
test.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:test animated:YES];
NSString *name1 = #"bd";
[[test pagename] setText:name1];
}
}
You are presenting the controller before setting the page.
Make 'name' an NSString instance variable in your webview class. Make it a property (nonatomic,retain) and synthesize it.
Instead of setting the viewcontroller's label, set name.
Then in viewDidLoad, set the label from name.
Set the label from the name, not the other way around.
In the cell was selected:
Test *test = [[Test alloc] initWithNibName:nil bundle:nil];
test.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
NSString *name1 = #"bc";
[test setName:[NSString stringWithString:(NSString *)name1]];
[self presentModalViewController:test animated:YES];
In the Web view viewdidload
-(void)viewDidLoad {
pagename.text = self.name;
[WebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:name ofType:#"html"]isDirectory:NO]]];
}
Can you put a breakpoint or a log to see if indexPath.row is always 0?
If this is the case, then maybe your table is created in a way that each element is in its own section. You would then have to use indexPath.section as each object would be row 0 of its own section.
Also, I do not know which type of object is 'pagename' but setters generally retain the parameters, so you do not have to create a copy of name1 in
[[test pagename] setText:[NSString stringWithString:(NSString *)name1]];
Note: this really depends on how the property is declared in the class, but typically, those type of properties are retained.

UIPickerView in Detailview with .PLIST

I've searched the web + stackoverflow for a solution.
I've an UITableView with information from a .plist file. The plist file have childs. Like the image.
plist http://www.afbeeldingenuploaden.nl/uploads/648899Schermafbeelding%202011-06-12%20om%2009.50.28.png
When i go to DetailView it will display information out of an UIPickerView that's included in the view. I want to display information from the child in the pickerview, the last level. Like the image.
plist1 http://www.afbeeldingenuploaden.nl/uploads/740501Schermafbeelding%202011-06-12%20om%2012.03.40.png
The problem is that i can't reach the last level from the plist in the UIPickerview with my code.
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSDictionary *dictionary = [tableDataSource objectAtIndex:row];
return [dictionary objectForKey:#"days"];
}
I use in my tableview for the detailview to reach the last level of the plist.
NSString currentLevel
Can anyone help me out with this, i'm stuck.
As I read the plist, it has an array of dictionaries first of which has a dictionary as one of its values at the second array level. Based on that, your method should be,
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSDictionary *dictionary = [tableDataSource objectAtIndex:row];
return [[dictionary objectForKey:#"New item"] objectForKey:#"days"];
}
This will work for the plist in the image. However if more members are added they should adhere to the same structure.
- (void)viewDidLoad {
[super viewDidLoad];
NSString *Path = [[NSBundle mainBundle] bundlePath];
NSString *DataPath = [Path stringByAppendingPathComponent:#"Diyet.plist"];
NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:DataPath];
self.data = tempDict;
[tempDict release];
NSArray *tempArray = [[NSArray alloc] init];
self.tableDataSource = tempArray;
[tempArray release];
self.tableDataSource = [data objectForKey:#"Bitki"];
pickerView.delegate = self;
pickerView.dataSource = self;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// Configure the cell.
cell.textLabel.text = [[tableDataSource objectAtIndex:indexPath.row] objectForKey:#"name"];
cell.textLabel.numberOfLines = 2;
cell.textLabel.font = [UIFont boldSystemFontOfSize:16];
tableView.scrollEnabled = NO;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *dictionary = [tableDataSource objectAtIndex:indexPath.row];
NSArray *modelle = [dictionary objectForKey:#"DETAIL"];
if([modelle count] == 0) {
DetailController *dvController = [[DetailController alloc] initWithNibName:#"DetailController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:dvController animated:YES];
dvController.names.text = [dictionary objectForKey:#"name"];
} else {
BootViewController *rvController = [[BootViewController alloc] initWithNibName:#"BootViewController" bundle:[NSBundle mainBundle]];
rvController.currentLevel += 1;
rvController.currentTitle = [dictionary objectForKey:#"name"];
[self.navigationController pushViewController:rvController animated:YES];
rvController.tableDataSource = modelle;
[rvController release];
}
}

call NSDictionary in current view controller

i have an function in which i am calling values from dictionary that is defined in other view controller. I have to call this method in view didload method of current view controller.
here is my code:
-(void)setData:(NSDictionary *)dic {
self.jsonLabel.text = [dic objectForKey:#"Description"];
self.jsonImage.image = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: [dic objectForKey:#"PictureURL"]]]];}
Try this
NSURL *jsonURL = [NSURL URLWithString:[jsonItem objectForKey:#"PictureURL"]];
Update:
See you allocate the dictionary while the second View controller inside its alloc init method.
Then you can pass the dictionary from the first view controller as
secondViewController.dict = self.dict;
Then you push the second view controller. Check the value of the dictionary in viewDidLoad: Then you can proceed if the dictionary is available.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//make sure jscontroller is allocated before this point.
NSDictionary *tempDict = [[NSDictionary alloc]init];
// set tempDict here with value or object aBook.PictureURL.
jscontroller.jsonItem = tempDict;
[tempDict release];
NSLog(#"json ndic.......%#", jscontroller.jsonItem);
[self.navigationController pushViewController:jscontroller animated:YES];
}
If viewControllerObject is the necessary object of the view controller that defines this function then allocate the object and call like this
- (void)viewDidLoad {
[viewControllerObject setData:urDict];
}
I was using this code in otherviewcontroller
and trying to retrieve in currentViewController
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
JSONViewController *controller = [[JSONViewController alloc] initWithNibName:#"JSONView" bundle:nil];
EmpDictionary=[[NSMutableDictionary alloc]init];
[EmpDictionary setValue:aBook.Description forKey:#"Description"];
[EmpDictionary setValue:aBook.PictureURL forKey:#"PictureURL"];
[EmpDictionary setValue:aBook.WeatherID forKey:#"WeatherID"];
EmpArray=[[NSMutableArray alloc]init];
[EmpArray addObject:EmpDictionary];
NSDictionary *itemAtIndex = [self.EmpArray objectAtIndex:indexPath.row];
[self.navigationController pushViewController:controller animated:YES];
[controller setData:itemAtIndex];}