2012-04-11 7 views
6

Jestem nowy w rozwoju iOS. Stworzyłem pasek nawigacyjny w moim widoku aplikacji na iPada. Nie potrzebuję kontrolera nawigacyjnego, dlatego dodałem tylko pasek nawigacyjny. Teraz chcę dodać przycisk w tym pasku nawigacyjnym. Próbowałem dużo, ale bez powodzenia. Czy można dodać tylko pasek nawigacyjny za pomocą przycisku? Jeśli tak, zasugeruj mi przykładowy kod.Jak dodać przycisk paska w pasku nawigacyjnym bez kontrolera nawigacyjnego.

"Nie mam kontrolera nawigacyjnego lub go potrzebuję, chcę tylko dodać pasek nawigacyjny tylko w jednym widoku."

Bellow jest mój kod, który piszę do dodawania pasek nawigacyjny w viewDidLoad()

UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1026, 50)]; 
[navBar setTintColor:[UIColor blackColor]]; 
[navBar setDelegate:self]; 
[self.view addSubview:navBar]; 

Dzięki z góry ....

Odpowiedz

1

Możesz dodać przyciski do paska nawigacyjnego w następujący sposób:

UIBarButtonItem *btnSave = [[UIBarButtonItem alloc] 
           initWithTitle:@"Save" 
           style:UIBarButtonItemStyleBordered 
           target:self 
          action:@selector(save_Clicked:)]; 
navBar.rightBarButtonItem = btnSave; 
[btnSave release]; 

UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] 
           initWithTitle:@"Cancel"          
           style:UIBarButtonItemStyleBordered 
           target:self 
           action:@selector(cancel_Clicked:)]; 
navBar.leftBarButtonItem = btnCancel; 
[btnCancel release]; 
+2

w powyższym kodzie ta linia powoduje błąd. 'navBar.rightBarButtonItem = btnSave;' error: Problem semantyczny: Właściwość 'rightBarButtonItem' nie została znaleziona na obiekcie typu 'UINavigationBar *' –

+0

możesz również dodać pasek nawigacyjny do pliku Xib, możesz dodać do niego element przycisku paska. –

5
UIBarButtonItem *bi1 = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(editButton)]; 

bi1.style = UIBarButtonItemStyleBordered; 
bi1.tintColor = [UIColor colorWithWhite:0.305f alpha:0.0f]; 

self.navigationItem.rightBarButtonItem = bi1; 

[bi1 release]; 
11

Użyłem Konstruktora interfejsów, aby utworzyć statyczną tabelęView w kontrolce UITableViewController. Ten UITableViewController jest pokazany modalnie. Następnie dodałem nawigacyjny pasek nawigacji bez kontrolki UINavigationController w następujący sposób:

//Creating the plain Navigation Bar 
UINavigationBar *headerView = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 

//The UINavigationItem is neede as a "box" that holds the Buttons or other elements 
UINavigationItem *buttonCarrier = [[UINavigationItem alloc]initWithTitle:@"Sign-In"]; 

//Creating some buttons: 
UIBarButtonItem *barBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Zurück" style:UIBarButtonItemStyleDone target:self action:@selector(signInBackPressed:)]; 
UIBarButtonItem *barDoneButton = [[UIBarButtonItem alloc] initWithTitle:@"Fertig" style:UIBarButtonItemStylePlain target:self action:@selector(signInDonePressed:)]; 

//Putting the Buttons on the Carrier 
[buttonCarrier setLeftBarButtonItem:barBackButton]; 
[buttonCarrier setRightBarButtonItem:barDoneButton]; 

//The NavigationBar accepts those "Carrier" (UINavigationItem) inside an Array 
NSArray *barItemArray = [[NSArray alloc]initWithObjects:buttonCarrier,nil]; 

// Attaching the Array to the NavigationBar 
[headerView setItems:barItemArray]; 

// Adding the NavigationBar to the TableView 
[self.tableView setTableHeaderView:headerView]; 

Mam nadzieję, że to pomoże komuś!