Goto a detail view in navigationcontroller storyboard from AppDelegate - iphone

I have an App where I use Storyboards.
The first view (navigation controller (initial view)) is connected to a LoginView. When the user is logged in, he sees a modal segue to a Tab controller who is connected to 4 navigation controller views whit their respective UITableViewController´s.
Summary:
NavController - LoginView --> Modal segue --> TabController ---> with 4 tabs - navControllers with Tableviews.
Inside one of those tableviewcontroller there is a DetailView. And when I get my PushNotification in the appDelegate, I need to go to that DetailViewcontroller.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
Here I get my object, and I can even send that object to the correct viewcontroller to be initialized.
MyDetailViewController *dvc = [[MyDetailViewController alloc] initWithQuestion:object];
But I have googled everywhere to find out how to perform the segue so that spesific detailview gets opened from the pushNotification. The app is staying cool on the view the user left when he left last time.
I need code example of how to do this.
I have tried with initializing of storyboard, setting rootviewcontroller etc but nothing happens (except some errors now and then).
Note: I use "Parse" to give me this notification.
I really appreciate good answers. And I will be gratefull into eternity.

First, maybe you should not alloc/init a view controller that should be instantiated in your storyboard.
As for presenting it, you could either use these navigation controller calls:
popToRootViewControllerAnimated:
pushViewController:animated:
making sure to have only the last one animated.
Or you can present a modal view controller programmatically, similar to the suggestion in this answer.

Related

Xcode: Display Login View in applicationDidBecomeActive

