How to set a view to a tag set in code not in Interface Builder? - iphone

I have an app that uses 4 different xibs, lets call them 1-4
So you start on view 1, if you press the button it takes you to view 2, on view 2, you have a back button (which takes you to 1) and forward button that takes you to 3 etc
Anyway, I am removing the next page buttons, and have added a swipe control instead of pressing a button, you can swipe to the next page.
However, I need to know how I can call a tagged view, using the swipe.
At the moment, the UIButton for next page is set in IB as tag 1
This is my swipe code (this is page 1 so only has a swipe left)
- (IBAction)swipeLeftDetected:(UIGestureRecognizer *)sender {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
Page2ViewController *UIViewController =
[[Page2ViewController alloc] initWithNibName:#"Page2ViewController~ipad" bundle:nil];
[self presentModalViewController:UIViewController animated:YES];
}else{
Page2ViewController *UIViewController =
[[Page2ViewController alloc] initWithNibName:#"Page2ViewController" bundle:nil];
[self presentModalViewController:UIViewController animated:YES];
Page2ViewController *VC = [[Page2ViewController alloc] initWithNibName:#"Page2ViewController" bundle:nil];
[self presentModalViewController:VC animated:YES];
[self.view removeGestureRecognizer:[self.view.gestureRecognizers lastObject]];
[VC release];
}
}
Whereabout in that code, can I tell it to swipe to tag 1?
Would appreciate any help :)
Thanks,
Chris
---- Updated FAO Rob;
In the appdelegate.m
- (void)swicthView:(int)viewControllerIndex :(CGRect)viewRect {
if (viewControllerIndex < 0 || viewControllerIndex > viewControllers.count) {
//invalid index passed to function - do nothing
}else{
if (subViewForceUseNibSize == NO) {
//pass the view frame size at runtime
if (CGRectIsEmpty(viewRect) || viewControllerIndex == 0) {
//no frame size so force full screen
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
viewRect =CGRectMake(0, 0, 320, 480);
}else{
viewRect = CGRectMake(0, 0, 768, 1024);
}
}
}else{
//force use the nib size, so reduce size of NIB to leave display of NIB main nib below
viewRect = ((UIViewController *)[viewControllers objectAtIndex:viewControllerIndex]).view.frame;
}
}
//swicth our view
if (viewControllerIndex == 0) {
/*
for (UIView *subview in window.rootViewController.view.subviews) {
[window.rootViewController.view sendSubviewToBack:subview];
}
*/
for (int x = 1; x<[viewControllers count]; x++) {
if (((UIViewController *)[viewControllers objectAtIndex:x]).view.superview != nil) {
[window.rootViewController.view sendSubviewToBack:((UIViewController *)[viewControllers objectAtIndex:x]).view];
}
}
[window bringSubviewToFront:((UIViewController *)[viewControllers objectAtIndex:0]).view];
return;
}
if (((UIViewController *)[viewControllers objectAtIndex:viewControllerIndex]).view.superview != nil) {
((UIViewController *)[viewControllers objectAtIndex:viewControllerIndex]).view.frame = viewRect;
[window.rootViewController.view bringSubviewToFront:((UIViewController *)[viewControllers objectAtIndex:0]).view];
[window.rootViewController.view bringSubviewToFront:((UIViewController *)[viewControllers objectAtIndex:viewControllerIndex]).view];
}else{
((UIViewController *)[viewControllers objectAtIndex:viewControllerIndex]).view.frame = viewRect;
[window.rootViewController.view bringSubviewToFront:((UIViewController *)[viewControllers objectAtIndex:0]).view];
[window.rootViewController.view addSubview:((UIViewController *)[viewControllers objectAtIndex:viewControllerIndex]).view];
}
}

