I have got response from services and I have got latitude value and I copied into an array.
Now I want to pass the array when I click the next views row.
I passed places earlier. In second view UITableViewController is there if I click the particular row then it should be printed the rows text value and particular text's latitude value.....?
Is this possible...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *myText=[places objectAtIndex:indexPath.row];
NSLog(#"his is selected lattidude value:%#",myText);}
Here I am printing the row's text with this I need to print the latitude value of each one....please help me.
There are lot many way to pass data from one view to other view.
Best for you is like:
Just create one variable in your second view controller.
When you push navigation from current page just before assign your value to that variable.
now you can easily use that value in your second view.
in your SecondViewController in .h file just
#interface SecondViewController : UIViewController{
NSString *strLatLong;
}
#property (nonatomic, retain) NSString *strLatLong;
#end
and in .m file just synthesize it like bellow..
#synthesize *strLatLong
and in your FirstViewController class just push with bellow code
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *myText=[places objectAtIndex:indexPath.row];
NSLog(#"his is selected lattidude value:%#",myText);
SecondViewController *objSecondView = [[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
objSecondView.strLatLong = myText;
[objSecondView.strLatLong retain];
[self.navigationController pushViewController:objSecondView animated:YES];
[objSecondView release];
}
i hope this help you...
Create #property in second viewcontroller class .h file
#property (nonatomic ) NSString *StrName;
#synthesize in second viewcontroller class .m file
#synthesize StrName;
after that in your current viewcontroller class
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//NSString *myText=[places objectAtIndex:indexPath.row];
secondviewcontroller *ss=[[secondviewcontroller alloc]initWithNibName:#"secondviewcontroller" bundle:nil];
ss.StrName=[places objectAtIndex:indexPath.row];
[self presentModalViewController:ss animated:YES];
}
you must create a variable in second class and define his property and on first class you use this property to send one value to next class you sed value like this
in first class you create a variable with property like this
#property(nonatomic ,strong) NSString *str;
and in first class when you create the object of second class then you send the value like this
SecondClass *secondClass=[[SecondClass alloc]init];
secondClass.str=#"value is here";
in this method you send value to next class
i hope you understand ;-)
Related
I have a NSManaged Object that contains several string fields, like :
#interface myObject : NSManagedObject
#property (nonatomic, copy) NSString * name0;
#property (nonatomic, copy) NSString * name1;
#property (nonatomic, copy) NSString * name2;
I have a table view controller that displays the properties of this object and when someones clicks on a cell, it calls a second view controller (EditTableViewController below) that will allow to modify this particular cell. I would like to pass this second controller the NSString* it needs to modify (depending on the cell clicked).
Therefore I tried something like this for the second controller :
#interface EditTableViewController : UITableViewController
#property (nonatomic) NSString *value;
and in the implementation part I have (among others) :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
EditPropertyCell *cell = [tableView dequeueReusableCellWithIdentifier:#"EditPropertyCell"];
cell.valueTextField.text = self.value;
cell.valueTextField.delegate = self;
[cell.valueTextField becomeFirstResponder];
[cell.valueTextField setReturnKeyType:UIReturnKeyDone];
return cell;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
self.value = textField.text;
[self.navigationController popViewControllerAnimated:TRUE];
return YES;
}
The first controller owns a reference to an object of type myObject
#interface myObjectDetailTableViewController : UITableViewController
#property (nonatomic, strong) myObject * myObject;
and calls this second controller through :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
EditTableViewController *etvc = [[EditTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
if ([indexPath row] ==0) {
etvc.value = self.myObject.name0;
}
if ([indexPath row] ==1) {
etvc.value = self.myObject.name1;
}
if ([indexPath row] ==2) {
etvc.value = self.myObject.name2;
}
[self.navigationController pushViewController:etvc
animated:YES];
}
The problem it that it doesn't work, i.e. when I am in the EditTableViewController I can correctly see the value I want to modify (e.g. name0) but then if I modify it is not saved. I guess the NSString* has been copied somewhere but I can't figure out where. I wonder whether it has do with NSManagedObject ?
Anyway, maybe the way I am trying to do this is completely inappropriate in which case I would also be happy to know what would be the best approach to do it
(in particular, later on I would like ideally to be able to also set fields that were set to nil that way).
You can't just pass the string, because you need to tell the managed object that the string is changing and you do that by calling the managed object setter method for that property.
A better option is to pass both the managed object and the string which is the key that should be changed. Your controller can then use the key to get the existing value to display and set the value for that key to update the managed object (which then needs to be saved to store the update).
This question already has answers here:
Passing data between view controllers
(45 answers)
Closed 8 years ago.
This is a Q&A Style Post
If I want to pass parameters, such as an NSString, NSArray, or NSDictionary, from one view controller to another, what is the easiest way to do this?
There are multiple ways to achieve this, but two of the cleanest methods would involve passing the parameter(s) into a custom init method for the next view controller, or setting the parameter(s) as a property of the next view controller.
Note that this is not restricted to passing data between view controllers - view controllers are objects and any objects can use the following principles, I'm simply using view controllers for the following examples.
Both of these examples are using a simple UITableViewController as the initial view controller, and the next view controller will be passed the user's selection from a list of cells where they can choose their favorite color, as well as the current date of their selection. This can be done FROM any type of view controller TO any type of view controller with a few minor modifications, and within reason there's really no limit to the types/quantity of information that can be passed this way.
keep in mind that you likely don't want a massive initializer method with 10 parameter names, so in that case you might want to have individual properties and set each one accordingly. You also might want to keep the initialization/setup code to a single line if there's only a few parameters, so using a custom initializer method might be for you in that case.
Demo table view setup
#import "TestTableViewController.h"
#import "NextViewController.h"
#interface TestTableViewController ()
#end
#implementation TestTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.colors = #[#"Red", #"Orange", #"Yellow", #"Green"];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.colors.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ColorCell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"ColorCell"];
}
cell.textLabel.text = self.colors[indexPath.row];
return cell;
}
Method #1 - Custom Initializer
NextViewController.h
#import <UIKit/UIKit.h>
#interface NextViewController : UIViewController
// expose the custom initializer so other view controller's can pass in the parameters we want to pass
- (instancetype)initWithFavoriteColor:(NSString *)favoriteColor currentDate:(NSDate *)date;
#end
NextViewController.m
#import "NextViewController.h"
#implementation NextViewController
// implement the custom initializer method
- (instancetype)initWithFavoriteColor:(NSString *)favoriteColor currentDate:(NSDate *)date {
self = [super init];
// do whatever you want here with the favorite color string and current date that
// was passed in, such as save them to instance variables...
return self;
}
#end
Implement method #1 in our demo table view:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// get the color they selected for this row from our data source array
NSString *selectedColor = self.colors[indexPath.row];
// initialize the next view controller using the handy init method we created
NextViewController *nextVC = [[NextViewController alloc] initWithFavoriteColor:selectedColor currentDate:[NSDate date]];
[self.navigationController pushViewController:nextVC animated:YES];
}
Method #2 - Creating/Setting Properties
NextViewController.h
#import <UIKit/UIKit.h>
#interface NextViewController : UIViewController
#property (nonatomic, retain) NSString *favoriteColor;
#property (nonatomic, retain) NSDate *currentDate;
#end
NextViewController.m
#import "NextViewController.h"
#implementation NextViewController
- (void)viewDidLoad {
[super viewDidLoad];
// do something here with your properties - by the time the view has loaded
// they will have been set/passed from the original view controller
self.favoriteColor...
self.currentDate...
}
#end
Implement method #2 in our demo table view:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// get the color they selected for this row from our data source array
NSString *selectedColor = self.colors[indexPath.row];
// initialize the next view controller normally, and set its favorite color property
// to be what the user selected
NextViewController *nextVC = [NextViewController new];
nextVC.favoriteColor = selectedColor;
nextVC.currentDate = [NSDate date];
[self.navigationController pushViewController:nextVC animated:YES];
}
In both instances, you have now quickly and easily passed multiple pieces of information from one view controller to another.
i have this simple code in the didSelectRowAtIndexPath method:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {
NSString *selected = #"test";
NSLog(#"You choose: %#", selected);
}
This is my ViewController.h:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
IBOutlet UITableView *tableData;
}
#end
When i run the App, the TableView display all data, but then i cliked in a cell the method above doesn't run (?)
Have you set the delegate for the tableview in your storyboard, select your table view right click and drag to the view controller and you should see the option to set dataSource delegate and tableviewdelegate - I forget the exact names.
e.g.: thats part of my code, but should show you what you need to do. You need to hand over the data somewhere.
NSDictionary *oneDict = [_noteBookArray objectAtIndex:indexPath.row];
NSString *touchString = [oneDict objectForKey:#"value"];
NSString *dateString = [oneDict objectForKey:#"timestamp"];
//call the method who does it
[_noteBookViewController setContentAndTimeStampWith:touchString and:dateString];
//set the present View Controller active (the controller who contains the values)
[self presentViewController:_noteBookViewController animated:YES completion:nil];
method from the notebookviewcontroller
- (void)setContentAndTimeStampWith:(NSString *)contentString and:(NSString *)timeStampString
{_textInputView.text = contentString;
_timeStampString = timeStampString;}
got it?
hi I'm new to iOS development. in my application i have fetch datas form outside database and i viewed in tableview so now i have created on detailviewcontroller. to give clear view about the data but its not working.
the code i used.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
detailviewpoliticalViewController *dvc =[[detailviewpoliticalViewController alloc]initWithNibName:#"dvc" bundle:nil];
fieldpolitical * ff =[eventarray objectAtIndex:indexPath.row];
dvc.eve =ff.title;
[self.navigationController pushViewController:dvc animated:YES];
}
this is the declaration and and initialization of the data in .h file
#import <UIKit/UIKit.h>
#interface detailviewpoliticalViewController : UIViewController
#property (nonatomic,strong) NSString *eve;
#property (strong, nonatomic) IBOutlet UILabel *event;
#end
in .m file in viewload
event.text = self.eve;
I think ff.title is not getting any value, because rest of the things you are doing is fine. Please try to print ff.title before passing it to next view.
First check dvc is your nib(xib) name.
Make exact nib name i thing your nib name should be detailviewpoliticalViewController. i checked from starting if your xib name is correct then tell me i will check next next lines
detailviewpoliticalViewController *dvc =[[detailviewpoliticalViewController alloc]initWithNibName:#"dvc" bundle:nil];
I searched extensively for delegation tutorials, but could not get it to work. Here is the story and the code.
DetailViewController has a UITextField and a UIButton. When you press the button you get to another PricelistViewController with a simple sectioned table. Tapping a row in that table should return the text from the row's title to the first view and insert it into the text field. But it doesn't. Here is the code:
PricelistViewController.h (the second view):
#class PricelistViewController;
#protocol PricelistViewControllerDelegate <NSObject>
#required
//- (void)theSaveButtonOnThePriceListWasTapped:(PricelistViewController *)controller didUpdateValue:(NSString *)value;
- (void)theSaveButtonOnThePriceListWasTapped:(NSString *)value;
#end
#interface PricelistViewController : UITableViewController
#property (nonatomic, weak) id <PricelistViewControllerDelegate> delegate;
#property (retain, nonatomic) NSMutableArray * listOfSections;
#end
PricelistViewController.m
#synthesize delegate;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dictionary = [listOfSections objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:#"ProductSections"];
NSString *selectedProduct = [array objectAtIndex:indexPath.row];
[self.delegate theSaveButtonOnThePriceListWasTapped:[NSString stringWithFormat:#"%#", selectedProduct]];
[self.navigationController popViewControllerAnimated:YES];
}
This is the code in DetailViewController.h (the first view with a text field and the button):
#import "PricelistViewController.h"
#interface DetailViewController : UIViewController <UITextFieldDelegate, PricelistViewControllerDelegate>
DetailViewController.m (this I how I am trying to change the text in the field):
- (void)theSaveButtonOnThePriceListWasTapped:(NSString *)value
{
NSLog(#"Text, sent here: %#", value);
NSLog(#"Text was sent here.");
self.detailDescriptionLabel.text = value;
}
detailDescriptionLabel is the UITextField for the text to display.
Can somebody check the code and help? I work on this matter two days with no luck!
Firstly why are you forward referencing (#class) your class in the header file of the PriceListViewController? This isn't needed.
Secondly are you using ARC, if you are your array property should be of type nonatomic, strong. If you're not your delegate property should be nonatomic, assign. You seem to be mixing your terminology
Also where and how are you initialising your array?
I figured it out.
In the PricelistViewController.m I changed the way I call the method (added If statement):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dictionary = [listOfSections objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:#"ProductSections"];
NSString *selectedProduct = [array objectAtIndex:indexPath.row];
if (delegatePrice && [delegatePrice respondsToSelector:#selector(theSaveButtonOnThePriceListWasTapped:didUpdateValue:)])
{
[delegatePrice theSaveButtonOnThePriceListWasTapped:self didUpdateValue:selectedProduct];
}
AND THE MOST IMPORTANT: I refused to use the Storyboard and instead made a old good .xib file for my PricelistViewController view and it worked right away!