call storyboard and xib in same viewcontroller? - ios

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

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.

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:

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

Related

displaying splash screen while downloading images from server in ios

in my app i am trying to make a slider using the LWSlideShow LWSlideShow
the source of images are fetched from my server after trying the solution here i stuck with error that said unbalanced call and it means that i am presenting a modal view on a view that did not completed his animation after solving this problem by putting animation to no the splashView that i present will be dismissed before the images are downloaded here is my code for further explanation:
- (IBAction)goDownload {
UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"Splash"];
[self.navigationController presentViewController:vc animated:YES completion:nil];
dispatch_async(dispatch_get_main_queue(), ^{
NSMutableArray *array = [#[] mutableCopy];
LWSlideItem *item = [LWSlideItem itemWithCaption:#""
imageUrl:#"http://code-bee.net/geeks/images/cover-1.jpg"];
[array addObject:item];
item = [LWSlideItem itemWithCaption:#""
imageUrl:#"http://code-bee.net/geeks/images/cover-2.jpg"];
[array addObject:item];
item = [LWSlideItem itemWithCaption:#""
imageUrl:#"http://code-bee.net/geeks/images/cover-3.jpg"];
[array addObject:item];
LWSlideShow *slideShow = [[LWSlideShow alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 120)];
slideShow.autoresizingMask = UIViewAutoresizingFlexibleWidth;
//slideShow.delegate = self;
[self.view addSubview:slideShow];
slideShow.slideItems = array;
if ([slideShow.slideItems count] == [array count]) {
[self dismissViewControllerAnimated:YES completion:nil];
}
});
}
//
//-(void)viewWillAppear:(BOOL)animated
//{
//
// UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"Splash"];
// [self.navigationController presentViewController:vc animated:YES completion:nil];
//}
- (void)viewDidLoad {
[super viewDidLoad];
[self goDownload];
}
also you can see from the code that also i try to use viewWillAppear same thing happened what i want is when the images are downloaded the splashView need to be dismissed i dont know what i am doing wrong
Running that code from a VC anytime before viewDidAppear (like viewDidLoad, viewWillAppear) will cause the problem you describe. But you probably don't want the slide show view to appear - even for an instant - until you're done fetching the assets. This is a common problem.
The solution is to realize that the "splash screen" and the network tasks aren't just preamble, they are as much a part of your application as the slide show.
EDIT
Make that Splash vc the app's initial view controller in storyboard. Right now, the slide show vc probably looks like this:
Uncheck the "Is Initial View Controller" checkbox, find your splash view controller (in the same storyboard, I hope) and check it's box to be the initial view controller. Now your app will start up on the splash vc, like you want it.
When the splash vc done, it can present the slide show vc, or it can even replace itself (with the slide show ) as the app window's root.
To replace the UI, I use variations of this snippet...
// in the splash vc, after all of the asset loading is complete
// give what used to be your initial view controller a storyboard id
// like #"MySlideShowUI"
UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"MySlideShowUI"];
UIWindow *window = [UIApplication sharedApplication].delegate.window;
window.rootViewController = vc;
[UIView transitionWithView:window
duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:nil
completion:nil];

Programmatically showing another UIViewController as popup in iOS?

I have main dashboard (UITableViewController) and once i sign in i need to show this page with a welcome message which am showing using a UIViewController.
How can i show this popup from my ViewDidAppear() method ?
Am using following code and its not working
-(void)viewDidAppear:(BOOL)animated{
popupObj= [self.storyboard instantiateViewControllerWithIdentifier:#"popup"];
[popupObj setModalPresentationStyle:UIModalPresentationCurrentContext];
}
please help me..
i saw couple of stackoverflow links
Update
When i change my code to this one
-(void)viewDidAppear:(BOOL)animated{
popupObj= [self.storyboard instantiateViewControllerWithIdentifier:#"popup"];
// [popupObj setModalPresentationStyle:UIModalPresentationCurrentContext];
popupObj.modalPresentationStyle = UIModalPresentationOverCurrentContext;
popupObj.modalTransitionStyle = UIModalPresentationPopover;
[self presentViewController:popupObj animated:YES completion:nil];
}
Now i can see my UIViewController coming as popup but now UIViewController
coming as full screen view.
But i need only this frame (320 , 320)
popupObj= [self.storyboard instantiateViewControllerWithIdentifier:#"popup"];
popupObj.view.frame = CGRectMake(20, 200, 280, 168);
[self.view addSubview:popupObj.view];
[self addChildViewController:popupObj];
You can add UIViewController as subview and set it's frame so it will look like popup.
Hope this will help you.
I have two way,maybe not good, but work at most of time.
First,when the second view controller appear, show a screen shot of the first view controller. like this:
- (void)setBackGround {
UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, [UIScreen mainScreen].scale);
[self.presentingViewController.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:NO];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.layer.contents = (__bridge id)(image.CGImage);
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (!_isShown) {
_isShown = YES;
[self setBackGround];
}
}
Do't forget set "_isShown = NO" when init.
Second: you can only init a view, and show it on a view controller with animation. code at :http://blog.moemiku.com/?p=101
I have update a example on the blog. Direct download url
Your code creates the view controller and sets its presentation style, but doesn't actually present it. For that to happen you need this line after the two you have right now:
[self presentViewController:popupObj animated:true completion:nil];
Please set navigation controller on your rootviewcontroller :
-(void)viewDidAppear:(BOOL)animated
{
popupObj= [self.storyboard instantiateViewControllerWithIdentifier:#"popup"];
[self.navigationController presentViewController:popupObj animated:YES completion:nil];
}
Or
-(void)viewDidAppear:(BOOL)animated
{
popupObj= [self.storyboard instantiateViewControllerWithIdentifier:#"popup"];
[self.view addSubview:popupObj.view];
}

Switching Views Back and Forth

I am trying to switch views in my application. I have the following code to take me from the Main View to the first level view:
-(IBAction)levelOneButton
{
levelOneView *testView = [[levelOneView alloc] initWithNibName:nil bundle:nil];
testView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:testView animated:YES completion:NULL];
}
Which works GREAT! Then the Code to take me to the next level is the exact same but slightly different!
-(IBAction)nextLevelButton
{
LevelTwoView *levelTwo = [[LevelTwoView alloc] init];
levelTwo.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:levelTwo animated:YES completion:nil];
}
Now when I want to go back to the Main menu from here is where the issue occurs. I have the following code which only dismisses the level2 and shows level1 again.
- (IBAction)goBackButton {
[self dismissViewControllerAnimated:YES completion:NULL];
}
Like I said when this code is used it just releases the current view and takes you back to the last view instead of taking you back to the main menu. After realizing it was not working I tried the following code to take me back to the main menu.
- (IBAction)goBackButton {
ViewController *mainMenu = [[ViewController alloc] init];
mainMenu.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:mainMenu animated:YES completion:nil];
}
When this code is run, I get presented with a black screen.
My question remains how may I go back to the view ViewController? Thank you so much!!
Try this when dismissing the second level view controller.
[[self presentingViewController] presentingViewController] dismissModalViewControllerAnimated:YES]

Present view controller using Storyboards

I have followed a tutorial to add In App Purchases to my app. There are 2 views:
Button to 'Buy item'
Screen comes up that allows user to select product
I have added the code completely fine but in the tutorial they were using XIB files but I am using Storyboard. My code for the 'Buy item' button looks something like this:
- (IBAction)PurchaseItem:(id)sender {
_purchaseController = [[PurchasedViewController alloc] initWithNibName:Nil bundle:nil];
_purchaseController.productID = #"com.myapp";
[self presentViewController:_purchaseController animated:YES completion:NULL];
[_purchaseController getProductID:self];
}
The issue I have is that when the button is clicked, a black screen appears, but I want PurchasedViewController to show
Do I need to change something?
EDIT:
Using edited code but getting error as attached:
- (IBAction)PurchaseItem:(id)sender {
PurchasedViewController *purchaseContr = (PurchasedViewController *)[self.storyboard instantiateViewControllerWithIdentifier:#"menu"];
//menu is only an example
purchaseContr.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:purchaseContr animated:YES completion:nil];
}
With a storyboard you should give an Identifier like in the picture:
tap on your viewController and in 'identity inspector'
in this example Custom Class should be: PurchasedViewController
and this is the code:
- (IBAction)PurchaseItem:(id)sender {
PurchasedViewController *purchaseContr = (PurchasedViewController *)[self.storyboard instantiateViewControllerWithIdentifier:#"menu"];
//menu is only an example
purchaseContr.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:purchaseContr animated:YES completion:nil];
}
Try this
_purchaseController = [[PurchasedViewController alloc] init];
_purchaseController.productID = #"com.myapp";
/* 1. Add the new VC as a child VC */
[self addChildViewController:_purchaseController];
/* 2. Add new view to source view */
[self.view addSubview:_purchaseController.view];
/* 3. Inform 'didMoveToParentViewController' to newly added view controller */
[_purchaseController didMoveToParentViewController:self];
[_purchaseController getProductID:self];

How to switch back to the Main view i.e ViewController.h" what is the nib name for MainStoryboard.storyboard?

I use the following code to switch from main view (ViewController.h)to another view (TouchViewController) and then similarly switch from this view to the next view (QuestionsViewController).
How do I go back to the main view from here? i.e what is the value for initWithNibName for MainStoryboard.storyboard?
- (IBAction)startGame:(UIButton *)sender {
TouchViewController *second = [[TouchViewController alloc] initWithNibName:#"TouchViewController" bundle:nil];
[self presentViewController:second animated:YES completion:nil];
}
and
- (IBAction)selectTopic:(UIButton *)sender {
NSString *category = [[sender titleLabel] text];
[[NSUserDefaults standardUserDefaults] setObject:category forKey:#"MyKey"];
QuestionsViewController *second = [[QuestionsViewController alloc] initWithNibName:#"QuestionsViewController" bundle:nil];
[self presentViewController:second animated:YES completion:nil];
}
Now to go back to the main view i have a problem i.e
- (IBAction)backtoMainMenu:(id)sender {
ViewController *second = [[ViewController alloc]initWithNibName:#"MainStoryboard.storyboard" bundle:nil];
[self presentViewController:second animated:YES completion:nil];
}
At this point the app just crashes! I suppose it's because initWithNibName:#"MainStoryboard.storyboard" can't be used but the ViewController does not have any other xib file - so what do I do?
First of all storyboard can't be used as a xib file they are not the same thing. Second you have one view controller that presents a view controller that also present a view controller so what you can do is something like this from the third view controller:
[self.presentingViewController.presentingViewController
void)dismissViewControllerAnimated:YES completion:nil];
This is the fastest way you can do it, but it's not really reusable, if you have a more complex view controller hierarchy than it will be really ugly to do it like this, other solutions can be delgates or notifications.
Also just a piece of advice, when you want to go back to a previous screen in general you don't what to create a new view controller (as you try in backToMainMenu method, you simply want to remove all the view controllers (screens) until you reach the desired screen.

Resources