In this post we will have a look at something called as UIBarButtonItem
Design Phase:We will have two objects of UIBarButtonItem in the Navigation Bar and will try to change the background color of the view when an appropriate BarButton will be selected.
So for this demo we will need two objects of UIBarButtonItem, and a Navigation Controller as the bar buttons will be placed in the navigation bar. So here's how our final design will look like
Final Design |
Step 1: Open Xcode create a new project and save it with an appropriate name, now add a UIViewController subclass file and save it with the name myviewController
Step 2: Go to the .h file of myviewController and declare the objects of UIBarButtonItem class and of course we will be requiring two methods which will change the background color for us on the selection of the UIBarButtonItem, so here's how everything looks so far
@interface myviewController : UIViewController {
UIBarButtonItem *leftbarButton,*rightbarButton;
}
-(void) leftbarMethod;
-(void) rightbarMethod;
@end
Step 3: now go to the .m file of the myviewController class and select its init method and add this piece of code
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
//setting the text and functions for the UIBarButtonItem
leftbarButton = [[UIBarButtonItem alloc]initWithTitle:@"Black"style:UIBarButtonItemStylePlain target:self action:@selector(leftbarMethod)];
rightbarButton = [[UIBarButtonItem alloc]initWithTitle:@"Red"style:UIBarButtonItemStylePlain target:self action:@selector(rightbarMethod)];
//setting the bar button items for the navigation controller
self.navigationItem.leftBarButtonItem = leftbarButton;
self.navigationItem.rightBarButtonItem = rightbarButton;
//setting the title of the view
self.title= @"Bar button";
}
return self;
}
The above code is quite self explanatory what I have done is I have allocated memory for both the UIBarButtonItem and in my next step I have added those barbutton item to the navigationbar using the navigationItem property of the UIViewController.
Now about those two methods well they are also quite easy and here's the code that they contain
-(void) leftbarMethod
{
//changing the back ground color of the view
[self.view setBackgroundColor:[UIColor blackColor]];
}
-(void) rightbarMethod
{
//changing the back ground color of the view
[self.view setBackgroundColor:[UIColor redColor]];
}
Step 4: Now all you have to do is add this view to your main window by going into the AppDelegate.m file and this is how you will do that
#import "UIBarDemoAppDelegate.h"
#import "myviewController.h"
@implementation UIBarDemoAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after application launch
myviewController *obj = [[myviewController alloc]init];
UINavigationController *nav = [[UINavigationControlleralloc]initWithRootViewController:obj];
[window addSubview:nav.view];
[window makeKeyAndVisible];
}
In the above code you are creating the object of myviewController class and in the next line you are creating the object of UINavigationController and suppling the details of the root view controller which will appear when the application runs.
Step 5: Press Build and Go and you will get the following output
i hope this post was helpful for you
No comments:
Post a Comment