Viewcontroller fails so call another Viewcontroller - Objective-c - iphone

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/

Related

Navigate from one view to another using UIButton

I have an application with 2 views . In the first one I have a button which when I clicked the user should go to the second view. I tried what is explained before here from Karoley , but it does not work . When I click the button nothing happened?
Here is the code of my action :
-(IBAction)gotoSecondPage:(id) sender{
NSLog(#"In gotoSecondPage");
LeoActionViewController *aSecondPageController =
[[LeoActionViewController alloc]
initWithNibName:#"LeoActionViewController"
bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:aSecondPageController animated:YES];
}
LeoActionViewCOntroller is a controler for a second view.
It just do not switch to a second view. I do not know why
I put code your problem this will help you. First of all, you declare method and open .xib file and then connect to that button with selected touchupinside connection.
In the .h file:
- (IBAction)gotoSecondPage:(id) sender;
In the .m file:
- (IBAction)gotoSecondPage:(id) sender
{
NSLog(#"In gotoSecondPage");
LeoActionViewController *aSecondPageController =
[[LeoActionViewController alloc]
initWithNibName:#"LeoActionViewController"
bundle:nil];
[self.navigationController pushViewController:aSecondPageController animated:YES];
[aSecondPageController release];
}
I'm not sure in what capacity you want to switch views.
What immediately comes to mind is that you want a Navigation Controller. This is an object that lets you put view controllers on a stack and push and pop them to show and hide them. It creates a navigation pathway through your app and is easy to use. It also facilitates the 'standard' navigation bar which is found in many iphone apps.
If you just want to change one view for another view you can do many things including hiding and showing different views using setHidden:(bool)hidden. Otherwise you can use addSubview:(UIView *)view and removeFromSuperview to add and remove views completely from the superview.

not getting tab view when we press back

hii every one
i have created a test project with tab view controller, on click of a tab it will goto that coresponding screen(say screen A) when i click back button in screen A it will come back to main page but with out tabView,
following is may code for back button where DataEntry is a class name to where i am navigating
DataEntry *avController;
UINavigationController *addNavigationController;
if(avController == nil)
avController = [[DataEntry alloc] initWithNibName:nil bundle:nil];
if(addNavigationController == nil)
addNavigationController = [[UINavigationController alloc] initWithRootViewController:avController];
//avController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController presentModalViewController:addNavigationController animated:YES];
insted of the above code if i use following code
[self.navigationController dismissModalViewControllerAnimated:YES];
it will work fine & ill get back to main page with tabs , but it wont run the updated code which is in the viewDidLoad so i need to navigate to the main page insted of using dismissModalViewController
can any one tell me how can i get tabView when i navigate to the main page with out using dismissModalViewController
this is not correct:
[self.navigationController presentModalViewController:addNavigationController animated:YES];
(this is not what you mean to have: it will present a modal view, when you dismiss it, it will uncover what was below, i.e. your tab)
you should use pushViewController to show your avController so that the back button is activated and so on.
look at this.
I'm struggling a little to understand your problem, but viewDidLoad only runs when the view loads: not when you come back to screen A : its still loaded then. To run code each time screen A is displayed, take a look at viewWillAppear / viewDidAppear http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html
I tend to create tabs programatically rather than using the IB: its clearer whats going on. Take a look at this answer to show how I do it.
UITabBar with UINavigationController in code

iphone - Modal view controller disappears?

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

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.

TabBar Application with a View that has a UIButton on the View to goto another View outside the TabBar

I'm new to iPhone development, and multiple views (xib or nib) are really confusing me. This is what i'm trying to achieve...
using TabBarControllerAppDelegate
Have 5 different TabBar Items and have created 5 different Views thru the TabBarController
The First View has a UIButton that is a Next button that needs to go to another view called View2.XIB.
I setup a new UIViewController which references the View2 and an IBAction for the switchPage, etc but not able to get it to do anything when clicking on the button.
All of My TabBar buttons work but not able to Navigate to anything outside of the Tabbar
Any help in this regard will be highly appreciated. Anyone have any examples
IBAction switchPageButtonPressed:(id)sender
{
[self.tabbarcontroller.tabBar setSelectedItem:[self.tabbarcontroller.tabBar.items objectAtIndex:1]];
here 1 means ur 2nd tabbar
}
It is difficult to find the problem without the code, but I will assume your action code for the switchPage button is incorrect. You should use code similar to the following:
- IBAction switchPageButtonPressed:(id)sender
{
ViewController2 *view2VC = [[ViewController2 alloc] initWithNibName:#"View2" bundle:nil];
[self presentModalViewController:nview2VC animated:YES];
[view2VC release];
}
If you are confident your method works, then you will want to verify that the action is hooked up correctly. The easiest way to do this is to place a breakpoint on the method and run the app in Debug. When you click the button, the debugger should break on your method, if it doesn't, you will need to check your connections in Interface Builder.