2015-09-06 58 views
3

當我嘗試顯示一個刷新按鈕作爲rightBarButtonItem時,我有一個奇怪的問題。RightBarButtonItem只顯示爲不活動

總之,我已經實現了它,但運行應用程序時看不到任何東西。但是,當我點擊故事板Debug --> View Debugging --> Capture View Hierarchy。我可以看到一個看起來無效且隱藏的刷新按鈕。我不知道爲什麼。

enter image description here

的viewcontrol通過自定義pageviewcontroller實際上推入。

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil]; 

    self.pageController.dataSource = self; 
    [[self.pageController view] setFrame:[[self view] bounds]]; 

    TNViewController *initialViewController = [self viewControllerAtIndex:currentIndex]; 
    UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] 
             initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh 
             target:self action:@selector(refreshClicked:)]; 
    initialViewController.navigationItem.rightBarButtonItem = refreshButton; 
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:initialViewController]; 

    NSArray *viewControllers = [NSArray arrayWithObject:navigationController]; 

    [self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil]; 

    [self addChildViewController:self.pageController]; 
    [[self view] addSubview:[self.pageController view]]; 
    [self.pageController didMoveToParentViewController:self]; 

    for (UIView *subview in self.pageController.view.subviews) { 
     if ([subview isKindOfClass:[UIPageControl class]]) { 
      UIPageControl *pageControl = (UIPageControl *)subview; 
      pageControl.pageIndicatorTintColor = [UIColor blackColor]; 
      pageControl.currentPageIndicatorTintColor = [utils colorFromHexString:@"#AA3635"]; 
      pageControl.numberOfPages = _news.count; 
      pageControl.backgroundColor = [UIColor whiteColor]; 
     } 
    } 
    self.edgesForExtendedLayout = UIRectEdgeTop; 
} 

我錯過了什麼?

回答

0

我終於找到了問題。在這種情況下非常令人困惑的是,實際的TNViewcontroller被包裝在TNPViewController中。後者是PageViewController。我一直試圖在TNViewController而不是TNPViewController上添加navigationItem。

這是正確的方法,它的工作原理。

ParentViewController:

TNPViewController *tnp = [[TNPViewController alloc] initWithNews:news index:indexPath.row]; 
[[self navigationController] pushViewController:tnp animated:YES]; 

TNPViewController:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] 
             initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh 
             target:self action:@selector(refreshClicked:)]; 
    self.navigationItem.rightBarButtonItem = refreshButton; 
} 
1

因爲您將它添加到uiviewcontroller,您需要創建一個UINavigationBar,一個UINavigationItem和UIButton。然後添加在UIButton的到UINavigationItem,然後UINavigationItem添加到UINavigationBar的:

_navBar = [[UINavigationBar alloc] init]; 
[_navBar setFrame:CGRectMake(0,0,self.view.bounds.size.width,52)]; 
[self.view addSubview:_navBar]; 
UINavigationItem *navItem = [[UINavigationItem alloc]initWithTitle:@""]; 
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshClicked:)]; 
[navItem setLeftBarButtonItem:barButton]; 

[_navBar setItems:@[navItem]]; 

此代碼來了,答案在這裏找到:

adding barButtonItems to a UINavigationBar without a Navigation Controller

希望這有助於。

+0

但我利用在首位右一個UINavigationController?爲什麼我應該手動添加這些?我仍然沒有任何運氣給它一槍。 – Houman

0

您是否嘗試在實例化UINavigation控制器後添加它?

+0

我想你還不能評論,因爲你太新了。新來者的Stackoverflow的常見尷尬。在你的回答中你沒有指定我必須實例化UINavigation Controller。請問我錯過了什麼?隨意編輯主要答案,我會看到它。 – Houman