简体   繁体   中英

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.

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

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.

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.

  1. You created your LoginView
  2. You created your SignupView and added it as subview to LoginView. So the LoginView is parent and signup is child.
  3. (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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM