performSegueWithIdentifier vs instantiateViewControllerWithIdentifier - ios

I don't seem to get this SIGABRT I keep getting. I have this storyboard iOS application, and in the storyboard I have a UITableViewController. Now, I can take a cell of the TVC and make it push the "segue" view controller, but what if I needed to stop the "segue" action on certain conditions? Apparently you can't, since the prepareForSegue:sender: method doesn't allow for it, and it seems to be the only callback that gets called when a transition is about to get performed.
So I guessed I could go into the tableView:didSelectRowAtIndexPath: and perform the segue programmatically. Suboptimal, but still…
Well, it turns out I guessed wrong. Or at least, I'm doing something wrong. The most obvious way to do it would be
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"TheOtherIdentifier" sender:self];
}
but the whole app crashes with a SIGABRT, which does not give any useful information (and yes, I'm sure it's that line that makes the app crash, I checked with the debugger :) Moreover, the VC I'm trying to load has the identifier correctly set, because the following code
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"TheOtherIdentifier"];
[self.navigationController pushViewController:vc animated:YES];
}
"works". Quotation marks indicate that this is clearly not the way such a transition should be performed.
Now: ideas?

Try this:
Use the first code block and not the second.
In storyboard control drag from the cell to the other view controller. Note that a segue is created.
Click on the segue. Use the attributes inspector to give the segue and identifier "theOtherIdentifier" (lower case "t" recommended). Also select a segue style of "push" assuming you are using a navigation controller.
Storyboard will instantiate the other view controller. Be sure you are not doing this in your code.

Related

ECSlidingViewController - adding UITableView and segue to another ViewController

I am trying to create push segue from view. Maybe image would be best for describing:
I started from sample ECSlidingViewController project (BasicMenu) and I am trying to expand first ViewController (Home) to another ViewController. I get it and I can go from selected row in tableView to the controller. But when I am in controller and I tap on Back I am at different screen from first one (it's blank screen with button at upper left). I guess I must set something more to get this working but I don't know what. Thanks
Updated:
Code from first view controller to go to next view controller:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Find the selected cell in the usual way
UITableViewCell *cell = [self.searchResultsTableView cellForRowAtIndexPath:indexPath];
[self performSegueWithIdentifier:#"selectedSegue" sender:cell];
}
I found that the problem is with code in method didSelectRowAtIndexPath. When I removed it. The segue is performed well and I go to other view controller and when I tap on top left button I get back where I was. It's okay.
So I guess the real problem was with sender in performSegueWithIdentifier.
But I need send to new controller some information about selected row. So I used this answer.

Redirect to a specific view controller

Just Have an issue and a "miss" in my programming "capacity".
I've got a tableview controller with some data parsed from a json.
When you choose a "news" you to to the detail view with all data in it.
Everything is ok, but I add a "check" If you are logged or not.
And if not you are "redirected" to login screen.
I try to do it with a segue (modal). It's working, but when I do it, my "navigation" is broken, like if he "lost" he's path.
I try to do it programatically like :
LoginViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
[self.navigationController pushViewController:controller animated:YES];
But when I'm doing it like that, nothing happen, my "detail view controller" load without redirecting
and got that log:
nested push animation can result in corrupted navigation bar Finishing
up a navigation transition in an unexpected state. Navigation Bar
subview tree might get corrupted.
Did someone have a hint for me ?
Thanks
you are doing it all wrong. you need to study the very basic of seque programming (storyboard).
follow this link
LoginViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
[self.navigationController pushViewController:controller animated:YES];
in storyboard you already have a push segue, & again you are pushing loginviewcontroller. thats why you are getting "nested push animation" warning.
This isn't the normal way to do it. You would normally present that login view controller modally.
If this is just the way you want it you could do it using the following steps:
1) Connect your DetailViewController to your TableViewController (not from cell itself, but from that yellow icon which represents your ViewController at the bottom black bar). Choose push if you like and add the identifier to that Segue (for example, "DetailSegue")
2) Connect your LoginViewController to your TableViewController just like you connected your DetailViewController and add the identifier to that segue (for example, "LoginSegue").
Now, when the user clicks on some cell, you wish to check if session is still active, if it is you will do [self performSegueWithIdentifier:#"DetailSegue" sender:self];, and if it is not, you will do [self performSegueWithIdentifier:#"LoginSegue" sender:self];
Hope this helps, cheers.
it's pretty tough to understand what you want to do, the question isn't too clear.
If you want to link to a scene in your storyboard though, create a segue to this by ctrl + click & drag from the initial scene, then give the segue an ID (do this by clicking on the segue and using inspector to set an ID)
then in your tableview controller, where ever you are picking up your tab (assuming this is in the tableview delegate method) you can call the segue programatically
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:#"YOURSEGUEID" sender:self];
}

