UIBarButtonItem不会出现

时间:2015-05-13 12:20:28

标签: ios objective-c uibarbuttonitem

我有一个问题,当加载视图时,它将加载UIBarButtonItem,但是我必须在“mostra_filtro_btn”中将其设置为nil,但是然后再次在“load_map”中我必须设置它,但它不会不要出现。 这是我的代码:

//
//  FirstViewController.m
//  House Finder
//
//  Created by Giovanni Poli on 12/05/15.
//  Copyright (c) 2015 Giovanni Poli. All rights reserved.
//

#import "MapViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "Reachability.h"
#import "UIKit/UIKit.h"
#import <Foundation/Foundation.h>
#import "FiltroViewController.h"

@interface MapViewController ()

@end

@implementation MapViewController
@synthesize filtro_controller,overlay_filtro_counter,mappa_controller;



-(void)load_map{
    NSLog(@"load_map");
    [mapView removeFromSuperview];
    [mappa_controller.view removeFromSuperview];
    [filtro_controller.view removeFromSuperview];

    UIBarButtonItem *Button = [[UIBarButtonItem alloc] initWithTitle:@"Filtro" style:UIBarButtonItemStyleBordered target:self action:@selector(mostra_filtro_btn:)];
    [Button setTitle:@"Filtro"];
    [Button setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName,nil]forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = Button;
}

- (id) init{
    filtro_controller = [[FiltroViewController alloc]init];
    [self.view addSubview:filtro_controller.view];
    [self addChildViewController:filtro_controller];
    return self;
}


- (void)viewDidLoad {
    self.title = @"Mappa";
    self.navigationItem.title = @"House Finder";
    self.navigationController.navigationBar.translucent = FALSE;
    UIBarButtonItem *Button = [[UIBarButtonItem alloc] initWithTitle:@"Filtro" style:UIBarButtonItemStyleBordered target:self action:@selector(mostra_filtro_btn:)];
    [Button setTitle:@"Filtro"];
    [Button setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName,nil]forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = Button;

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"json"];
    NSData *content = [[NSData alloc] initWithContentsOfFile:filePath];
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:content options:kNilOptions error:nil];
    NSArray * json_all = [json objectForKey:@"results"];






    mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height-40)];
    mapView.delegate = self;
    mapView.showsUserLocation = NO;
    mapView.userInteractionEnabled = YES;
    CLLocationCoordinate2D annotationCoord;
    self.view.userInteractionEnabled = YES;

    NSDictionary * temp;
    for (id object in json_all) {
        temp = object[@"titolo"];
        annotationCoord.latitude = [object[@"lat"] floatValue];
        annotationCoord.longitude = [object[@"lon"] floatValue];
        MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
        annotationPoint.coordinate = annotationCoord;
        annotationPoint.title = object[@"titolo"];
        annotationPoint.subtitle = object[@"agenzia"];
        [mapView addAnnotation:annotationPoint];
    }

    [mapView showAnnotations:[mapView annotations] animated:YES];
    [self.view addSubview:mapView];
    [self.view setNeedsDisplay];
}


- (IBAction) mostra_filtro_btn: (id)sender{
    NSLog(@"mostra_filtro_btn");
    self.navigationItem.rightBarButtonItem = nil;


    filtro_controller = [[FiltroViewController alloc]init];
    [self.view addSubview:filtro_controller.view];
    [self addChildViewController:filtro_controller];
}







- (MKAnnotationView *)mapView:(MKMapView *)mapViews viewForAnnotation:annotation{
    if (annotation == mapViews.userLocation) return nil;
    MKPointAnnotation * temp = annotation;
    MKAnnotationView * m = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"casa"];
    m.canShowCallout = YES;
    m.enabled = YES;
    m.userInteractionEnabled = YES;
    NSString * icon_file = [NSString stringWithFormat:@"%@.png",temp.subtitle];
    m.image = [UIImage imageNamed:icon_file];
    return m;
}


- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
    NSLog(@"overlay prezzo");

}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end

4 个答案:

答案 0 :(得分:1)

使用以下代码取消导航项,使其隐藏在您想要的位置

[self.navItem setRightBarButtonItem:nil];
[self.navItem setLeftBarButtonItem:nil];

答案 1 :(得分:1)

首先尝试创建UIButton然后将其放入导航按钮,如下所示

UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 44.0f, 30.0f)];
[backButton setImage:[UIImage imageNamed:@"back.png"]      forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(popVC) forControlEvents:UIControlEventTouchUpInside];        
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]     initWithCustomView:backButton];

答案 2 :(得分:0)

由于我不能将此作为评论发布,因此我认为在load_map中缺少这些内容:

dispatch_async(dispatch_get_main_queue(), ^{
       NSLog(@"load_map");
    [mapView removeFromSuperview];
    [mappa_controller.view removeFromSuperview];
    [filtro_controller.view removeFromSuperview];

    UIBarButtonItem *Button = [[UIBarButtonItem alloc] initWithTitle:@"Filtro" style:UIBarButtonItemStyleBordered target:self action:@selector(mostra_filtro_btn:)];
    [Button setTitle:@"Filtro"];
    [Button setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName,nil]forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = Button; 
})

这是一个可能的解决方案,直到你尝试。如果您分享调用load_map的代码,那就太棒了!

干杯!

答案 3 :(得分:0)

您的代码非常完美,正如您所说的在mostra_filtro_btn :方法中添加方法viewDidLoad的按钮,以及点击按钮,导航栏中的按钮设置为nil。

但问题是,无处可调用load_map方法。一旦按钮变为nil,您需要调用此方法,并使用load_map方法将其恢复。

愿你明白。

享受编码!!