I am relatively new to IOS development and throughout my application I have been passing a lot of information between my view controller segues. I am currently getting this data from a cloud database and I am worried as time goes on the data I will be passing over my segues will increase as the cloud data increases. Is it a bad idea to pass large amounts of data over these segues, how will it affect the performance of my application if I will eventually pass relatively large arrays over these segues (1,000 to 2,000 elements)? What else can I do if this is a bad idea? Suggestions, tips, pointers would all be great. Thank You
As long as you are just passing the pointer to strong/retaining property this will not have a negative effect on the performance — just a pointer is set, it doesnt matter how the object looks like.
Assume this code:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"otherSegue"]) {
OtherViewController *destViewController = segue.destinationViewController;
destViewController.dataArray = self.downloadedArray;
}
}
As long as the property dataArray on otherViewController is strong or retaining (#property (strong) NSArray *dataArray;), the execution time of that code should not increase with the size of the array, as just a pointer is passed.
I don't really use storyboards, but a quick look on google pointed to something:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showRecipeDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
RecipeDetailViewController *destViewController = segue.destinationViewController;
destViewController.recipeName = [recipes objectAtIndex:indexPath.row];
}
}
Related
I have an UISearchBar in on top of my TVC. If the search is active it displays another tableView on top of the normal tableView (this is normal). Now i need to get the "searchTableView" in prepareForSegue() because I need to call:
var newIndexPath = table.indexPathForSelectedRow!
and this fails if you search something.
I also can't do the decision in didSelectRowAtIndexPath() because the segue is called to fast because it is 'linked' directly to the UITableViewCell. I also tried to create the segue from the ViewController but this also fails because the segue needs to end on the same ViewController again.
So basically I want to ask if anyone knows how to solve the error in the Code line above will doing a search.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath;
if (self.searchDisplayController.active)
{
indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
}
else
{
indexPath = [self.tableView indexPathForSelectedRow];
}
// Use the indexPath...
}
I am trying to pass in IndexPath through a segue though a navigationController, but it crashes on line:
view.selectedAccount = self.selectedAccountRow;
thanks
-(IBAction)changeButtonPressed:(id)sender {
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.accountTableView];
NSIndexPath *indexPath = [self.accountTableView indexPathForRowAtPoint:buttonPosition]
self.indexPath = indexPath;
NSLog(#"%ld",(long)indexPath.row);
}
and
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
[super prepareForSegue:segue sender:sender];
ViewController2 *view = [segue destinationViewController];
view.selectedAccount = self.indexPath;
}
answer here: Set NSString Object, prepareForSegue through UINavigationController
I see many errors:
self.selectedAccountRow never gets defined, change to this:
-(IBAction)changeButtonPressed:(id)sender {
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.accountTableView];
NSIndexPath *indexPath = [self.accountTableView indexPathForRowAtPoint:buttonPosition]
self.selectedAccountRow = indexPath;
}
When using [segue destinationViewController] you need to ensure that the class your assingning is the right one:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
ViewController2 *view = (ViewController2*)[segue destinationViewController];
view.selectedAccount = self.selectedAccountRow;
}
There's no need to declare the as strong, as it is the default one, but it's recommender as it's easier to read, and it's a good coding practice.
#property (strong, nonatomic) NSIndexPath *selectedAccount;
You need to name better your classes and properties, if you declare a viewController subclass, the name should be self explanatory, maybe you can name it detailViewController instead on viewController2.
Also, you should consider using a 2-3 letters prefix for your classes, please read good obj-c practices in here:
Objective-C Good practices
Right click on your view in IB and remove and instances where there is a yellow warning sign. Run and everything should work.
My prepare for segue call keeps causing a crash without any error message. So I use NSLog to track what’s happening. The following line is never reached
NSLog(#"The VCs matched!!: " )
Here is the method
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSLog(#"enter prepare for segue.");
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
if ([segue.identifier isEqualToString:SegueIdentifierA]) {
NSLog(#"Destination to match are: %#, %# .",segue.destinationViewController,[PaperDetailViewController class] );
if ([segue.destinationViewController isKindOfClass:[PaperDetailViewController class]]) {
NSLog(#"The VCs matched!!: " );
PaperDetailViewController *paperDetailView = (PaperDetailViewController *)segue.destinationViewController;
}else NSLog(#"NO ONO NO match for %# AND %#.",segue.destinationViewController,[PaperDetailViewController class] );
}
NSLog(#"exit prepare for segue.");
}
It would be great to see the stack trace as the comments suggest. That would provide valuable clues. Without them, the most likely culprit is:
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
Which is blindly assuming that the sender is a UITableViewCell. If it isn't, you'd see a crash for sure (and we'd see it in the stack trace). If a table selection is what's triggering the segue, than this is safer and easier to read...
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
Given the information all I can recommend is that you specify the class for your view controller in the storyboard editor.
Sorry for the false alarm. There seems to have been a problem with my Xcode. Xcode just crashed and after I restarted it, everything just work as expected. Thanks and +1 to everyone for helping.
I used two tableviewcontrollers,i want to display country names array data in
first tableviewcontroller (recipeBookTableViewController) and other capital array data in second tableviewcontroller (RecipetableviewController),
when a first tableviewcontroller cell is clicked it should move to second tableviewcontroller with capital array data.
I have taken prepareforSegue method to shift to second tableview. but the method is never called.
how to send data to next tableview.
RecipeBookViewController(table1) Recipetableview(table2)
INDIA (cell) ------------> Delhi(cell)
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"showRecipeDetail"])
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
RecipeDetailViewController *destViewController = segue.destinationViewController;
destViewController.second = [onew objectAtIndex:indexPath.row];
NSLog(#"%#",onew);
NSLog(#"data is %#",destViewController.second);
// destViewController.lable2 = [derivationArray objectAtIndex:indexPath.row];
// destViewController.image = [iconArray objectAtIndex:indexPath.row];
}
}
Problem is in creating segue.
Make sure you have created segue from cell of first table to second viewController.
Name the segue as showRecipeDetail.
Look at the image below:
This will probably take two seconds to answer, but my search skills have not gotten me very far on this issue. I am performing a segue but I'm not sure how to grab the id on the destination. Here is my code on the tableview controller.
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath: NSIndexPath *)indexPath{
NSLog(#"reaching accessoryButtonTappedForRowWithIndexPath:");
[self performSegueWithIdentifier:#"leads_calls_to_detail" sender:[[self.leads_calls objectAtIndex:indexPath.row] objectForKey:#"ID"]];
}
What do I have to create on my destination view controller to be able to grab the id that I'm attempting to pass, or is the way I'm performing my segue incompatible with what I am attempting?
You should just pass values to the destinationViewController inside prepareForSegue: and pass self as the sender.. try using something like:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:#"leads_calls_to_detail"])
{
YourViewController *controller=(YourViewController *)segue.destinationViewController;
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
//Or rather just save the indexPath in a property in your currentViewController when you get the accessoryButtonTappedForRowAtIndexPath callback, and use it here
controller.yourPropertyToSet = [self.leads_calls objectAtIndex:path.row];
}
}
And also according to Rob Mayoff, you can grab the index path for the row that the accessory was tapped at by using something like this:
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];//where sender is the sender passed in from prepareForSegue:
How to find indexPath for tapped accessory button in tableView