Coming back from a push view without a UINavigationController and a back button, is it possible?

I have a ViewController, for example startingScreenViewController. I have a push segue from an item on this view to another view (someViewController) which is a ViewController that has a TableView (not TableViewController of course) in it. Now, without having a back button I want to go back to the first view by tapping on a cell in table view. I have written my cell delegate:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
I tried using
[self.navigationController popViewControllerAnimated:YES];
and obviously it didn't work, since I don't have a navigationController. I even tried to segue back to the startingScreenViewController which was a stupid thing to do and also didn't work.
I'm in need of some help, and suggestions would be much appreciated.
I don't know how you manage to open someViewController with push segue without UINavigationController, but if it works, try to use this code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self dismissViewControllerAnimated:YES completion:nil];
}
And, of course, check, is your view controller delegate of table view.
The short answer is to not use a push segue when you don't have a navigation controller. That should not work at all, and the fact that it does work is an accident. There is no guarantee that it will continue to work in the future.
Use a modal presentViewController:animated:completion call, the dismiss it using the corresponding call.

Manually push a view to another view (ObjC)

I am stuck at this since 2 days now, searched almost 27 diff stackoverflow answers yet cannot do it
I want to manually push my view to a different view, I created a Segue with it with identifier gameView and tried using prepareForSegueWithIdentifier method but it doesnt works, always says my viewController cannot find a segue with identifier "gameView" even though it exists.
Here are some screenshots for better understanding:
Earlier on I was using same code which is in prepareForSegue (wasnt using manual push so no prepareWithIdentifier) since earlier I was pushing view from cell selection and it was working, but now I need to push the view manually due to implementing async tasks etc.
Any help on this will be appreciated.
After seeing your project I saw that u were using segue, and made a mess out of it which confused the program, I removed it and made the selecting system with the delegate method for UITableView
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
And then process some data and when its done I present the right ViewController using the method
presentViewController:(UIViewController *)viewController animate:(BOOL)animate completion:^{block}
ViewControllerYouWantToChangeTo *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"ID"];
[self presentViewController:vc animated:YES completion:nil];
ID refer to storyboard or the VC you want to change to
and instead of using
[self dismissViewControllerAnimated:YES completion:nil];//<-- this could cause a warning
use :
[[[self parentViewController] parentViewController] dismissViewControllerAnimated:YES completion:nil];

Creating an array of view controllers?

I'm brand new when it comes to app development so this might be a stupid question.
So i have made a UI table. It is customizable, as in users can insert or delete rows. I want to allow users to click on a table cell and it'll direct them to another view controller. All the view controllers will look the same for each cell (sorta like a template). Any idea how to implement this using storyboard?
Appreciate it!
You do not need an array of view controllers. All you need is one view controller, which gets instantiated when the user clicks the cell to navigate to it, and gets deallocated as soon as the user closes the screen to go back to your main view controller.
All you need to implement this in your storyboard is adding a push segue from a cell or a button in your main view controller to your "detail" view controller. When the segue gets triggerred, your code gets a chance to configure the newly created "detail" view controller in the prepareForSegue:sender: method, before the controller's view appears on the screen. This is the place where you customize the data that shows up in the detail view (presumably, depending on the particular row in the table that has triggered the segue).
Here is a link to a good tutorial explaining how to build a master-detail application with Xcode and storyboards.
In storyboard you create a viewcontroller that will display the data after a cell has been selected, you will only need one and not an array. Link it from the tableviewcontroller to the new viewcontroller. Click the segue in Xcode and in the inspector give it a unique identifier.
tableView:didSelectRowAtIndexPath: will get called when you select a cell, here you can perform the segue:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.selectedObject = ... // store the object that was selected
[self performSegueWithIdentifier:#"mySegue" sender:self];
}
In your tableviewcontroller you make sure you implement prepareForSegue:sender:. Here you can hand over the correct model object to populate your destination viewcontroller with data.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:#"mySegue"])
{
MyDetailViewCotroller *controller = [segue destinationViewController];
controller.dataObject = self.selectedObject;
}
}
Check out this example code from Apple (does not used Storyboard though): http://developer.apple.com/library/ios/#samplecode/SimpleDrillDown/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007416

Resources