Storyboard - Open UIViewController as UIPopoverController - ios

For my iPad version of the app I want to open my UIViewController as a UIPopoverController from the click of a button. So, the real code for view controller opening is below. How can I easily convert such code into opening a UIViewController as a UIPopoverController?
Code:
UIStoryboard *sb = [UIStoryboard storyboardWithName:[[NSBundle mainBundle].infoDictionary objectForKey:#"UIMainStoryboardFile"] bundle:[NSBundle mainBundle]];
UIViewController *aboutView = [sb instantiateViewControllerWithIdentifier:#"AboutViewController"];
aboutView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:aboutView animated:YES completion:NULL];

Use something like:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
UIPopoverController *poc = [[UIPopoverController alloc] initWithContentViewController:aboutView];
[poc presentPopoverFromRect:CGRectMake(somePoint.x, somePointY, 1.0, 1.0) inView:self.view permittedArrowDirections: UIPopoverArrowDirectionAny animated:YES];
} else {
aboutView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:aboutView animated:YES completion:NULL];
}
You can also present the popover from a button and you also can control the direction in which the popover will be presented. If you use UIPopoverArrowDirectionAny, iOS will make a smart decision for the popover to be visible.
You should also keep a strong reference to your popover and only present it while it is nil to make sure the popover is only present once. That means if the popover is dismissed, set the property holding it to nil.

A common solution is to have two storyboards, one for iPhone, one for iPad. That way you can use the popover segue in the iPad storyboard and the modal segue in the iPhone storyboard. You can readily arrange your Info.plist so that the correct storyboard loads automatically at launch time. You will still need some conditional code, though, since your code will respond differently to having a presented view controller than having a popover.

Related

Presentview(full screen) controller while dismissing View1 which is model view itself

I have an ipad app.
I am trying to open view 2 (kind of push view) full with entire screen. how normally do with push view or UIModalPresentationFullScreen. but my base view which is view 1 is also modal view.
so i was trying to open view 2 when view 1 get dismiss…
- (void) handleNewButton :(int)id
{
[self dismissViewControllerAnimated:YES
completion:^{
NewViewController *View2 = [NewViewController alloc] init];
View2.modalPresentationStyle = UIModalPresentationFullScreen;
View2.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController: View2 animated:YES completion:nil];
}];
}
but my view 2 is not opening. i know i can not do push view. But is there any way to achieve it?.
When you do this dismissViewControllerAnimated the UIViewController (self in this case) is gone, in the sense that he is not on the screen anymore, if it has been released or not, that's another story. The reason for you to not be able to show the View2 (very poor name, it should at least ViewController2) is because you are trying to show it from a UIViewController that is not on the screen anymore.
So, what can you do?
The current self in the context of the handleNewButton method, in theory was presented by another UIViewController, that's from where you want to present your View2.
Probably the quickest way of implementing of what I said, would probably be with a notification described here. Although I would do it with a block, so when the self would be created, I would pass a dismissiCompletionBlock that would be called when that UIViewController was dismissed.
try to allocate NewViewController with nib name if you are not using storyboard,
[self dismissViewControllerAnimated:YES
completion:^{
NewViewController *n=[[NewViewController alloc]initWithNibName:#"NewViewController" bundle:nil];
View2.modalPresentationStyle = UIModalPresentationFullScreen;
View2.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController: View2 animated:YES completion:nil];
}];
or if you are using storyboard get NewViewController using identifier.

Presenting new ViewController from different storyboard - strange delay

