Is there way to make show segue works only on iPhone? Some kind off turn it off at iPad? I have show segue on iPhone and it works perfect but on iPad I am using UISplitVC with same VC. And now every time when I choose something on left part of UISplitVC it make that segue instead my left UITableVC.
Set an identifier for the segue in Interface builder, then in your view controller class you can use this in a method instead of presenting it in your storyboard. You can do that by clicking the class button in your view controller and entering it in "Storyboard ID" as shown here.
IF YOU HAVE A BUTTON THAT YOU CLICK to present the new view you can drag an IBAction from the storyboard to this method on the view controller you are presenting it from.
-(IBAction)launchIphoneOnlyController (id)sender {
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone ) {
UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"identifier"];
[self presentViewController:vc animated:true completion:nil];
}
}
Otherwise make a call to the following method from wherever you want to instantiate the view from.
-(void)launchIphoneOnlyController {
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone ) {
UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"identifier"];
[self presentViewController:vc animated:true completion:nil];
}
}
In shouldPerformSegueWithIdentifier check the seque ID you set in the story board and check (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) for iPhone.
See apple developer.
Related
I write code in IB action to check condition to switch view controller in same story board but if I touch button it go to black screen
this is my code
-(IBAction)Buyvoyage{
if (ck==1) {
NSLog(#"brought IAP");
VoyageplayViewController *voy = [[VoyageplayViewController alloc]init];
[self presentViewController:voy animated:YES completion:NULL];
}
}
try replacing
[self presentViewController:voy animated:YES completion:NULL];
with
** [self presentViewController:voy animated:YES completion:nil];**
However, i would prefer you to use seuge by following below steps.
1. Create segue from each UIButton to the corresponding UIViewControllers, set Segue identifier in IB by selecting the segue(s) you just created e.g. if you have 2 button then set identifier of the Segue "TestSegue1" and "TestSegue2" respectively.
2. Now create an IBaction for UIButton from which you want to navigate, and put below code in that IBAction
3. Remember to set Tag property in IB of all the UIButton(s) you want to perform navigation on as per your requirement
// When any of my buttons is pressed, push the next UIViewController using performSeugeWithIdentifier method
- (IBAction)buttonPressed:(UIButton*)sender
{
if (sender.tag==1) {
[self performSegueWithIdentifier:#"TestSegue1" sender:sender];
}
else if(sender.tag==2)
[self performSegueWithIdentifier:#"TestSegue2" sender:sender];
}
Or Use storyboard identifier to present your viewcontroller using below code
- (IBAction)buttonPressed:(UIButton*)sender
{
//Get StoryBoard Identifier using
UIStoryboard *storyboard = [self storyboard];
if(ck==1) {
VoyageplayViewController *voyVC = [storyboard instantiateViewControllerWithIdentifier:#"Voyage"];
[voyVC setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:voyVC animated:YES];
}
}
don't forget to set ViewController StoryBoard Id in StoryBoard -> identity inspector
I am building a universal App which is supposed to present a view controller to add stuff. On iPad, this is presented in a popover and on iPhone in a modal viewController. I am using the presenting ViewController as a delegate.
Now, if I want to assign the properController, I have to:
if([[segue identifier] isEqualToString:#"popoverAddSegue"])
self.myPopOver = [(UIStoryboardPopoverSegue * )segue popoverController];
if([[segue identifier] isEqualToString:#"modalAddSegue"])
self.myModalView = [segue destinationViewController];
thus using two properties and only ever assigning one.
Then, in my delegate function, I again have to differentiate:
if(self.myPopOver)
[self.myPopOver dismissPopoverAnimated:YES];
if(self.myModalView)
[self.myModalView dismissViewControllerAnimated:YES completion:nil];
The problem obviously is that UIPopoverController does not inherit from UIViewController... Is there any elegant way of doing this? Factory or something? I hate having customised code in a viewController that should be agnostic about how it presents its viewControllers...
I use this
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
// The device is an iPhone or iPod touch.
} else {
// The device is an iPad => show library true popover.
}
This is not exactly what you ask for, but it have a good point : you can have the same segue identifier for both iPhone and iPad.
Now to have 1 similar method for both (popover / modalVC), may be you can add to both a new category with a method name dismissAnimated:
I got first ViewController with out navigation controller, I go from it to second ViewController via
if (!self.mapViewController)
{
self.mapViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"MapViewController"];
}
[self presentViewController:self.mapViewController animated:YES completion:nil];
I send some data with prepareForSegue to it, but I need that it also be with navigation controller.
I embed in a navigation controller in storyboard but my code still called second ViewController with out navigation.
I am not a professional with the way of the storyboard, however i believe that instead of presentViewController
you should be using the following function to present a storyboard VC based on segues.
[self performSegueWithIdentifier:#"SegueIdentifierHere" sender:nil];
Make sure that in your storyboard you have incorporated a UINavigationControllerVC as well.
Give an identifier to the navigation controller in storyboard. Instantiate and present that.
UINavigationController *navVC = [self.storyboard
instantiateViewControllerWithIdentifier:#"TheNavVCWhoseRootIsMyMapViewController"];
self.mapViewController = navVC.viewControllers[0]; // your map vc is at the root
[self presentViewController:navVC animated:YES completion:nil];
You are using presentViewController:animated:completion which will indeed display a UIViewController without the UINavigationController the previous UIViewController was embedded in.
Try using the following:
[self.navigationController pushViewController:self.mapViewController animated:YES]
I have a simple single view app with two storyboards with different layouts for portrait and landscape. The landscape layout is completely different which is why I am using a second storyboard.
When I switch to landscape the view doesn't change and I get the
"Application tried to present a nil modal view controller on target"
` error in the debugger. I have checked that the landscape storyboard is referencing to the CalculatorViewController class.
This is the line of code which generates the error:
[self presentViewController:landscapeViewController animated:YES completion:nil];
Here is the whole method from CalculatorViewController.m:
- (void)orientationChanged:(NSNotification *)notification
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
!isShowingLandscapeView)
{
// code here to show landscape storyboard
UIStoryboard *landscapeStoryboard = [UIStoryboard storyboardWithName:#"LandscapeStoryboard" bundle:nil];
CalculatorViewControllerLandscape *landscapeViewController = [landscapeStoryboard instantiateInitialViewController];
[self presentViewController:landscapeViewController animated:YES completion:nil];
isShowingLandscapeView = YES;
}
else if (UIDeviceOrientationIsPortrait(deviceOrientation) &&
isShowingLandscapeView)
{
[self dismissViewControllerAnimated:YES completion:nil];
isShowingLandscapeView = NO;
}
}
The Initial View Controller is called CalculatorViewController.
The View Controller for the landscape layout is called CalculatorViewControllerLandscape.
This is my first app so I really appreciate any help. I have tried to find a solution in similar questions posted without success.
It means that landscapeViewController is nil.
This can be cause by either:
landscapeStoryboard being nil (most likely because a storyboard named LandscapeStoryboard cannot be found)
no initial view controller being indicated in the storyboard.
You need to make sure that the identifier for the storyboard landscape-view is set to "LandscapeStoryboard". Select the storyboard and check the identifier.
In Xcode 4.5 you can change views from a segue, But I want to programatically change views in Mainstoryboard. I Know in Xib. we use this method:
SecondViewController *Second = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:Second animated:YES];
I want to change the view when the user clicks done in the textfield (This code detects if user clicks done):
-(IBAction)hidekeyboard {
[sender resignFirstResponder];
}
You create a segue in your storyboard that goes from the view that has the text field in question and goes to your SecondViewController. You have to assign an identifier to it (also in the storyboard, click on the line connecting the two view controllers), let's call it "segueToSecondViewController". Then, you can manually call the segue using:
[self.storyboard performSegueWithIdentifier:#"segueToSecondViewController" sender:self];
An alternative to segueing is to instantiate the viewController from the storyboard.
First you assign a Storyboard ID to the viewController in the storyboard.
Then you instantiate that viewController, and present it.
MBTagEditorViewController *tagEditor = [self.storyboard instantiateViewControllerWithIdentifier:#"TagEditor"];
[self presentModalViewController:tagEditor animated:YES];