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];
Related
I want to present a view controller as an popup in iPhone from a UIBarButtonItem
I have written the following piece of code on the action of the button.
-(IBAction)showAvailableCategory:(UIButton *)sender
{
CategoryPopup *categoryPopupViewController = [[CategoryPopup alloc]init];
UIPopoverPresentationController *popOverCat = categoryPopupViewController.popoverPresentationController;
categoryPopupViewController.preferredContentSize = CGSizeMake(138, 122);
popOverCat.delegate = self;
popOverCat.sourceView = sender;
popOverCat.sourceRect = sender.bounds;
popOverCat.permittedArrowDirections = UIPopoverArrowDirectionAny;
[self presentViewController:categoryPopupViewController animated:YES completion:nil];
}
I have made my CategoryPopup as freeform in the xib and also I am implementing the below delegate method
-(UIModalPresentationStyle) adaptivePresentationStyleForPresentationController: (UIPresentationController * ) controller
{
return UIModalPresentationNone;
}
But it does not come up as popup rather it comes fullscreen, is there a workaround?
You forgot just one setting, add it right after categoryPopupViewController's initialization:
categoryPopupViewController.modalPresentationStyle = UIModalPresentationPopover;
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];
}
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];
}
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]
I have 6 view controllers when my app starts. It's like an image gallery. When a user pushes for example the button on the third view, he/she should gets to the third view in the tab bar.
I use this code to launch the view controllers on the top of the tab bar controller:
- (void)viewDidAppear:(BOOL)animated {
static BOOL first = YES;
if (first) {
UIViewController *popup = [[Home1ViewController alloc] initWithNibName:#"Home1ViewController" bundle:nil];
[self presentViewController:popup animated:NO completion:nil];
first = NO;
}
}
By using this code to dismiss this new view, I'm just coming to the specific view, but not my tab-bar page...
-(IBAction)dismissView {
TabBarPage3 *screen = [[ TabBarPage3 alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:screen animated:YES];
}
Please help me with this!
Thanks
This is the code for change view on TabBar
[((UITabBarController *)(self.parentViewController))setSelectedIndex:index];
Sample Project of UITabbar