0

I don't know how to load my page as it is, When i press back button. I have two type of pages storyboard and xib. Which display all information, When i click go button it will taken to new page. All these working good, But my problem is when i press back button i need my storyboard and xib file should be display in same view. When i run my app it display only storyboard page xib didn't load.

Thanks for advance here's my code:

- (IBAction)back:(id)sender {
    Recharge *view = [self.storyboard instantiateViewControllerWithIdentifier:@"Recharge"];
    [self presentViewController:view animated:YES completion:nil];
    self.recharge = [[mobileViewcontroller alloc]init];
    self.recharge.view.frame = CGRectMake(0,100, 400, 400);
    [self addChildViewController:self.recharge];
    [self.view addSubview:self.recharge.view];
}
Kampai
  • 22,848
  • 21
  • 95
  • 95
Anand ios
  • 57
  • 1
  • 12

3 Answers3

0

I'm assuming that Recharge is the view controller from the storyboard and mobileViewController is the view controller from the xib.

You're calling the wrong constructor to load mobileViewController. You need to use (instancetype)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle to load it with a xib file. See https://developer.apple.com/library/ios/Documentation/UIKit/Reference/UIViewController_Class/index.html#//apple_ref/occ/instm/UIViewController/initWithNibName:bundle:

anthonyliao
  • 1,314
  • 1
  • 8
  • 8
  • can u write code to load my storyboard and xib file together – Anand ios Oct 30 '14 at 06:13
  • Uh, I even gave you the init method to call. It's pretty straight forward. Replace `self.recharge = [[mobileViewcontroller alloc]init];` with `self.recharge = [[mobileViewcontroller alloc]initWithNibName:@"yournibname.xib" bundle:nil];` – anthonyliao Oct 30 '14 at 06:16
  • i hav main storyboard it contains tableview in that same viewcontroller i add subview of xib file to display as same viewcontroller,upto these steps its working good but when i cleck back button need to display the view as it is – Anand ios Oct 30 '14 at 06:30
  • iam tried Recharge *view = [self.storyboard instantiateViewControllerWithIdentifier:@"Recharge"]; [self presentViewController:view animated:YES completion:nil]; self.mobileView = [[mobileViewcontroller alloc]initWithNibName:@"mobileViewcontroller.xib" bundle:nil]; self.mobileView.view.frame = CGRectMake(0,100, 400, 400); [self addChildViewController:self.mobileView]; [self.view addSubview:self.mobileView.view]; – Anand ios Oct 30 '14 at 07:12
0

Replace your code with

- (IBAction)back:(id)sender {
    Recharge *rechargeView = [self.storyboard instantiateViewControllerWithIdentifier:@"Recharge"];
    [self presentViewController:rechargeView animated:YES completion:nil];

    self.recharge = [[mobileViewcontroller alloc]init];
    self.recharge.view.frame = CGRectMake(0,100, 400, 400);
    [rechargeView addChildViewController:self.recharge];
    [rechargeView.view addSubview:self.recharge.view];
}

And it will work.

As you are adding your mobileViewcontroller view on your current viewController which is not visible to user, instead you should add your mobileViewcontroller to the the recharge viewController which is in hierarchy and visible to user.

iHulk
  • 4,869
  • 2
  • 30
  • 39
  • really super bro its worked thank u so much for ur help...:) :) and one more small request in that page i have entered some fields i want to restore the same value when i press back button – Anand ios Oct 30 '14 at 07:19
  • You welcome. So you want to restore values at what page? – iHulk Oct 30 '14 at 07:29
  • in MobileViewController page i.e self.recharge = [[mobileViewcontroller alloc]init]; – Anand ios Oct 30 '14 at 07:33
  • in MobileViewController i have two textField callled Phone Number and amount both the field contains some value i need to restore the same value when i call again that same page – Anand ios Oct 30 '14 at 07:57
  • For that either do not simplest way is just do not create your mobileviewcontroller object again and again. apply a check at back button action if(!self.recharge){ self.recharge = [[mobileViewcontroller alloc]init]; } so if there is already an object it will not create a new one and use the old one – iHulk Oct 30 '14 at 08:23
  • or save your values in nsuser defaults and use them whenever need. – iHulk Oct 30 '14 at 08:25
  • ok bro very thank u for ur suggestion i keep ur words and work if i struck anywhere i comment again – Anand ios Oct 30 '14 at 08:30
  • c this link the same problem iam facing http://stackoverflow.com/questions/23131762/how-to-retain-uitextfield-text-when-navigating-to-different-view-controller-and – Anand ios Oct 30 '14 at 10:15
  • what problem you are facing by using user defaults? – iHulk Oct 30 '14 at 10:44
  • plz check this question i have posted http://stackoverflow.com/questions/26650865/how-to-retain-my-uitextfield-values-in-viewcontroller-when-i-calling?noredirect=1#comment41905570_26650865 – Anand ios Oct 30 '14 at 10:47
0

when ever you call new viewcontroller with passing somthing you have do code like this put all code before[self presentViewController:view animated:YES completion:nil];

try this:

  • (IBAction)back:(id)sender {

    Recharge *view = [self.storyboard instantiateViewControllerWithIdentifier:@"Recharge"];

    self.recharge = [[mobileViewcontroller alloc]init];

    self.recharge.view.frame = CGRectMake(0,100, 400, 400);

    [view addChildViewController:self.recharge];

    [view addSubview:self.recharge.view];

    [self presentViewController:view animated:YES completion:nil];

    }

  • what u did is same what i did its not worked anyways thanks for ur time for this reply i got output from Ihulk – Anand ios Oct 30 '14 at 07:30