In my app I would like to show a login screen - which will be displayed when the app starts and when the app becomes active. For reference, I am using storyboards, ARC and it is a tabbed bar application.
I therefore need to do the process in the applicationDidBecomeActive method:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
if ( ... ) { // if the user needs to login
PasswordViewController *passwordView = [[PasswordViewController alloc] init];
UIViewController *myView = self.window.rootViewController;
[myView presentModalViewController:passwordView animated:NO];
}
}
To an extent this does work - I can call a method in viewDidAppear which shows an alert view to allow the user to log in. However, this is undesirable and I would like to have a login text box and other ui elements. If I do not call my login method, nothing happens and the screen stays black, even though I have put a label and other elements on the view.
Does anyone know a way to resolve this? My passcode view is embedded in a Navigation Controller, but is detached from the main storyboard.
A variety of answers finally led me to an answer which doesn't seem too complicated so I will post it here - and it actually looks really good if I am honest.
Firstly, my password view is embedded in a Navigation Controller (Editor -> Embed In) and this is connected to the main tab bar controller using a modal segue with an id, in my case 'loginModal'.
In the applicationDidBecomeActive method put something like this:
[self performSelector:#selector(requestPasscode) withObject:nil afterDelay:0.2f];
And then put this function somewhere in the App Delegate
-(void)requestPasscode{
if ( /* If the user needs to login */ ) {
[self.window.rootViewController performSegueWithIdentifier:#"loginModal" sender:self];
}
}
This will present your login view whenever the app begins or enters the foreground (for example, when switching apps).
NOTE: The above line will not work if the root of your app is embedded in a navigation controller.
There are however two bugs;
If the user was previously viewing a modal view when they dismissed the app
If the user dismissed the app on the password view.
Both of these cause the app to crash so the following line goes in the applicationWillResignActive method.
[self.window.rootViewController dismissViewControllerAnimated:NO completion:nil];
It basically dismisses all modal views that are presented. This may not be ideal, but modal views are more often then not, used for data entry and so in many cases, this is a desired effect.
You should init PasswordViewController viewcontroller from xib or if you store UI in Storyboard you should use Segue for present this controller.
I can't say about another parts but that part seems to me very weird.
My passcode view is embedded in a Navigation Controller, but is detached from the main storyboard.
in storyboards you can store view controllers and view inside of view controllers so it's not good to store some view outside of viewcontroller because you will not be able to load this view from storyboard after receiving memory warning. Please correct me if I didn't get what do you mean.
If we are going by your way there is no difference load PasswordViewController at applicationDidBecomeActive or at your first view controller at Storyboards because you calling present view controller from first loaded view controller. So you can do it in your first view controller. Also you can store some hidden view inside of your first viewcontroller and show this view if the user needs to login.
I tested it. So at first your controller become loaded and then you got method applicationDidBecomeActive. So it's better to put your code inside -(void)viewDidAppear:animated method of your first viewcontroller.
Best regards,
Danil

Xcode - Logout button

Apparently, I have one LoginViewController and a StartUpController.
LoginViewController has functionality to authenticate user and register new user (presentModalViewController). Once the user has logged in, my system will display StartUpController.
Inside this StartUpController, I have everything such as TabBarController, NavigationController, etc. This StartUpController is actually handling 5 different views.
My question is: what should I do to remove all of my views when my user click on "LogOut" button from one of my view?
I want to show my LoginViewController again.. but at the same time, remove the StartUpController view and all its views.
Please teach me how to do this:
If your authentication view is your root view controller's view, use the -popToRootViewControllerAnimated: method on your navigation controller reference, e.g.:
[myNavigationController popToRootViewControllerAnimated:YES];
Assuming you're not doing any weirdness with view controller ownership, the navigation controller will release the children view controllers (which, in turn, should release their views and other properties).

PoptorootViewController fails then removes all Viewcontrollers

Trying to fix a very strange error, i have 3 view controllers that start from the app delegate and push each other accordingly. The 3rd view controller then has a toolbar button that calls the code here:
-(void)showEventBrowser;
{
accelManeger.delegate = nil;
NSLog(#"%u",[self.navigationController.viewControllers count]);
[self.navigationController popToRootViewControllerAnimated:NO];
}
This works the first time round but when i come back to this view controller and try again. Firstly it reports that there are 3 view controllers on the stack. It then deallocs the 2nd view controller in the stack and doesnt crash but will not go any further. If i hit the button again it says there are no view controllers on the stack and fails to respond.
I have logs for all the viewdid, viewwill, e.t.c in each view controller and there appears to be no odd behaviour. Also no memory warnings from any view controllers.
Why would this work once through but not the second time ?
Well i Fixed this.
I was trying to poptorootviewcontroller from a viewcontroller that had no view but isntead just displayed a UIImagepickercontroller. Even when attempting to dissmiss this modalviewcontroller first, (even with a delay), i still had the same problem. I instead changed the viewcontroller in question to a UIMagepickercontroller subclass and handle the present and dismiss in another viewcontroller.
Lesson learnt, dont pop to root with UIImagepickercontroller modal view ontop.

iPhone: How to Trigger the Loading of a View Using a UI Element in a Previous View

I've been reading the Head First iPhone Development book and I understand how to get to a new view from a table but how exactly would I be able to get to a new view or view controller, by just simply pressing a button? Is that even possible?
I mean there are some apps where you click a button, not a table cell and it loads a new view. How exactly is that done? If someone could help out a newbie it would be greatly appreciated!
I think what you're looking for is a modal vew controller. THis presents a modal view like you described on top of everything else. If rootViewController is the view controller that is displaying your current view, and myNewViewController the view controller you want to display modally:
[rootViewController presentModalViewController:myNewViewController animated:YES];
There's plenty of examples of this kind of thing on the net, just search for presentModalViewController
Like bpapa said in the comments, it's hard to be specific without code. However, generally what you want to do is:
Build a navigation controller that contains one original view.
Create a button in your original view using the Interface Builder.
Build a callback method (usually defined with IBAction) that is run when the button is pushed.
In that callback method, create a new view and push it onto the navigation controller the same way you would using a table view cell.
Alternately, if you only want one level of hierarchy, you could use a modal view controller; instead of pushing onto the navigation controller in the last step, just present the modal view controller.
The general answer is that you have an object that manages which view controller loads when.
The most commonly used is the UINavigationController. It is a UIViewController that instead of controlling views, controls other view controllers. It works like a simple stack. You push views you want to display onto the nav's controller stack and when you want them to disappear you pop them off.
A common (though sloppy) way of using a nav is to make it a property of your app delegate. Then anywhere in your app you can references it by:
UINavigationController *nav=[[[UIApplication sharedApplication] delegate] navigationController];
The view controller for the first the user sees is held in the nav's topViewController property. If you want to load a view based on a user action in the topViewController.view, you would have something like this:
- (IBAction) loadNextView:(id) sender{ // Action called by a a UI event such as a button press.
UINavigationController *nav=[[[UIApplication sharedApplication] delegate] navigationController];
UIViewController *nextViewController=...// load from nib, connect with IBOutlet, create programmatically
[nav pushViewController:nextView animated:YES];
}
The first view disappears to be replaced by the next one. To return to the first view, you have a method in the next view controller like so:
- (IBAction) unloadSelf:(id) sender{ // Action called by a a UI event such as a button press.
UINavigationController *nav=[[[UIApplication sharedApplication] delegate] navigationController];
[nav popViewControllerAnimated:YES];
}
... and the nav returns you automatically to the previous view regardless of what that view was.
When you first start out, especially if you use Interface Builder, the structure of the app is largely hidden. Behind the scenes all view controllers and their views exist in a hierarchy of some kind that leads back up to the app delegate. You should train yourself to think in hierarchal terms even if it is not immediately obvious how that hierarchy is constructed.

Tab bar controller

I have the next question:
In my project I have the next:
UItabbarController
....Some UINAvigationControllers....
*(1) UINavigationController
UIViewController (UItableView) - When select one row it goes to...(by push)
UIViewController (UItableView)
My problem is when i click in the tab bar item, I see the viewController view like last time that i saw this, and no reload to the *(1) first view another time.
Where I need to write sth for each time that i click in a tab bar item i reload the first view of this tab bar item.
Thanks!
If I understand your question correctly, you are trying to return the navigation controller to the root element when it's tab bar item is selected.
To do this, set some object (e.g. your application delegate, but it can be some other object instead) to be the delegate for your UITabBarController. (If you use the application delegate, it will be the delegate for more than one thing, which is fine.) Then, implement the tabBarController:didSelectViewController: method. In that method, tell the selected view controller (which should be a NavigationController) to return to the root view controller.
Something like this. Add this implementation to your AppDelegate.m class:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
[viewController popToRootViewControllerAnimated:NO];
}
In your .xib file, set the delegate for the TabBarController to the the AppDelegate. (If you are programmatically creating the tabBar, you'll have to do it programmatically there.)
As you suspected, trying to do this in viewWillAppear method, or anything other method, of the view controller that live in the navigation controller is not the correct approach. It is a method to perform on the navigation controller, and detected by the delegate of the tab bar.
Try putting your callback code to reload the view in the viewWillAppear or viewDidAppear methods. These are both called every time a view controller displays it's view on the screen.
Also feel free to copy and paste your actual code, it usually makes things easier on our end :)