I have noticed strange behaviour.
When i'm presenting new viewController that is initiated programatically from different storyboard and flag animated is set to YES there is huge delay before ViewController is shown.
UINavigationController * enterVC = [[UIStoryboard storyboardWithName:#"Survey_iPhone" bundle:nil] instantiateViewControllerWithIdentifier:#"echoEntryVC"];
[self presentViewController:enterVC animated:NO completion:nil];
When flag is set to NO view Controller is presented immediately.
UINavigationController * enterVC = [[UIStoryboard storyboardWithName:#"Survey_iPhone" bundle:nil] instantiateViewControllerWithIdentifier:#"echoEntryVC"];
[self presentViewController:enterVC animated:YES completion:nil];
Does someone have the same problem? Any explanation would be good :)

Presenting and dismissing a modal view in ios 7

I have a view controller that has a button on it. The button is the Privacy Policy. When it's clicked it goes to the proper IBAction and I create the privacy controller.
- IBAction ...
{
PrivacyPolicyViewController *privacy = [[PrivacyPolicyViewController alloc] init];
.....
}
I want to create a modal view of the privacy controller that has a UIWebView that animates itself upward and a back button to close it in ios 7. The ways I see online all are ios 6 and seem deprecated.
Use something like this:
// assuming your controller has identifier "privacy" in the Storyboard
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
PrivacyPolicyViewController *privacy = (PrivacyPolicyViewController*)[storyboard instantiateViewControllerWithIdentifier:#"privacy"];
// present
[self presentViewController:privacy animated:YES completion:nil];
// dismiss
[self dismissViewControllerAnimated:YES completion:nil];
[self presentmodalviewcontroller:vc]; has been deprecated.
you can try for
[self presentViewController:viewController animated:YES completion:nil];
it will work for you..
If you are using Storyboards, you can use a segue to present a modal view controller as well, and do it programmatically.
In your storyboard, ctrl+drag from the File's owner icon in the bar under the starting view to the view you want to present modally, let go and select "modal".
click on the segue icon, and then in the Attributes inspector, give it an identifier, like "toNewView".
in your starting view controller's .m file, use this code to perform the modal segue: [self performSegueWithIdentifier:#"toNewView" sender:self];
It's a nice clean way to do it because you don't have to import a .h file to instantiate the second controller object for the presentViewController method.
To dismiss it, you just use an unwind segue.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
taskQueeDetails *privacy = (taskQueeDetails*)[storyboard instantiateViewControllerWithIdentifier:#"taskQueeDetails"];
// Present the modal
[self presentViewController:privacy animated:YES completion:nil];
use the code and change the string instantiateViewControllerWithIdentifier:#"taskQueeDetails"];
it will work fine

Load UISplitViewController programmatically from another Storyboard

Currently I am using two iPad storyboards in my project.
The first storyboard has a Login screen and a tableview controller. I want to call the second storyboard from the first storyboard tableview controller, when the cell is clicked. Normally it's easy, but here the second story board has a UISplitViewController.
MainSVC *baseView = [[MainSVC alloc] init]; //UISplitViewController
UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:#"Mail_iPad" bundle:nil]; //Second Storyboard
baseView =[storyBoard instantiateViewControllerWithIdentifier:#"MainSVC"]; //MainSVC = Storyboard Name
[self presentViewController:baseView animated:YES completion:nil];
This code is not working. I searched in google, but couldn't find the best solution.
How can I call second storyboard splitview controller programmatically?
Use this code. This should be work. !
UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:#"Mail_iPad" bundle:nil];
UISplitViewController *split = [storyBoard instantiateViewControllerWithIdentifier:#"MainSVC"];
self.view.window.rootViewController = split;
Do not allocate another instance of split view controller. It should be done like this
UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:#"Mail_iPad" bundle:nil]; //Second Storyboard
MainSVC *baseView =(MainSVC *)[storyBoard instantiateViewControllerWithIdentifier:#"MainSVC"]; //MainSVC = Storyboard Name
[self presentViewController:baseView animated:YES completion:nil];

storyboard, self presentViewController, iOS 6, blank screen

I am using storyboards, I have one viewcontroller and on click I need to show another view controller modally. I am trying using this code
[self presentViewController:zoomV animated:YES completion:NULL];
I am coming up with a blank screen.
This is how I create
zViewController *zoomV = [[zViewController alloc] init];
[self presentViewController:zoomV animated:YES completion:NULL];
I tried researching this and some answers revolve around using storyboards and not having a rootviewcontroller associated. So what I have is in the initial scene I have a navigationController, and from there I drag to another Viewcontroller a relationship which defines it as a rootViewcontroller. Is that sufficient ? or is this irrelevant?
Since you have your zViewController in your storyboard, you should instantiate your zViewController using UIStoryboard instantiateViewControllerWithIdentifier:.
In your first view controller, instead of creating the zViewController using alloc/init do this, of course setting an identifier for your zViewController in your storyboard.
zViewController *zoomV = [self.storyboard instantiateViewControllerWithIdentifier:#"yourIdentifier"];
[self presentViewController:zoomV
animated:YES
completion:NULL];
Also you could accomplish the same using a segue and executing it directly, without the need of instantiating the zViewController, but is up to you.
As a second(small) comment, do not name classes starting with lowercase in ObjC :).
You may refer below snippet for story board:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
AddNameViewController *sfvc = [storyboard instantiateViewControllerWithIdentifier:#"AddNameViewController.m"];
[sfvc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:sfvc animated:YES completion:nil];

Resources