使用UISegmentControl切换视图时,UISearchBar消失

时间:2016-12-12 16:46:56

标签: ios objective-c iphone

我在我创建的tableView侧栏中有一个UISearchBar(& UISearchDisplayController)(见附图)。点击我的UINavigationBar中的按钮时可以看到它。在我的Map View Controller中,我在导航栏中有一个UISegmentControl,允许我的用户在视图之间来回切换(mapView& friendsMapView)。

enter image description here

出于某种原因,UISearchBar在最初打开时在侧栏中显得很好。但是当我点击段控件切换到friendsMapView,然后返回mapView时,我的UISearchBar就消失了(见图)。

enter image description here

知道为什么会这样吗?我在StoryBoard中实现了上述功能。请参阅下面的segmentControl代码(不确定这是否有帮助):

.m

-(IBAction)segmentControl:(id)sender {
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UITableViewController *menuController;

    menuController = [storyBoard instantiateViewControllerWithIdentifier:@"neighbourMenu"];
    self.title = @"Controller 1";

    switch (self.segmentControl.selectedSegmentIndex)
    {

        case 0:

          [mapView setHidden:NO];
            [friendsMapView setHidden:YES];


            menuController = [storyBoard instantiateViewControllerWithIdentifier:@"neighbourMenu"];


            break;



        case 1: //FRIENDS MAP VIEW


            menuController = [storyBoard instantiateViewControllerWithIdentifier:@"FriendMenu"];
          //  self.title = @"Item 1";

            [mapView setHidden:YES];
            [friendsMapView setHidden:NO];


            //FRIENDS MAPVIEW LOCATION/ZOOM

            friendsMapView.delegate = self;
            self.locationManager = [[CLLocationManager alloc] init];
            self.locationManager.delegate = self;



#ifdef __IPHONE_8_0
            if(IS_OS_8_OR_LATER) {



                [self.locationManager requestWhenInUseAuthorization];
                //     [self.locationManager requestAlwaysAuthorization];
            }
#endif
            [self.locationManager startUpdatingLocation];


            friendsMapView.showsUserLocation = YES;
            [friendsMapView setMapType:MKMapTypeStandard];
            [friendsMapView setZoomEnabled:YES];
            [friendsMapView setScrollEnabled:YES];

            break;



        case 2:

            [mapView setHidden:YES];
            [friendsMapView setHidden:YES];

            break;


    }

    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:menuController];
    [self.revealViewController setRearViewController:nc];



}

1 个答案:

答案 0 :(得分:2)

我不知道您当前的viewcontroller,但似乎您在下方设置了一个新的navigationcontroller,而您当前的viewcontroller的导航栏有搜索栏,但新的navigationcontroller没有。

UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:menuController];
[self.revealViewController setRearViewController:nc];