Delay in passing data using segue Objective-C - ios

I'm trying to pass the storeId to another viewController using segue, however, there seems to be a delay when passing data? Data is "nil" at first, I have to tap it again in order it to pass the correct data.
Here is my code
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.passStoreData = self.storeList [indexPath.row];
self.passStoreId = [self.storeList [indexPath.row] valueForKey:#"storeId"];
self.passStoreName = [self.storeList [indexPath.row] valueForKey:#"storeName"];
NSString *store = [NSString stringWithFormat:#"%# : %#", self.passStoreId, self.passStoreName];
[self.storeButton setTitle:[NSString stringWithFormat:#"%#", store] forState:UIControlStateNormal];
storeTable.hidden = YES;
[self performSegueWithIdentifier:#"showOrder" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
OrderListViewController *orderListVc = [segue destinationViewController];
orderListVc.theStoreId = self.passStoreData;
}
What can I do to avoid the delay in passing the data using segue? Thank you.

By the time you call [segue destinationViewController], the OrderListViewController is loaded and ready to use. So any code in viewDidLoad will already have been called, way before you set theStoreId.
Move your view setup code to viewWillAppear: and it should work.

After trying and researching. I found the answer to this.
I used dispatch_async(dispatch_get_main_queue() to run it on main thread and the passing of data through segue was not delayed. Thank you

Related

Perform segue is not having any impact

I have following code :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
indexPathRow = indexPath.row;
NSLog(#"indexPathRow.%d", indexPathRow);
mylist.recordIdToEdit = [[myIdArray objectAtIndex:indexPathR NSLog(#"Item selected..%d", safetyInventoryList.recordIdToEdit);
[self performSegueWithIdentifier:#"DetailsViewController" sender:nil];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"DetailsViewController"])
{
DetailsViewController *destViewController = segue.destinationViewController;
int itemIdToFetch = myList.recordIdToEdit;
[detailedList fetchDetails:itemIdToFetch];
destViewController.name = detailedList.itemName;
destViewController.itemId = itemIdToFetch;
}
}
my segue identifier is correct and perform segue works fine if i dont have any processing in my preparefor segue! Also, i tried to move the fetching data from DB to detailed view controller instead of prepareforsegue, in its viewdidload. that also has no effect. the details view controller is not shown and no error is thrown. Program halts there
!
Remove line
[self performSegueWithIdentifier:#"DetailsViewController" sender:nil];
from didSelectRowAtIndexPath and try.
Make sure your segue has identifier name DetailsViewController you can check this by selecting segue and select Attribute Inspector StoryBoard segue identifier name.
I think You should try to fetch data from DB to detailed view controller but in different thread,
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
dispatch_async(queue, ^{
});
});
Hope it will solve your issue.

performSegueWithIdentifier doesn't show new view

I'm having an issue with my performSegueWithIdentifier, because he fires and when I get a breakpoint on the viewDidLoad of the NextViewController he enters and executes fine. Problem is there is no output and the screen continues to show the CurrentViewController. What can be the error ?
(CurrentViewController is a TableViewController)
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_selected = [self.evlist objectAtIndex:indexPath.row];
[self performSegueWithIdentifier:#"eventlist" sender:self];
return indexPath;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSString * event_id = [_selected objectForKey:#"id"];
NSString * event_name = [_selected objectForKey:#"label"];
[segue.destinationViewController setEvent_id:event_id];
[segue.destinationViewController setEvent_id:event_id];
[segue.destinationViewController setEvent_name:event_name];
}
(NextViewController is a ViewController)
- (void)viewDidLoad
{
[super viewDidLoad];
_evname_label.text = _event_name;
_evid_label.text = _event_id;
NSLog(#"This is strange %#",_event_name);
}
He logs everything as expected... the view doesn't show!
Your code appears correct. Are you sure the segue is configured correctly? Check the Style, Presentation and Transition styles.
Sorry but I had a bug in my coding... I was calling the prepareforsegue inside a for cycle and the problem was somewhere around it.
Prepareforsegue outside for and it was cool :) Thanks all

Using PFQueryForTableViewController and capturing information for segue at table cell

so I am using a PFQueryForTableViewController class connected to my storyboard. I use the queryForTable method to get the information in the back, but the information is in a different order every time that the user queries for the table. When the user presses on the cell it segues to another viewController. I need to retain the information at that cell when the user presses the cell to fill in the information on the next view controller. I am currently using the tableView: didSelectRowAtIndexPath: object: method, as well as the prepareForSegue: method. I am not sure how to retain the information, and with the code I am currently running, I am getting the error:
"'NSInvalidArgumentException', reason: '-[UITableViewCell objectAtIndex:]: unrecognized selector sent to instance 0x9b6e420'"
Here is the code for the segue:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[super tableView:tableView didSelectRowAtIndexPath:indexPath];
self.selectedCell = indexPath;
[self performSegueWithIdentifier:#"profile" sender:[self.objectId objectAtIndex:self.selectedCell.row]];
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"profile"]) {
ProfileViewController *userInfo = [segue destinationViewController];
userInfo.userInfo = [NSMutableArray arrayWithArray:sender];
}
}
Using sender for passing this information is not good practice. Instead, I would use a private property in the table view and setting this when you select the row.
In didSelectRowAtIndexPath:
self.currentProfile = [self objectAtIndexPath:indexPath];
[self performSegueWithIdentifier:#"profile" sender:self];
Then, in prepareForSegue:
ProfileViewController *userInfo = [segue destinationViewController];
userInfo.user = self.currentProfile; // Or, if you want to pass user details as array, create array here

prepareForSegue getting called twice, with Attempt to present <UINavigationController> while presentation is in progress

I am new to ios programming and asking here, but I visit all the time! I am stumped at why I am getting this problem, it compiles with no errors and I have checked and checked all my outlets and identifiers in my MainStoryboard.
I have 2 UITableViewControllers, I am passing a string from the first to the second when the user selects an item in the table, so in
FirstTableViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int sel = indexPath.row;
if (sel == 0) {
_keyName = [NSString stringWithString:_string1];
NSLog(#"the table was selected at cell 0, %#", _string1);
}
if (sel == 1) {
_keyName = [NSString stringWithString:_string2];
}
// more code below...
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"ResultsSegue"])
{
UINavigationController *navigationController = segue.destinationViewController;
ResultsViewController *rv = [[navigationController viewControllers] objectAtIndex:0];
[rv setResults: _keyName];
NSLog(#"in the progress view, %#", _keyName);
//rv.delegate = (id)self;
rv.delegate = self;
}
}
And in my ResultsViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"in the results, %#", _results);
NSLog(#"in the results view");
}
In the NSlog readout I get:
...
in the progress view, (null)
in the results, (null)
in the progress view, The Right String
Warning: Attempt to present on
Then when I hit the cancel button to return to the firstTableview and press the detail view again it no longer shows null..
in the progress view, The Right String
in the results, The Right String
in the progress view, The Right String
The problem is prepareForSegue is called before didSelectRowAtIndexPath. You should just eliminate the didSelectRowAtIndexPath method, and do everything in prepareForSegue. You can use the following line to get the indexPath you need:
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
Rob's answer helped me, as well - thanks! I'm coding in Swift, so for those who run into this while Swifting, here's how to get the index (or index row) clicked on in Swift 3:
var rowClicked = (self.tableView.indexPathForSelectedRow?.row)!
May be your segue is connection start form the table view cell instead of view controller.
I had same issue I removed the segue from the table view cell and added back at view controller level.

UITableView only sending data to Detail View Controller on Second Try

Hey there I have been working on a transit app for some time and have been stuck with this issue for a while now.
I am using iOS 5 and a storyboard. Basically I have a UITableView that displays favorite bus stop locations, when a user selects a row I use:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Favorite *favorite = [self.favoriteItems objectAtIndex:indexPath.row];
stopString = favorite.title;
routeString = favorite.subtitle;
}
With the stop and route information of the cell the user chose I then prepare for a segue that corresponds to a segue on my storyboard, pushing a detail view controller that uses the stop name and route name to display times from a plist.
I am fairly new to Objective C and iOS so I am using a segue that my friend told me would work, however, it might be the problem. The segue looks like this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UIViewController *destination = segue.destinationViewController;
if ([destination respondsToSelector:#selector(setDelegate:)])
{
[destination setValue:self forKey:#"delegate"];
}
if ([destination respondsToSelector:#selector(setSelection:)])
{
NSString *route = routeString;
NSDictionary *selection1 = [NSDictionary dictionaryWithObjectsAndKeys:route, #"route", stopString, #"stop", nil];
[destination setValue:selection1 forKey:#"selection"];
}
}
After the segue in my DetailViewController I grab the stop and route information in the view DidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
route = [selection objectForKey:#"route"];
stopName = [selection objectForKey:#"stop"];
NSLog(#"stopName: %#", stopName);
NSLog(#"routeName: %#", route);
}
Here is where my problems arise. When I run the simulator and click on an a cell in my table view, I am pushed to the DVC, however, the stopName and routeName are both null, so no information was sent or received. BUT, if I go back to the table and click another cell, the routeName and stopName are filled with the information that should have sent the first time I clicked a cell. If I continue this process it continues to send the information for the cell tapped previously, not currently.
So basically information is sending but only after I go through the segue twice. Obviously I want it to send the information and receive it the first time, but it is delayed and driving me nuts. I appreciate any help someone can give me as I have been searching the internet for days now trying to fix this issue, thank you so much in advance for any assistance!
prepareForSegue: is being called before didSelectRowAtIndexPath:. This is why the values you see always are lagging behind.
The better solution is to get the stopString and routeString values in your prepareForSegue: method (and not use didSelectRowForIndexPath: at all). The key to doing this is to realize that the sender parameter value being passed to prepareForSegue: is the UITableViewCell that was tapped. You can use the UITableView method indexPathForCell to get the cell's indexPath in your table, and then use that to look up the data in your favoriteItems array.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UITableViewCell *cell = (UITableViewCell*)sender;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
Favorite *favorite = [self.favoriteItems objectAtIndex:indexPath.row];
stopString = favorite.title;
routeString = favorite.subtitle;
UIViewController *destination = segue.destinationViewController;
if ([destination respondsToSelector:#selector(setDelegate:)])
{
[destination setValue:self forKey:#"delegate"];
}
if ([destination respondsToSelector:#selector(setSelection:)])
{
NSString *route = routeString;
NSDictionary *selection1 = [NSDictionary dictionaryWithObjectsAndKeys:route, #"route", stopString, #"stop", nil];
[destination setValue:selection1 forKey:#"selection"];
}
}
Make sure that you are NOT connecting the segue to the next view controller to the tableView CELL directly. Connect to the whole UITableViewController / UIViewController (whichever you are using) and give a name, say "segueNameInStoryboard".
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Favorite *favorite = [self.favoriteItems objectAtIndex:indexPath.row];
stopString = favorite.title;
routeString = favorite.subtitle;
/* add this line */
[self performSegueWithIdentifier:#"segueNameInStoryboard" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"segueNameInStoryboard"])
{
UIViewController *nextViewController = segue.destinationViewController;
nextViewController.delegate = self;
NSDictionary *selection1 = [NSDictionary dictionaryWithObjectsAndKeys:routeString, #"route", stopString, #"stop", nil];
nextViewController.selection = selection1;
}
}

Resources