Looking at the revised code sample, it is clear that there is a UIAppDelegate method called swicthView [sic] that is used for transitioning between five different view controllers, all of which are loaded simultaneously. Given this structure, it is advised that you have a property to keep track of which of the five pages is loaded, and based on the left or right swipe, invoke swicthView to transition to that controller. Thus:
#interface ViewController ()
#property (nonatomic) NSInteger currentPage;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.currentPage = 0;
UISwipeGestureRecognizer *gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleLeftSwipe:)];
gesture.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:gesture];
[gesture release];
gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleRightSwipe:)];
gesture.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:gesture];
[gesture release];
// the rest of the viewDidLoad
}
- (void)handleLeftSwipe:(UISwipeGestureRecognizer *)gesture {
if (self.currentPage < 4)
{
++self.currentPage;
[UIAppDelegate swicthView:self.currentPage :CGRectZero];
}
}
- (void)handleRightSwipe:(UISwipeGestureRecognizer *)gesture {
if (self.currentPage > 0)
{
--self.currentPage;
[UIAppDelegate swicthView:self.currentPage :CGRectZero];
}
}
Frankly, I'd strong advise retiring the swicthView design and rather employing a custom container view controller. If you watch WWDC 2011 - Implementing a UIViewController containment, you'll see a good introduction about the importance of keeping a view controller hierarchy synchronized with a view hierarchy, and see some practical demonstrations of custom containers.
The original answer, provided below, was based upon the original snippet of code that was performing presentViewController. It turns out that a very different solution was called for, outlined above, but I retain the original answer for historical purposes:
Original answer:
I assume you have the following sort of code in viewDidLoad:
UISwipeGestureRecognizer *gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleLeftSwipe:)];
gesture.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:gesture];
And then you gesture handler could be:
- (void)handleLeftSwipe:(UISwipeGestureRecognizer *)gesture
{
NSString *nibName;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
nibName = #"Page2ViewController~ipad";
else
nibName = #"Page2ViewController";
Page2ViewController *controller = [[Page2ViewController alloc] initWithNibName:nibName bundle:nil];
// if supporting iOS versions earlier than 5.0, then you should use:
//
// [self presentModalViewController:controller animated:YES];
//
// otherwise you should use presentViewController as done below.
[self presentViewController:controller animated:YES completion:nil];
[controller release];
}
Note, I'm don't remove the gesture (unless you really don't want the gesture there when you return back to this view, which is unlikely). Also note, I'm creating controller, presenting, and releasing.
I'm not understanding your repeated reference to tag properties in this context, as numeric tag values are used for identifying subviews of a view, not for identifying view controller or anything like that. So you say "UIButton for next page is set in IB as tag 1" and later you ask "Whereabout ... can I tell it to swipe to tag 1?" It doesn't make sense to "swipe to a button". You could, though, have the two handlers, the button's IBAction (which I'll call onPressNextButton ... I don't know what you called it) and the handleLeftSwipe call the same method, e.g.:
- (void)handleLeftSwipe:(UISwipeGestureRecognizer *)gesture
{
[self goToNextViewController];
}
- (IBAction)onPressNextButton:(id)sender
{
[self goToNextViewController];
}
- (void)goToNextViewController
{
NSString *nibName;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
nibName = #"Page2ViewController~ipad";
else
nibName = #"Page2ViewController";
Page2ViewController *controller = [[Page2ViewController alloc] initWithNibName:nibName bundle:nil];
[self presentViewController:controller animated:YES completion:nil];
[controller release];
}
References:
presentViewController, the preferred method for modal transitions.
presentModalViewController, the now deprecated method that you use if you need backward compatibility for iOS versions prior to 5.0.
Naming basics in the Coding Guidelines for Cocoa, for advice in naming variables and methods. Note variables generally start with lowercase letters and classes generally start with uppercase letters.

Related

Current ChildView Controller

I am trying to implement a SwipeGesture on my viewcontroller.
The issue I am facing currently is that I cannot determine what the currently displayed childview controller is.
The swipeGesture is added to the container view controller. And then must determine what is the currently displayed VC is in the parent-child relationship, then move to the right or left.
If you are talking about custom container view controller, it's the job of the container controller to keep track of this. So, you might have your own #property that keeps track of which one you're on, and adjust it as you transitionFromController as a result of a swipe. So you might have a numeric property that you increment as you go to the right and that you decrement as you go to the left.
In general (if you're just trying to keep track of which "from" controller you're passing to transitionFromViewController), there are two approaches. One approach is implied by listing 14-3 in Adding a child controller of the View Controller Programming Guide.
In this scenario, in viewDidLoad you perform addChildViewController for the first view controller with. You do not, in this scenario, load the other child view controllers via addChildViewController at this point, though, but rather you let your method that does the transitioning take care of that (as in Listing 14-3).
If you do it this way, you can just grab [self.childViewControllers lastObject], and that will be your "current" child controller. So, that might look like:
#interface ViewController ()
#property (nonatomic) NSInteger currentIndex;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIViewController *initialController = ... // set your initial controller
[self addChildViewController:initialController];
initialController.view.frame = self.containerView.bounds;
[self.containerView addSubview:initialController.view];
[initialController didMoveToParentViewController:self];
UISwipeGestureRecognizer *gesture;
gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
gesture.direction = UISwipeGestureRecognizerDirectionRight;
[self.containerView addGestureRecognizer:gesture];
gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
gesture.direction = UISwipeGestureRecognizerDirectionLeft;
[self.containerView addGestureRecognizer:gesture];
}
- (void) handleSwipe:(UISwipeGestureRecognizer *)gesture
{
if (gesture.direction == UISwipeGestureRecognizerDirectionLeft && self.currentIndex < (kMaxIndex - 1))
{
self.currentIndex++;
UIViewController *newController = ... // set your new controller
UIViewController *oldController = [self.childViewControllers lastObject];
[self cycleFromViewController:oldController
toViewController:newController
direction:gesture.direction];
}
else if (gesture.direction == UISwipeGestureRecognizerDirectionRight && self.currentIndex > 0)
{
self.currentIndex--;
UIViewController *newController = ... // set your new controller
UIViewController *oldController = [self.childViewControllers lastObject];
[self cycleFromViewController:oldController
toViewController:newController
direction:gesture.direction];
}
}
- (void) cycleFromViewController:(UIViewController*) oldController
toViewController:(UIViewController*) newController
direction:(UISwipeGestureRecognizerDirection)direction
{
[oldController willMoveToParentViewController:nil];
[self addChildViewController:newController];
newController.view.frame = oldController.view.frame;
UIViewAnimationOptions options;
if (direction == UISwipeGestureRecognizerDirectionRight)
options = UIViewAnimationOptionTransitionFlipFromLeft;
else if (direction == UISwipeGestureRecognizerDirectionLeft)
options = UIViewAnimationOptionTransitionFlipFromRight;
[self transitionFromViewController:oldController
toViewController:newController
duration:0.33
options:options
animations:^{
[oldController removeFromParentViewController];
}
completion:^(BOOL finished) {
[newController didMoveToParentViewController:self];
}];
}
The other model that I've seen some people do is to load all of the potential child controllers via addChildViewController in viewDidLoad. I don't personally like this approach, but I know people do it this way and it works fine.
But if you do it this way, you can no longer rely upon childViewControllers to know which controller is the current one. In that case, you have to define your own class property for the currentChildController. So it might look something like:
#interface ViewController ()
#property (nonatomic, strong) UIViewController *currentChildController;
#property (nonatomic) NSInteger currentIndex;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:#"ChildOne"]];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:#"ChildTwo"]];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:#"ChildThree"]];
self.currentChildController = self.childViewControllers[0];
self.currentChildController.view.frame = self.containerView.bounds;
[self.containerView addSubview:self.currentChildController.view];
for (UIViewController *controller in self.childViewControllers)
[controller didMoveToParentViewController:self];
UISwipeGestureRecognizer *gesture;
gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
gesture.direction = UISwipeGestureRecognizerDirectionRight;
[self.containerView addGestureRecognizer:gesture];
gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
gesture.direction = UISwipeGestureRecognizerDirectionLeft;
[self.containerView addGestureRecognizer:gesture];
}
- (void) cycleFromViewController:(UIViewController*) oldController
toViewController:(UIViewController*) newController
direction:(UISwipeGestureRecognizerDirection) direction
{
self.currentChildController = newController;
newController.view.frame = oldController.view.frame;
UIViewAnimationOptions options;
if (direction == UISwipeGestureRecognizerDirectionRight)
options = UIViewAnimationOptionTransitionFlipFromLeft;
else if (direction == UISwipeGestureRecognizerDirectionLeft)
options = UIViewAnimationOptionTransitionFlipFromRight;
[self transitionFromViewController:oldController
toViewController:newController
duration:0.33
options:options
animations:^{
}
completion:nil];
}
- (void) handleSwipe:(UISwipeGestureRecognizer *)gesture
{
if (gesture.direction == UISwipeGestureRecognizerDirectionLeft && self.currentIndex < (kMaxIndex - 1))
{
self.currentIndex++;
UIViewController *oldController = self.currentChildController;
UIViewController *newController = self.childViewControllers[self.currentIndex];
[self cycleFromViewController:oldController
toViewController:newController
direction:gesture.direction];
}
else if (gesture.direction == UISwipeGestureRecognizerDirectionRight && self.currentIndex > 0)
{
self.currentIndex--;
UIViewController *oldController = self.currentChildController;
UIViewController *newController = self.childViewControllers[self.currentIndex];
[self cycleFromViewController:oldController
toViewController:newController
direction:gesture.direction];
}
}
Those are the two logical approaches.
By the way, the first approach doesn't preclude having your own class property to know which controller you're on, and sometimes that's useful (e.g. if the type of animation you employ is contingent upon whether you're going to the "right" or to the "left), but unless that's the case, you can get away with just looking at [self.childViewControllers lastObject].
In a project i'm working on, I have many View Controllers each with their own Navigation Controller added to a Parent View Controller. I know the initial entry child, but I do the following to find the currently displayed children of the child:
[myKnownViewController.childViewControllers objectAtIndex:0].navigationController.topViewController

