iphone - Modal view controller disappears? - iphone

So let's say I have a viewController named homeViewController, and another view controller named listViewController
I display listViewController on top of homeViewController as a modal.
If the user clicks the off button, and then comes back to the app the modalViewController is gone.
ListViewController *listViewController = [[ListViewController alloc] init];
[self presentModalViewController:listViewController animated:NO];
[listViewController release];
Note: Application doesn't startup from scratch when this occures and the previous state is still visible

I'm assuming that by "off button" you mean the user locks the iDevice.
I just tried this in one of my apps and the modal view controller is still there after unlocking. My guess would be that it's something unrelated to the code you have posted. I would check your - (void)applicationWillResignActive:(UIApplication *)application method in your app delegate class and see if there's anything there that would dismiss the modal view controller.

Here is what the problem was.
When the user locks the screen I remove homeViewController from window
[homeViewController removeFromSuperview];
When user starts the app again I do
[windows addSubview:homeViewController];
that brings homeViewController on top of its modeal

Related

Viewcontroller fails so call another Viewcontroller - Objective-c

I have two ViewControllers one named ViewController and another named Signup,
Here is the code I use to call the Signup.xib file,
- (IBAction)signupButton:(id)sender {
Signup *myView = [[Signup alloc] initWithNibName:#"Signup" bundle:nil];
[self.view addSubview:myView.view];
}
This code works. It starts up the Signup Viewcontroller but When I try to call the ViewController.xib file from the Signup.m It doesn't work.
Here is how I call the ViewController.xib file from the Signup.m file,
- (IBAction)loginView:(id)sender {
ViewController *dashboardView = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
[self.view addSubview:dashboardView.view];
}
I have imported the ViewController.h file within the Signup.m file.
The loginView button works because I placed an NSLog() in the button to see if it worked and it does. But When I click the loginView button I get some type of error. I don't know what errors are in xcode but its something like this,
0x110309b: movl 8(%edx), %edi <- Thread 1: EXc_BAD_ACCESS (code=1, address=0x60000008)
It is highlighted in green and I don't know what it means.
As user Priyatham51 said, number 3 is the reason why you are getting the error. The alternative, IF you are not using UINavigationController to push/pop views, is to to present the Signup view controller as a modal view. Then when you want to go back, you can call inside the Signup controller.
[self dismissModalViewControllerAnimated:YES];
However you need to change your signup button to.
- (IBAction)signupButton:(id)sender {
Signup *myView = [[Signup alloc] initWithNibName:#"Signup" bundle:nil];
[myView setModalPresentationStyle:UIModalPresentationFormSheet]; //you can change the way it is presented
[myView setModalTransitionStyle:UIModalTransitionStyleCoverVertical]; //you can change the animation
[self presentModalViewController:myView animated:YES]; //show the modal view
}
I hope this helps you.
Try this to present Signup view to user :
- (IBAction)signupButton:(id)sender {
Signup *myView = [[Signup alloc] initWithNibName:#"Signup" bundle:nil];
[self presentModalViewController:myView animated:YES];
}
And back to Login view by dismissing the signup view controller
- (IBAction)loginView:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
You don't generally add View Controller's views to your current view, but if it is what you want to achieve then try cleaning project first. Sometimes XCode fails to properly load nib files after you add/remove them. Go to Product menu-> "Clean", then hold down "Option" key, go to Product Menu and press "Clean Build Folder".
Also, add "All Exceptions" breakpoint. It will be on the left side panel of XCode, under tab "Breakpoints". On that tab there is "+" button at the bottom. Click "Add Exception Breakpoint" and then just press "Done". This basically will give you more information about your crash.
I know that this doesn't exactly help to fix your problem, but will give you more details on it.
The main reason why your code is not working because you are trying to add your ParentView to the subview as a child again and you are not supposed to do that.
It seems like your are trying to do this.
You created your LoginView
You created your SignupView and added it as subview to LoginView. So the LoginView is parent and signup is child.
(Here is your mistake ). You are trying to add the LoginView as the subview of the SignupView. Which is never going to work.
Try doing this
I would suggest you create your LoginView first and have button "SignUp". So when a user clicks on signup "Push" the new SignupView Controller on top the loginViewController. And have a button "Login View" on your signup view . When user click on that button try to "dismiss current view controller". So user will be shown the login view again. You can use this as work around.
Please take a look at this example
http://www.roseindia.net/tutorial/iphone/examples/pushView.html
Here one more working example
http://www.theappcodeblog.com/2011/08/15/iphone-development-tutorial-present-modal-view/

display view controller before tab bar controller

I'm starting my first application for iphone. I'm using xcode 4.3.3, IOS 5, and the principle of storyboard.
the home screen of the application is a tab bar controller and I want to display a login before the home screen if the user does not logged.
I can not find a solution: if I have to use the file AppDelegate.m with the function didFinishLaunchingWithOptions() or file of my controller with the function viewDidAppear() or something else.
if someone would help me for a solution
Thank you.
just create the login screen when your app is launched and when your login is succeed push your tab bar controller from there...
It is better to add function in AppDelegate.m to remove unwanted window appearing if not logged in (Your home view will be shown for a moment before redirecting to login page if you write code in ViewDidAppear method).
Another method is add a new view controller and check where to redirect based on log in status from view controller's ViewDidAppear method.
Try using a Modal View Controller, Docs
On didFinishLaunchingWithOptions() or viewWillAppear() try do something like this:
YourViewController *viewController = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil];
viewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
viewController.modalPresentationStyle = UIModalPresentationFormSheet;
//Present as Model view controller
["presentedViewController" presentModalViewController:viewController animated:YES];
//release it After presenting to it
[viewController release];
Then to remove it call: dismissModalViewControllerAnimated: (docs)
you can use another view with login screen and Save Bool value in nsuserdeafault then when app is start check for nsuserdefault and show view according to that.
then after you can call everywhere where you want in delegate.m or viewwillappear.

How to develop a TabBar based application with a login functionality?

I am developing an application where i need to show a list as a menu(Courses,lessons,grade,logout) to the user. so even before this i need to show a login screen. Only upon successful and valid login i need to re-direct the user to the menu. So i have planned to develop a tabBar based application with 4 tabs. Here i am confused on how to add the login view controller even before the TabBar controller is loaded. I want the first tab to be selected every time. As of now i am adding my TabBar controller as a rootviewcontroller to my AppDelegate window and then presenting the login view controller as a modal view controller. But the problem here is even before the Login View controller is loaded, my courses view controller is loaded because the tabbarcontroller is loaded first. My actual requirement is i need to load the course view controller with the list of courses based on the inputs given in the Login View controller. But loadview of course view controller is loaded even before the load view of login view controller. so my list of courses is always the same irrespective of who logs in. I am confused here on how to move forward...Any suggestion here would be of great help...
So, a very quick example, could be; in your loginViewController you should have some method something like this:
//Call this after the user has done with the login
-(IBAction)remove:(id)sender{
AppDelegate *del=(AppDelegate*)[[UIApplication sharedApplication] delegate];
//Set some data based on the user's input (eg some property shared in the AppDelegate)
//del.dataEnterByTheUser=someData;
[del removeLoginView];
}
Then in your AppDelegate (assuming that now the rootViewController is the loginViewController) you could do like this (you can optimize the transition):
-(void)removeLoginView{
UITabBarController *tabVC=[[UITabBarController alloc] init];
ViewController *v1=[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
//v1.data=self.dataEnterByTheUser;
ViewController *v2=[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
NSArray *arrayVC=[NSArray arrayWithObjects:v1,v2, nil];
[tabVC setViewControllers:arrayVC];
[tabVC setSelectedViewController:0];
CGRect rectVC=self.loginViewController.view.frame;
rectVC.origin.y=self.view.frame.size.height;
[UIView animateWithDuration:0.3f delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.loginViewController.view.frame=rectVC;
} completion:^(BOOL finished){
[self.loginViewController.view removeFromSuperview];
self.loginViewController=nil;
self.window.rootViewController=tabVC;
}];
}
Also remember to set in each viewControllers's initWithNibName: the self.title to set the title on the tabItem.
No need to fiddle around with the rootViewController...
Just add the following code at the beginning of your viewWillAppear: method of the view controller which would normally appear first (most likely the VC you are presenting in the first tab):
[self.tabBarController presentModalViewController:loginController animated:NO];
Where loginController is obviously the view controller which manages your login screen. If you show it without animation, it will be the first thing visible when your app launches (after the default image disappears). I've used the same method to show a disclaimer page which the user must read before using the app. It's working just fine and made it to the store without problems.
Edit: In this solution, the loginController must dismiss itself once the user has successfully logged in:
[self dismissModalViewControllerAnimated:NO]; //Although you might do this animated, this time
You can just change the array of view controllers in the tab bar controller at runtime. That should be sufficient for your purposes.
I've written a small example. Try to login with the following credentials:
username: john, password: doe
username: pete, password: poe
You will see a different combination of tabs depending on the login used.
The example can be downloaded from my Dropbox: http://dl.dropbox.com/u/6487838/LoginTabExample.zip

Objective-c How to load a UIViewController in synchronous way

my main application view load a UIVIewController for showing a disclaimer, user can or not accept, in a modal way.
The disclaimer UIVIewController has 2 buttons, 'agree' and 'disagree'.
If user clicks on 'disagree' the application close itself.
If user clicks on 'agree' the disclaimer UIVIewController close itself and the main application goes on.
The problem is that when i load the disclaimer UIViewController the main application goes on and not wait until the disclaimer UIViewController is discmissed.
There is a way to open a modal UIViewController in a 'synchronous way'?
Thank you.
so I am assuming that in your application delegate in the applicationDidFinishLaunching method you have a viewController whos view is automatically added to the window. Well inside the viewController class in the -(void)viewDidLoad method you should allocate the disclaimer controller and present it modally using
[self presendModalViewController:my_disclaimer animate:YES];
This will cause the controller to slide up in front of everything. If the user clicks no then simply leave that modal controller display locking them out of the application.
so the code inside your viewController -(void)viewDidLoad method should read
-(void)viewDidLoad
{
//Alocate memory
MyCustomController *controller = [[MyCustomController alloc] initWithWhatever:arguements_to_get_controller_setup];
//present controller
[self presentModalViewController:controller animate:YES];
}

Showing a modal view controller from a tab bar app

First, I would like to warn that I am a complete newbie into iPhone coding...
I need to show up a viewcontroller from a library, I know that it is modal. I have a tab bar app (created with the default XCode template). I need to show that viewcontroller, there are no problem if it hides the tabbar itself... But I am quite clueless, I don't know even what to search, or what to read...
You can call presentModalViewController:animated: to display another UIViewController modally.
EDIT: If you want to display your modal view in response to a button touch (for example), you would display it like this:
- (IBAction)buttonTouched:(id)sender
{
ModalViewController* controller = [[ModalViewController alloc] init];
[self presentModalViewController:controller animated:YES];
[controller release];
}
Then when you want to dismiss the modal controller, call dismissModalViewControllerAnimated:. This can be called either on your main view controller, or the modal one.
I don't know even what to search, or
what to read...
View Controller Programming Guide is a good place to start to help you understand view controllers (including modal ones). If that's confusing, get a bigger picture with iOS Application Programming Guide or start at the very beginning.
You can call modal view as
YourViewController *yvc = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:YES]
[self presentModalViewController:yvc animated:YES];
You can call it in the IBAction method in case you want to call it on any control event like Button Click
-(IBAction)buttonClicked:(id)sender
{
YourViewController *yvc = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:YES]
[self presentModalViewController:yvc animated:YES];
}
You can call it using self.
Hope this helps you.
If you have more doubts on this then you can ask me.