开发者

iPhone: Use a view as a transparent overlay with closing button

I've got a map with three bar buttons for different markers to show up in the map. If I click on a bar button, the specific markers are shown in the map, which already works great.

Now I would like to show a transparent overlay (popup window) with the description of the markers after I clicked on a bar button with a button to close the overlay again and show the markers (which are set in the background).

The function of the bar button:

- (IBAction)routeTwo:(id)sender
{
    // The code for the overlay
    开发者_JAVA百科// ...

    // remove any annotations that exist
    [map removeAnnotations:map.annotations];  

    // Add any annotations which belongs to route 2
    [map addAnnotation:[self.mapAnnotations objectAtIndex:2]];
    [map addAnnotation:[self.mapAnnotations objectAtIndex:3]];
    [map addAnnotation:[self.mapAnnotations objectAtIndex:4]];
    [map addAnnotation:[self.mapAnnotations objectAtIndex:5]];
}

I tried the following possibilities:

1. Using a modal view controller

RouteDescriptionViewController *routeDescriptionView = [[RouteDescriptionViewController alloc] init];
    [self presentModalViewController:routeDescriptionView animated:YES];
    [routeDescriptionView release];

Works great, but the problem is: The map view in the background is not visible anymore (configuring alpha values of the modal view doesn't change anything).

2. Add RouteDescriptionView as a subview

RouteDescriptionViewController *routeDescriptionView = [[RouteDescriptionViewController alloc] init];
    [self.view addSubview:routeDescriptionView.view];
    [routeDescriptionView release];

Works great as well, but the problem here is: I can't configure a close button on the subview to close/remove the subview (RouteDescriptionView).

3. Using UIAlertView

Would work as expected, but the UIAlert is not really customizable and therefore not suitable for my needs.

Any ideas how to achieve this?


If you go with option 2, you can add an instance of UIButton as a subview of routeDescriptionView, and connect that button to routeDescriptionView's removeFromSuperview method.

For example, in your RouteDescriptionViewController's loadView or viewDidLoad method:

UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[closeButton setImage:[UIImage imageNamed:@"close.png"] forState:UIControlStateNormal];
[closeButton addTarget:[self view] action:@selector(removeFromSuperview) forControlEvents:UIControlEventTouchUpInside];
[[self view] addSubview:closeButton];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