How to access the views of the MoreViewController and change their titles?

on language switch within my App, I need to access the views of the MoreViewController in a TabBar and change their titles.
Could anyone pls tell me how to do that?
Your help is much appreciated.
Cols
Here are some snippets that might work for you. Note that all of the below is subject to break on each and every new iOS release at it is not meant to be done.
Customizing the More view title as itself as well as the title while it is being edited by the user.
- (void)customizeTitleViewWithNavigationItem:(UINavigationItem *)navigationItem
{
VASSERT(navigationItem != nil, #"invalid navigationItem supplied", navigationItem);
UILabel *titleView = [[UILabel alloc] initWithFrame:CGRectZero];
titleView.backgroundColor = [UIColor clearColor];
titleView.font = [UIFont boldSystemFontOfSize:20.0f];
titleView.text = navigationItem.title;
[titleView sizeToFit];
navigationItem.titleView = titleView;
[titleView release];
}
This needs to be implemented within the UINavigationController's delegate;
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated
{
if (navigationController == tabBarController_.moreNavigationController)
{
if ([viewController isKindOfClass:NSClassFromString(#"UIMoreListController")])
{
[self customizeTitleViewWithNavigationItem:viewController.navigationItem];
}
else
{
NSLog(#"viewController (%#) does not seem to be a UIMoreListController", viewController);
}
}
else
{
NSLog(#"navigationController (%#) does not seem to be the moreNavigationController", navigationController);
}
}
This needs to be implemented within the UITabBarController's delegate;
- (void)tabBarController:(UITabBarController *)controller willBeginCustomizingViewControllers:(NSArray *)viewControllers
{
//get the second view of the upcoming tabbar-controller
UIView *editView = [controller.view.subviews objectAtIndex:1];
//did we get what we expected, which is a UITabBarCustomizeView?
if (editView != nil && [editView isKindOfClass:NSClassFromString(#"UITabBarCustomizeView")])
{ //yes->get the navigation-view
UIView *navigationView = [editView.subviews objectAtIndex:0];
//is that a navigationBar?
if (navigationView != nil && [navigationView isKindOfClass:[UINavigationBar class]])
{ //yes->...
UINavigationBar *navigationBar = (UINavigationBar *)navigationView;
[self customizeTitleViewWithNavigationItem:navigationBar.topItem];
}
else
{
NSLog(#"the navigationView (%#) does not seem to be a navigationBar", navigationView);
}
}
else
{
NSLog(#"the editView (%#) does not seem to be a UITabBarCustomizeView", editView);
}
}
I was able to change the title of one of the tabBar items for my 6th view controller (the last one in the more list) with the following code:
NSArray *vcs = [(UITabBarController *)self.window.rootViewController viewControllers];
[[vcs.lastObject tabBarItem] setTitle:#"New Title"];
Is this what you want to do?
After Edit: To change these titles after they are set up the first time, you need to re-set the viewControllers property of the tabBar controller. In this code example, I connected a button in my 6th view controller to an action method that changes the titles of 3 of my controllers, 2 in the more list and one in the regular list.
-(IBAction)changeNames:(id)sender {
UITabBarController *tbc = (UITabBarController *)[[UIApplication sharedApplication] delegate].window.rootViewController;
NSArray *vcs = tbc.viewControllers;
[[vcs.lastObject tabBarItem] setTitle:#"New Title"];
[[[vcs objectAtIndex:4] tabBarItem] setTitle:#"New VC"];
[[[vcs objectAtIndex:3] tabBarItem] setTitle:#"New VC2"];
tbc.viewControllers = tbc.viewControllers;
}

Remove Unused ViewControllers

When I click in the TTTabBar, the tabs elected sends me to one the the correct ViewController, then I click another TTTabBar it will send me to the other one.....etc, but how can I remove the last ViewController, so they don't just keep stacking on top of each other.
- (void)tabBar:(TTTabBar*)tabBar tabSelected:(NSInteger)selectedIndex
{
if(selectedIndex == 0){
UIViewController* viewController = (UIViewController*)[[TTNavigator navigator] viewControllerForURL:#"tt://Forum"];
[self.view addSubview:viewController.view];
[self.view addSubview:_tabBar];
}else if(selectedIndex == 1) {
UIViewController* viewController = (UIViewController*)[[TTNavigator navigator] viewControllerForURL:#"tt://Profile"];
[self.view addSubview:viewController.view];
[self.view addSubview:_tabBar];
}else if(selectedIndex == 2) {
UIViewController* viewController = (UIViewController*)[[TTNavigator navigator] viewControllerForURL:#"tt://PMs"];
[self.view addSubview:viewController.view];
[self.view addSubview:_tabBar];
}else if(selectedIndex == 3) {
UIViewController* viewController = (UIViewController*)[[TTNavigator navigator] viewControllerForURL:#"tt://Friends"];
[self.view addSubview:viewController.view];
[self.view addSubview:_tabBar];
}
}
I got this to work
if ([viewController isKindOfClass:[UIViewController class]]) {
[viewController.view removeFromSuperview];
} else {
}
when i added it above
if(selectedIndex == 0){
Use:
- (void)removeFromSuperview
To do that in your scenario you'll have to keep a reference to the current view around or create a way of getting a reference to it using a tag.
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html
Is TTNavigator a class you wrote? It looks like a singleton manager class to me... Is viewControllerForUrl returning an autoreleased instance of UIViewController? You could implement a stack in the TTNavigator where you would hold the references to all your view controllers and then popping them as you see fit.

How to dismiss a UIPopover correctly?

i have added a UIPopover to my navigationbar when the user touches the rightbarbuttonitem.
I want to know whats the cleanest coding for showing and dismissing the popover. With the code below, it works, but not correct.
If the user touches the rightbarbuttonitem first, the popover appears. Now he can choose a cell,.. works. Or you can touch outside the popover and it disappears. Both ways correctly.
But when touching the rightbarbuttonitem and after that, the same touch on that icon again, you have to touch it twice to bring up that popover again. (Not on the first touch).
How should that correctfunction look like? Thanks for your time..
-(void) downloads:(UIBarButtonItem*)button{
NSLog(#"downloads");
if(tableViewController == nil) {
tableViewController = [[[TableViewController alloc] initWithStyle:UITableViewStylePlain] autorelease];
self.popoverController = [[[UIPopoverController alloc] initWithContentViewController:tableViewController]autorelease] ;
[self.popoverController presentPopoverFromBarButtonItem:barButtonItem permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
tableViewController.delegate = self;
}else {
[self.popoverController dismissPopoverAnimated:NO];
tableViewController.delegate = nil;
tableViewController = nil;
//[self.popoverController release];
//self.popoverController = nil;
}
}
//delegate
-(void) selectedTable:(NSString*)text{
NSLog(#"selectedTable: %#", text);
[self.popoverController dismissPopoverAnimated:YES];
}
Comments
Your downloads: method checks for tableViewController value and make decision if UIPopoverController should be displayed or dismissed. But yours selectedTable: method does dismiss popoverController and does not set tableViewController to nil thus it doesn't work as expected by you.
And I also don't see UIPopoverControllerDelegate implementation, especially popoverControllerDidDismissPopover: which will tell you when UIPopoverController was dismissed (by touching outside your UIPopoverController for example).
Memory Management
Read memory management guide. [self.popoverController release] with self.popoverController = nil leads to crash - one release and second release during setting to nil. (I assume your popoverController property retains).
#Chiefly Izzy: Thanks for your help. You point me in the right direction.
Here´s the code i´m using. It works great. Hope that memory management is clean, too. Isn´t it?
-(void) downloads:(UIBarButtonItem*)button{
if([self.popoverController isPopoverVisible])
{
[self.popoverController dismissPopoverAnimated:YES];
[self.popoverController.delegate popoverControllerDidDismissPopover:self.popoverController];
return;
}
// PopOver Bookmarks-Icon
if(tableViewController == nil) {
tableViewController = [[PdfTableViewController alloc] initWithStyle:UITableViewStylePlain]; //autorelease];
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:tableViewController]; //autorelease] ;
[self.popoverController presentPopoverFromBarButtonItem:barButtonItem permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
popoverController.delegate = self;
tableViewController.delegate = self;
}
/* Using "isPopoverVisible" from top or that else. both works
else {
[self.popoverController dismissPopoverAnimated:YES];
[self.popoverController.delegate popoverControllerDidDismissPopover:self.popoverController];
}
*/
}
-(void) selectedTableView:(NSString*)text{
NSLog(#"selectedTableView: %#", text);
[self.popoverController dismissPopoverAnimated:YES];
[self.popoverController.delegate popoverControllerDidDismissPopover:self.popoverController];
}
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popover{
NSLog(#"popoverControllerDidDismissPopover");
tableViewController.delegate = nil;
[tableViewController release];
tableViewController = nil;
popoverController.delegate = nil;
[popoverController release];
popoverController = nil;
}

How to dismissPopoverAnimated on iPad with UIPopoverController in MKMapView (SDK3.2)

I have a MKMapView (also a UIPopoverControllerDelegate) with Annotations. This MapView has, in the MKTestMapView.h file, a UIPopoverController* popoverController defined in the #interface and a #property (nonatomic, retain) UIPopoverController* popoverController; defined outside of the #interface section. This controller is #synthesized in the MKTestMapView.m file and it is released in the - (void)dealloc section. The Annotations in this MapView have rightCalloutAccessoryViews defined to the following:
- (void)mapView:(MKMapView *)mapView2 annotationView:(MKAnnotationView *)aview calloutAccessoryControlTapped:(UIControl *)control{
...
CGPoint leftTopPoint = [mapView2 convertCoordinate:aview.annotation.coordinate toPointToView:mapView2];
int boxDY=leftTopPoint.y;
int boxDX=leftTopPoint.x;
NSLog(#"\nDX:%d,DY:%d\n",boxDX,boxDY);
popoverController = [[UIPopoverController alloc] initWithContentViewController:controller];
popoverController.delegate = self;
CGSize maximumLabelSize = CGSizeMake(320.0f,600.0f);
popoverController.popoverContentSize = maximumLabelSize;
CGRect rect = CGRectMake(boxDX, boxDY, 320.0f, 600.0f);
[popoverController presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
...
}
Now here comes the fun part. First of all, I am not sure if I need maximumLabelSize and the rect to be the same size. I am new to the popovercontroller so I am playing this by ear..
Okay, the popover shows. Now to dismissing it. I can click anywhere on mapView2 and the popover goes away...but I need the user to click a button in the view if they change anything. URGH!
The docs show:
To dismiss a popover programmatically,
call the dismissPopoverAnimated:
method of the popover controller.
Well, here is the problem: By definition of how the popoverController works, you are clicking inside the view of the displayed popover (to click the button) but have to trigger the dismissPopoverAnimated: method of the controller that launched this popover view, in my case, the popoverController inside the MKTestMapView.m file.
Now, having said all that, remember, [popoverController release] doesn't happen until:
- (void)dealloc {
[popoverController release];
[mapView release];
[super dealloc];
}
So, do i just do the following inside the button (messy but may work):
(Assuming my popover view is a TableView) In the:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
MKTestMapView * mKTestMapView = [[MKTestMapView alloc] init];
[[mKTestMapView popoverController].dismissPopoverAnimated:YES];
}
Here is my issue: I cannot figure out whether doing the above gives me a reference (if there is such a thing) to the existing view that is on the screen -- and therefore the view that is the owner of that popoverController. If it is as simple as
[[[self parentView] popoverController].dismissPopoverAnimated:YES];
I will shoot myself cos I don't think that is the correct syntax either!
This should be easy...yet I am lost. (probably just frustrated with so many iPad differences that I am learning).
Can anyone explain more?
I had the same problem... I had a neat "close" button (X) in the top of my view loaded by the popover, but it didn't work. In my universal app it will be presented as a new view, so that code should stay.
What I did now was that I added the following to my detailedPinView (the view the popover loads):
in the detailedPinView.h file:
#interface detailedPinView : UIViewController {
[...]
UIPopoverController *popover;
[...]
}
-(void)setPopover:(UIPopoverController*)aPopover;
In the detailedPinView.m file:
- (void)setPopover:(UIPopoverController*)aPopover
{
popover = aPopover;
}
The X button closes the view using an IBAction, this is what I did there:
In the detailedPinView.m file:
-(IBAction)releaseDetailedView:(UIButton *)sender
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
if (popover != nil)
{
[popover dismissPopoverAnimated:YES];
}
else {
NSLog(#"Nothing to dismiss");
}
}
else{
[self.parentViewController dismissModalViewControllerAnimated: YES];
}
}
In the class loading the my map and the popover view I added the following code:
[...]
-(void)mapView:(MKMapView *)theMapView annotationView:(MKAnnotationView *)pin calloutAccessoryControlTapped:(UIControl *)control
{
UIViewController *detailController = [[detailedPinView alloc] initWithNibName:#"detailedPinView"
bundle:nil
annotationView:pin];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
UIPopoverController* aPopover = [[UIPopoverController alloc] initWithContentViewController:detailController];
[aPopover setDelegate:self];
[aPopover setPopoverContentSize:CGSizeMake(320, 320) animated:YES];
[detailController setPopover:aPopover];
[detailController release];
[mapView deselectAnnotation:pin.annotation animated:YES];
self.popoverController = aPopover;
[mapView setCenterCoordinate:pin.annotation.coordinate animated:YES];
[self.popoverController presentPopoverFromRect:CGRectMake(382,498,0,0) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
else
{
[self presentModalViewController: detailController animated:YES];
}
[detailController release];
}
[...]
I don't know if the was the answer you were hoping for, I think it might be a bit of a messy way to do it... but giving the time schedule this worked like a charm :)
Here is another simple solution.
I found that we should follow the following steps for clearly dismissing popovers.
dismiss a popover.
release a view loaded by the popover.
Before iOS8, almost all of us may release a view loaded by a popover first, and then we programmatically dismiss the popover.
However, in iOS8, we do the steps in revers.
Before iOS8, my code of dismissing a popover
// creating a popover loading an image picker
picker = [[UIImagePickerController alloc] init];
...
pickerPopover = [[UIPopoverController alloc] initWithContentViewController:picker];
[pickerPopover presentPopoverFromRect:aFrame inView:aView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
// dismissing the popover
[picker.view removeFromSuperview]; // (1) release a view loaded by a popover
[picker release], picker = nil;
if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
[pickerPopover dismissPopoverAnimated:YES]; // (2) dismiss the popover
}
In iOS8, the dismissing code part should be changed as below,
if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
[pickerPopover dismissPopoverAnimated:YES]; // (2) dismiss the popover first
}
[picker.view removeFromSuperview]; // (1) and then release the view loaded by the popover
[picker release], picker = nil;