Different Storyboards for iPad and iPhone not working right - iphone

I, I have 2 storyboards:
storyboard_iphone
storyboard_ipad
I have 4 viewcontrollers, and when I'm using the app in iPads, only 2 view controllers use the storyboard_ipad. The other view controllers take storyboard_iphone.
In my target project info, I have two rows
Main storyboard file base name (iPad)= storyboard_ipad
Main storyboard file base name = storyboard_iphone
What is the problem?

You can do something like below method to get storyboard as per device:
-(UIStoryboard*)getStoryBord{
UIStoryboard *storyboard;
if ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad))
{
storyboard = [UIStoryboard storyboardWithName:#"storyboard_ipad" bundle:nil];
}
else
{
storyboard = [UIStoryboard storyboardWithName:#"storyboard_iphone" bundle:nil];
}
return storyboard;
}

I would strongly suggest you to use only one storyboard for all devices. Create universal app and use size classes along with Auto Layout.

Related

UIPopoverController for iphone not working?

I need to use a UIPopOverController for my iPhone app ,i searched stackoverflow someone said UIPopoverController does not run on iphone iphone device WHY?.when i run on iphone device
i got this error reason: '-[UIPopoverController initWithContentViewController:]
called when not running under UIUserInterfaceIdiomPad.'
-(void)btnSetRemainderTapped:(UIButton *)button
{
setReminderView =[[SetRemainderView alloc]initWithNibName:#"SetRemainderView" bundle:[NSBundle mainBundle]];
setReminderView.contentSizeForViewInPopover = CGSizeMake(setReminderView.view.frame.size.width, setReminderView.view.frame.size.height);
setReminderView.delegate = self;
popOverController = [[UIPopoverController alloc]
initWithContentViewController:setReminderView] ;
CGRect rect = CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/2, 1, 1);
[popOverController presentPopoverFromRect:rect
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
can any one help me?
You CAN use popoverController in iPhone apps.
1. Create a category
// UIPopoverController+iPhone.h file
#interface UIPopoverController (iPhone)
+ (BOOL)_popoversDisabled;
#end
// UIPopoverController+iPhone.m file
#implementation UIPopoverController (iPhone)
+ (BOOL)_popoversDisabled {
return NO;
}
#end
2. Import it to your class and use popover in iPhone as usual.
But remember that this is private method and Apple can reject your app. But I know people who use this normally and Apple published their apps.
Edit: As stated by Soberman, since iOS 8 it is possible to present popovers on iPhone using public APIs, so this answer is probably not relevant anymore.
As stated in Apple's documentation on UIPopoverController:
Popover controllers are for use exclusively on iPad devices.
So there is no way to use this class in iPhone application unfortunately. But there are a couple of custom third-party implementations of the functionality provided by UIPopoverController which add iPhone support and more. See https://github.com/50pixels/FPPopover for example.
Edit: There also is another highly customizable popover implementation for both iPhone/iPad worth checking out: https://github.com/nicolaschengdev/WYPopoverController.
Since iOS8 we are now able to create popovers, that will be the same on iPhone, as on iPad, which would be especially awesome for those who make universal apps, thus no need to make separate views or code.
You can get the class as well as demo project here: https://github.com/soberman/ARSPopover
All you need to do is subclass UIViewController, conform to the UIPopoverPresentationControllerDelegate protocol and set desired modalPresentationStyle along with the delegate value:
// This is your CustomPopoverController.m
#interface CustomPopoverController () <UIPopoverPresentationControllerDelegate>
#end
#implementation CustomPopoverController.m
- (instancetype)init {
if (self = [super init]) {
self.modalPresentationStyle = UIModalPresentationPopover;
self.popoverPresentationController.delegate = self;
}
return self;
}
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
return UIModalPresentationNone; //You have to specify this particular value in order to make it work on iPhone.
}
Afterwards, instantiate your newly created subclass in the method from which you want to show it and assign two more values to sourceView and sourceRect. It looks like this:
CustomPopoverController *popoverController = [[CustomPopoverController alloc] init];
popoverController.popoverPresentationController.sourceView = sourceView; //The view containing the anchor rectangle for the popover.
popoverController.popoverPresentationController.sourceRect = CGRectMake(384, 40, 0, 0); //The rectangle in the specified view in which to anchor the popover.
[self presentViewController:popoverController animated:YES completion:nil];
And there you have it, nice, neat blurred popover.
So #Sobermans answer didn't really solve the issue from start to finish for me so I want to detail how I got it done using the docs. That being said I do like the idea of using your own presentation controller subclass to manage all of the customisation you want to exhibit.
1. Create your controller to present
The first step is instantiating the controller you want to present:
let vc: UIViewController = ...
vc.modalPresentationStyle = .Popover
vc.preferredContentSize = CGSize(width: CGRectGetWidth(view.bounds)/2, height: 100)
Now we have a controller with the popover presentation style and an arbitrary content size.
2. Implement adaptivePresentationStyleForPresentationController
By default UIPopoverPresentationController will present on full screen on iPhone so to prevent this behaviour you need to force the adaptive presentation style to none.
First we set the delegate of the popover presentation controller
vc.popoverPresentationController.delegate = self;
Then we implement UIPopoverPresentationControllerDelegate
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return .None;
}
3. Present and configure popup
First we need to call presentViewController and only after that can we configure the popover:
presentViewController(vc, animated:true, completion:nil)
if let popover = vc.popoverPresentationController {
popover.permittedArrowDirections = .Right | .Left
popover.sourceView = button
popover.sourceRect = button.bounds
}
Use a custom popover controller, such as:
https://github.com/sammcewan/WYPopoverController
(this seems to be the best supported one that I have found).
I ended up creating my custom tooltip/popover class.
Can be initalised with any content view and dynamically adjusts it's frame.
Hope it helps.
https://github.com/akeara/AKETooltip
If you want to do it in Swift, I believe the code is the following:
extension UIPopoverController {
class var _popoversDisabled : Bool {
get { return false }
}
}
Edit: It is working in Xcode 6 beta 4 on iPhone with iOs7.1
This is a really interesting (and depressing) thread to read. I can't believe Apple prevents popup dialogs on iPhones, with absolutely no justification.
And, it's true, on iOS 8, if you try to work around this limitation, it'll make your popups appear as a full-screen modal dialog.
The following excellent webpage describes "How Apple Cheats" to let its own iBooks and iTunes apps break its own rules, and allow popups - but just from within their own iPhone apps.
HowAppleCheats
Have a read (warning: it'll make you hate Apple & XCode even more..)
Want to get around the "UIPopoverController called when not running under UIUserInterfaceIdiomPad" error on iOS 8 ?
Simple.
Just go into your .plist file, and change the Bundle ID to "com.apple.itunesu" to make XCode think that your app is actually iTunes.
Then your popup will work fine.
(Sigh.)
The alternative way of doing this is to directly add your UIViewController to your screen.
In this example, I wanted a "helper screen" to appear on top of my iPhone screen. It's a UIViewController, it is stored in it's own .xib file, and it has a few lines to add a pretty border:
- (void)viewDidLoad {
[super viewDidLoad];
// Give our popup a pretty curved border
self.view.layer.borderColor = [[UIColor blueColor] CGColor];
self.view.layer.borderWidth = 1.0;
self.view.layer.cornerRadius = 8;
}
To display it, I simply create an instance of this UIViewController, add it to my screen, then center it:
-(void)showHelperScreen
{
if (self.helperScreen == nil)
{
// Add the popup UIViewController to our screen
self.helperScreen = [[HelperViewController alloc] init];
[self.view addSubview:self.helperScreen.view];
}
// Center the popup in the middle of the screen
CGSize screenSize = [[UIScreen mainScreen] applicationFrame].size;
self.helperScreen.view.center = CGPointMake(screenSize.width/2, screenSize.height/2);
}
Of course, I also needed to add some code to make the popup disappear when the user taps outside of it, but this does at least show that you can (safely) display popups on an iPhone, even if your app isn't specifically called iTunes or iBook.
Voila.
Hope this helps, and if anyone needs me, I'll be back in my safe, happy place (Visual Studio, in other words).

SplitViewController Woes - replacing detail view controller in portrait mode failure

I've got a SVC setup which works well. The master and detail views of the SPC are both UINavigationControllers, with separate root view controllers each.
My application works perfectly. I can start it up in either portrait or landscape mode and everything works as it aught to. The 'master' popup button is visible in portrait mode but hidden in landscape mode. Both master and detail planes animate and rotate properly, there is no popping or weird animation artifacts present, and everything stretches properly. 100% satisfaction per spec.
Issues arise when I replace the detail view controller. Since we cannot replace the root node of a uinavi controller, what I do is create a brand new UINavigationController, assigning it the new view I want in the detail view. I do this because the views in the detail view can do 1-2-3 levels deep, but I need to be able to assign unique 'root' views (eg I need to be able to completely replace the root UINavigationController).
The code looks like this:
self.detailViewController = [[SomeNewController1of3 alloc] initWithNibName:#"SomeNewController1of3" bundle:nil];
self.splitViewController.delegate = self.detailViewController;
UINavigationController *newNC = [[UINavigationController alloc] initWithRootViewController:self.detailViewController];
self.splitViewController.viewControllers = #[navigationController, newN];
First, I create the new viewcontroller, whatever it may be. I then set the splitviewcontroller's delegate to = the newly created viewcontroller (so that willHideViewController, etc fires). Then I create the a navicontroller to house the newly created viewcontroller, so that it can push and pop from it. And finally, I replace the second (detail) view of the split view controller with my new navi controller.
Again, this plan works flawlessly when I start in landscape.
When I start in portrait, these are the anomalies I've noticed:
All popups (alert boxes, the master view, etc) have weird rotations and flip out from an awkward spot when they are shown. It's almost as if it rotates these popups right as they're displayed, instead of having them simply 'slide' out of their correct locations.
When the master view is displayed, after doing the weird rotation describes above, it disappears completely for an instant (leaving a black, empty region) then reanimates to it's original location.
Until I complete a device rotation, the 'master' button on the detail view controller which displays the popup for the master view is not shown. After confirming with breakpoints, I've indeed concluded that willHideViewController is Not being called by my above method.
What I don't get is why everything behaves flawlessly when I start in landscape, but all of these errors arise in portrait? All of my views actually have a default orientation set to portrait in IB, and are scale to fit. Also it's not like they're crazy interfaces, they are all just stock tableviews.
Have any of you encountered this issue or found a method around it?
Firstly, the UISplitViewController delegate should be a top level object like your App Delegate, not a view controller the split controller is showing.
Secondly, to answer your question, to replace a showing detail view controller (i.e. pushed on the master nav) in portrait you can make use of a split controller delegate method:
AppDelegate.m
- (BOOL)splitViewController:(UISplitViewController *)splitViewController showDetailViewController:(UIViewController *)vc sender:(id)sender{
if(!splitViewController.isCollapsed){
return NO;
}
UINavigationController *masterNav = splitViewController.viewControllers.firstObject;
if(![masterNav.topViewController isKindOfClass:UINavigationController.class]){
return NO;
}
UINavigationController *existingDetailNav = (UINavigationController *)masterNav.topViewController;
UINavigationController *newDetailNav = (UINavigationController *)vc;
existingDetailNav.viewControllers = #[newDetailNav.viewControllers.firstObject];
return YES;
}
You might be wondering why we don't just replace the master nav stack. Well the problem is the split view controller preserves the detail view controller from the last time showDetail that went through its default behavior. So if we were in portrait, tapped on a cell to show detail, then replaced the detail, then navigate back then upon rotating to landscape the old detail will show on the right and not the new one. By replacing the detail nav stack with the new detail controller as above this solves this because although the split is showing the preserved detail nav it now contains the new detail.
I find it is easier to subclass the SplitViewController. From there, you can easily swap out the the detail adding a navController for those that need it.
Subclass UISplitViewController and set your root splitViewController to that class. Then add this method to your UISplitViewController subclass:
-(void)setDetailControllerTo:(UIViewController *)detailController withNavControllerTitle:(NSString *)title {
[detailController view]; // this line forces the viewDidLoad method to be called
if (title) {
UINavigationController *navController = [[UINavigationController alloc] init];
[navController pushViewController:detailController animated:YES];
detailController.title = title;
NSArray *viewControllers=#[self.mainController.viewControllers[0],navController];
self.mainController.viewControllers = viewControllers;
} else {
NSArray *viewControllers=#[self.mainController.viewControllers[0],detailController];
self.mainController.viewControllers = viewControllers;
}
}
To call this method do something like this from the master view controller in the tableView:didSelectRowAtIndexPath: method
FixedSplitViewController *splitController = (FixedSplitViewController*) self.splitViewController;
CurrentEventViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"CurrentEventViewController"];
// add any setup code here
[splitController setDetailControllerTo:controller withNavControllerTitle:#"Current Event"];
If you wish to keep the master view visible in portrait rotation, add this method to the SplitViewController subclass:
-(BOOL)splitViewController:(UISplitViewController *)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation {
return NO;
}

Autorotating only some of the tabs inside a UITabBar? (ios 5)

I have a UITabBar with 5 tabs. I only wish to enable autorotation for a UIViewController that gets pushed onto the stack deep inside tab #3. So to be clear: tap UITabBar item 3, and you get tabbar item #3's root UIView, which should not autorotate. Tap and get another UIViewController pushed onto the stack (via a UINavigationController). Tap again, and get another UIViewController pushed onto the stack. Only here should this UIView autorotate.
The other 4 tabs should not rotate at all--not the root view of the tabs, nor any of the child views of the tabs.
Can someone tell me what approach I should use? I read that every single tab needs to respond "YES" to willAutorotateToInterfaceOrientation.
In each view's shouldAutorotate..., you could call a method in the root view controller that checks what is currently being displayed. If the deep-level view for tab 3 is on display, it will return YES, otherwise NO, and the views will, in turn, return the same.
Edit -- more detail per user798719's request:
Your root view controller knows which view is on display. You add a method to the root view controller - (BOOL) isDeepLevelTab3Displayed;. The method checks whether the deep-level view for tab 3 is on display and, if so, returns YES, otherwise returns NO.
Each sub view controller’s shouldAutorotate… method will get a ref to the root controller so that it can call isDeepLevelTab3Displayed.
If you’re using a navigation-style controller, you can get the ref like this (self is the sub controller):
NSArray *arrayOfControllers = [self viewControllers];
UIViewController *rootController = [arrayOfControllers objectAtIndex:0]; // per UIViewController class ref, root controller is at index 0
Or you could get anything in your project like this:
YourProjectAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
UIViewController *rootController = appDelegate.rootController; // or appDelegate.intermediateClass1.intermClass2.rootController — however you set up your project
So every sub controller would do this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
UIViewController *rootController = [[self viewControllers] objectAtIndex:0];
return [rootController isDeepLevelTab3Displayed];
}
Therefore, every subcontroller would return YES when autorotation should happen, fulfilling the requirement you mention at the end of your question.
However, if all your subcontrollers are instances of UINavigationController, you could determine which view is currently on display directly, by calling visibleViewController. Then you just need a way of checking its identity.
You could check the controller’s nibName or title, for example, against a constant, or add an integer property intControllerIdentity to all your controllers and set them in the controller’s initWithNibName…
The integer-property scheme might be best, because it won’t be affected should you later change the nibName or title.
You’d add constants to some class whose h file is imported by all the controllers (or, if all the controllers are instances of the same class, put these constants in that class’s h file):
#define kFooController 1
#define kBarController 2
#define kRotatableController 3
And you’d set it like this:
self.intControllerIdentity = kRotatableController;
And check it like this:
if (self.intControllerIdentity == kRotatableController)
Hope that helps. But evaluate this added detail with a critical eye; I have worked with autorotation but not yet with navigation controllers.

Page-Based application modification

I have created standard Page-Based application in Xcode 4.2.1.
I need to change ModelController.m
It has method -
- (DataViewController *)viewControllerAtIndex:(NSUInteger)index storyboard:(UIStoryboard *)storyboard;
for each page this method create new DataViewController
DataViewController *dataViewController = [storyboard instantiateViewControllerWithIdentifier:#"DataViewController"];
I want to use only one DataViewController instance for all page in this application. To change method viewControllerAtIndex like this:
if (dataViewController == NULL) {
dataViewController = [storyboard instantiateViewControllerWithIdentifier:#"DataViewController"];
}
dataViewController.dataObject = [self.pageData objectAtIndex:index];
But this does not work correctly.
Thanks for advance.
I think people need more detail about 'how' it does not work. If it creates multiple instances of DataViewController, have you tried putting if block into #synchronized block? Kind of shooting in the dark, but that is what I would try at first..

Monotouch UITabBarController + UINavigationController

I'm getting a little lost trying to use both a UITabBarController and UINavigationController in MonoTouch.
I can create a navigation based project, and navigate using only a navigationController, fine. Or I can add a tabBarController and navigating to a couple of main screens, fine.
However, I can't seem to navigate to another without using the TabBarController if one is present. E.g, I'm doing an app that deals with "foo", so I am two views on my tabbar, FooHome, and FooSettings. How do I navigate to a new view if the user click something like "Add Foo" on the FooSettings view.
The NavigationController.PushToView doesn't seem to have any effect, and I don't want to add the view to the tabController since its nice and simple with only two items.
Should I be using this.View.AddSubView? The idea sort of sounds like a dialog box, I'm just not sure how to do it with monoTouch...
I was wrestling with this for hours and this post helped. Thank you so much. For those still in the dark a little, here's my code:
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
// create a new window instance based on the screen size
window = new UIWindow (UIScreen.MainScreen.Bounds);
var vc1 = new VideosVC ();
var vc2 = new ScheduleVC ();
var vc3 = new TheCastVC ();
var vc4 = new MerchandiseVC ();
UINavigationController uinc1 = new UINavigationController(vc1);
UINavigationController uinc2 = new UINavigationController(vc2);
UINavigationController uinc3 = new UINavigationController(vc3);
UINavigationController uinc4 = new UINavigationController(vc4);
tabBarController = new UITabBarController ();
tabBarController.ViewControllers = new UIViewController [] {
uinc1,
uinc2,
uinc3,
uinc4,
};
window.RootViewController = tabBarController;
window.MakeKeyAndVisible ();
return true;
}
Add your FooHome and FooSettings controllers to UINavigationControllers and set those navigation controllers to your tab controller.
So for example, the first tab will contain a navigation controller whose root controller is FooHome and the second tab will contain a navigation controller whose root controller is FooSettings.
When you tap on Add Foo in FooSettings, you will push the new controller inside the second tab.
Another approach would be to use the storyboard to create your screens. It's very easy to use a UITabBarController with UINavigationController. You just create segues between them. What has taken you hours could have been done in seconds. Also, by using the storyboard you can very quickly rearrange views & tabs without writing code; making it much more maintainable.