2014-12-15 3 views
4

내비게이션 컨트롤러 안에있는 TableViewController의 아래쪽에 툴바 항목을 표시하려고합니다. 나는 Swift에서이 코드를 작성했다.Swift를 사용하여 NavigationController에 툴바 항목을 표시 할 수 없습니다.

저는 Xcode 기본 마스터 - 세부 템플릿을 사용하여 프로젝트를 만들고 MasterTableViewController의 ViewDidLoad 메서드에 아래 코드를 작성했습니다.

문제를 해결하는 데 도움을주십시오.

다음 코드 스 니펫을 찾으십시오.

override func viewDidLoad() { 

    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    self.addToolBar(); 
} 


func addToolBar()->Void { 

     self.hidesBottomBarWhenPushed = false 

     var toolBarItems = NSMutableArray() 

     var systemButton1 = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: nil, action: nil) 
     toolBarItems.addObject(systemButton1) 

     var systemButton2 = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil) 
     toolBarItems.addObject(systemButton2) 

     var systemButton3 = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Trash, target: nil, action: nil) 
     toolBarItems.addObject(systemButton3) 

     self.navigationController?.toolbarHidden = false 
     self.setToolbarItems(toolbarItems, animated: true) 
     //self.navigationController?.toolbarItems = toolbarItems; 

    } 

은 그러나 흥미롭게도, 오브젝티브 C로 작성된 동일한 코드가 작동하고 두 항목

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    self.navigationItem.leftBarButtonItem = self.editButtonItem; 

    [self addToolbar]; 
} 

-(void) addToolbar 
{ 
    self.hidesBottomBarWhenPushed = NO; 

    NSMutableArray *items = [[NSMutableArray alloc] init]; 

    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:nil action:nil]; 
    [items addObject:item1]; 

    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
    [items addObject:item2]; 

    UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:nil action:nil]; 
    [items addObject:item3]; 


    self.navigationController.toolbarHidden = NO; 
// self.navigationController.toolbarItems = items; 
//  
    [self setToolbarItems:items animated:YES]; 
} 
+0

뷰 컨트롤러의 기본 속성으로 (대소 문자 제외) 동일하지의 이름을 사용하는 것이 좋을 것이다 일했다. 당신의 오타 (Matthias의 답변에서 지적한)는 이것 때문에 컴파일러에 잡히지 않았습니다. – rdelmar

답변

2

당신은 당신의 코드에서 작은 오타가와 하단의 도구 모음을 보여줍니다. 나는 당신을 위해 차이를 강조 :

var toolBarItems = NSMutableArray() 
// ^
// [...] 
self.setToolbarItems(toolbarItems, animated: true) 
//      ^

귀하의 코드는이 (애니메이션 포함) 기본적으로 수행합니다

self.toolbarItems = self.toolbarItems 

여러분을 설정 toolbarItems 배열을 비어 현재 toolbarItems 배열에.

self.setToolbarItems(toolBarItems, animated: true)을 사용하면 효과가 있습니다.

1

이있는 NSMutableArray 나에게 오류를 준 사용하여, 아래의 코드 나

func setUpToolbar(){ 

    var toolBarItems = [UIBarButtonItem]() 

    let systemButton1 = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: nil, action: nil) 
    toolBarItems.append(systemButton1) 

    let systemButton2 = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil) 
    toolBarItems.append(systemButton2) 

    let systemButton3 = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Trash, target: nil, action: nil) 
    toolBarItems.append(systemButton3) 

    self.setToolbarItems(toolBarItems, animated: true) 
    self.navigationController?.toolbarHidden = false 
} 
관련 문제