iPhone NavigationBar With ToolBar
Posted on: September 16, 2009 at 12:00 AM
Posted on: September 16, 2009 at 12:00 AM
In this tutorial we will learn how to use two Bar i.e. Navigation Bar & Tool Bar and will add buttons to Bar and also the action to it.
iPhone Navigation Bar With Tool Bar
In this tutorial we will learn how to use two Bar i.e. Navigation bar And Tool Bar alongwith we will add buttons to Bar and also the action to it. To do this we are using Navigation Based Application this is because in Navigation Based Application will have navigation bar so there is no need to add Navigation bar will add ToolBar to the project and on that ToolBar will add a Button which shows different view.
Final View of Project will look like:
My project name is NavigationAndToolbar and is Based on Navigation Based Application, after creating just run and see view, View will have one Navigation Bar and a Table View with no data on it therefore the Table view cell will be empty. Now that you know how your application is, will change it into our requirement by adding few codes and also by adding ViewController to the project and Name it as NewViewController. This ViewController will be used to show another view when button on ToolBar is pressed. In RootViewController will add some code with the help of that code will add ToolBar and also will add or insert object into the Table View, As you know that in Navigation Application there is Table View added so there is no need to add separately Tableview on View the only thing we have to do is add or insert data into the cells of TableView.
In .h file will add ToolBar And the NavigationController,NewViewController variable is created so that variable will be called when the button on ToolBar is pressed and then it will show Next View, add this into RootViewController.h file.
UIToolbar *toolbar;
UINavigationController *mNavController;
NewViewController *mControllerToolbar;
In RootViewController.m file add the following code Number of rows in section write return 3 and in number of section in Table view write return 10.
In ViewDidLoad method set the title for the Navigation Bar and also set the Navigation Item i.e BarButton as shown below.
iPhone Navigation Bar With Tool Bar
In this tutorial we will learn how to use two Bar i.e. Navigation bar And Tool Bar alongwith we will add buttons to Bar and also the action to it. To do this we are using Navigation Based Application this is because in Navigation Based Application will have navigation bar so there is no need to add Navigation bar will add ToolBar to the project and on that ToolBar will add a Button which shows different view.
Final View of Project will look like:
My project name is NavigationAndToolbar and is Based on Navigation Based Application, after creating just run and see view, View will have one Navigation Bar and a Table View with no data on it therefore the Table view cell will be empty. Now that you know how your application is, will change it into our requirement by adding few codes and also by adding ViewController to the project and Name it as NewViewController. This ViewController will be used to show another view when button on ToolBar is pressed. In RootViewController will add some code with the help of that code will add ToolBar and also will add or insert object into the Table View, As you know that in Navigation Application there is Table View added so there is no need to add separately Tableview on View the only thing we have to do is add or insert data into the cells of TableView.
In .h file will add ToolBar And the NavigationController,NewViewController variable is created so that variable will be called when the button on ToolBar is pressed and then it will show Next View, add this into RootViewController.h file.
UIToolbar *toolbar;
UINavigationController *mNavController;
NewViewController *mControllerToolbar;
In RootViewController.m file add the following code Number of rows in section write return 3 and in number of section in Table view write return 10.
In ViewDidLoad method set the title for the Navigation Bar and also set the Navigation Item i.e BarButton as shown below.
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.title = @"Technology";
}
And in that will add ToolBar with code in ViewWillAppear, In that will Initialize the Toolbar, set ToolBar by its size, height and creating button on ToolBar, Adding ToolBar as a subview to the Navigation controller after that reloading the Table view.
- (void)viewWillAppear:(BOOL)animated
{ [super viewWillAppear:animated];
toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleDefault;
[toolbar sizeToFit];
CGFloat toolbarHeight = [toolbar frame].size.height;
CGRect rootViewBounds = self.parentViewController.view.bounds;
CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);
CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);
CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight);
[toolbar setFrame:rectArea];
UIBarButtonItem *About = [[UIBarButtonItem alloc]initWithTitle:@"About" style:UIBarButtonItemStyleBordered target:selfaction:@selector(info_clicked:)];
[toolbar setItems:[NSArray arrayWithObjects:About,nil]];
[self.navigationController.view addSubview:toolbar];
[self.tableView reloadData]; }
There is a method for the About button which is used to show the next view and in that we are creating variable for ViewController and loading its nib file. First Initializing the New View Controller and after that Initializing the Navigation controller with the New View Controller.
- (void) About:(id)sender
{ if(mControllerToolbar == nil)
mControllerToolbar = [[NewViewController alloc]initWithNibName:@"NewViewController" bundle:[NSBundle mainBundle]];
mControllerToolbar.PushedView = NO;
if(mNavController == nil)
mNavController = [[UINavigationController alloc]initWithRootViewController:mControllerToolbar];
[self.navigationController presentModalViewController:mNavControlleranimated:YES]; }
And in NewViewController.h file will add boolean function variable and the property for that as we declared the property in .h file we have to synthesize it into .m file.
BOOL PushedView;
@property (nonatomic, readwrite) BOOL PushedView;
In ViewDidLoad method of .m file will have if condition and in that Bar button is added with cancel name so that when we move to next view there is cancel button by which we can come to previous view, cancel_Clicked method we are going to come back to our view i.e. first view or previous view this is done by using dismissModalViewController and whenever the button is clicked will call this action and in this action there is a method by which on clicking cancel button will move to previous view.
- (void)viewDidLoad
{ if(PushedView == NO)
{ self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:selfaction:@selector(cancel_Clicked:)] autorelease];
} }
-(void) cancel_Clicked:(id)sender
{ [self.navigationControllerdismissModalViewControllerAnimated:YES]; }
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{ return (interfaceOrientation == UIInterfaceOrientationPortrait); }
After All this click Build And Go Button
0 comments:
Post a Comment