Manually push a view to another view (ObjC) - ios

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];

Related

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.

Trying to initialize a view controller from storyboard

I have a view controller I placed in the main storyboard but when I try to initialize and load up the view controller my app crashes. The xcode version I'm using doesn't really tell me the errors I get properly, but I did see that it was giving me a sigabrt signal. I don't know why it isn't working.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"SelectDateViewController"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:vc animated:YES];
This is how I called it. All the names I used are correct but I still don't understand why it's crashing.
Set a breakpoint at the last line to verify vc (and also storyboard) are not nil.
presentModalViewController will crash if its viewController argument is nil.
If both are nil, then your storyboard name is likely incorrect (less likely).
If just vc is nil then the identifier is incorrect (either in code or the storyboard). Make sure the VC's Storyboard ID (as circled in the screenshot) matches your description. It is customary not to choose a class name for this (as you might have more than one instance of a given class in your storyboards).
The Storyboard ID is case sensitive and must be unique. I find the sure fire way to set it is type the name in that field after selecting the view controller and pressing return to end editing (don't just click off the field). I've noticed some corruption occurring in storyboards when converting from Xcode 4 to 5 and vice versa, so diving into the plist/xml might also help.
Edit:
Also the timing of when this is called is important, see
presentModalViewController not working.
Make sure the view controller receiving the message has already been shown (i.e. called in viewDidAppear, not viewDidLoad).
As mentioned by Abdullah, you should likely move off the deprecated method to:
[self presentViewController:vc animated:YES completion:nil];
In this case the completion block can be nil without issue, but vc must not be.
[self presentModalViewController:vc animated:YES]; is deprecated.
Try:
[self presentViewController:vc animated:YES completion:nil];
Did you set the initial ViewController from Storyboard?
If yes then Make sure the your Storyboard id is same.
If you are using storyboard you can directly present or dismiss the viewController no need to write the code for it
hope this help you :)

How to add/remove a sub view from a table view

I have a UITableView in which I want to let user to click on the cell and use a sub view to display the detail information. I have tried this method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SubMainViewController __autoreleasing *mSubView = [self.storyboard instantiateViewControllerWithIdentifier:#"mainSubView"];
[self presentViewController:mSubView animated:YES completion:nil];
}
This seems to be working, however, I couldn't get back to my table view, could anyone help me out?
Well it seems you want to load a detailview when user selects a cell and you want the user to be able go back to the previously view when back or some button like that is pressed. You need to use navigation controller for that. Here is the link to apple's doc http://developer.apple.com/library/ios/ipad/#documentation/UIKit/Reference/UINavigationController_Class/Reference/Reference.html
And here is a sample tutorial to get you started
http://razell.hubpages.com/hub/IPhone-Guide-Loading-a-detail-view
Hope this helps, let me know if you need any more help.
EDIT:
So according to your comment you are looking for a modal view to pop up over the view. Well here is a link to whole a lot of different ways to achieve that, and they all have example projects too so you can actually see how the trick is executed.
http://samwize.com/2012/12/06/7-ios-custom-popup-views/
In your SubMainViewController you need to dismiss the your current viewcontroller using
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];

performSegueWithIdentifier vs instantiateViewControllerWithIdentifier

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.

Resources