Implementing UIBarButtonItem
Today we will see how we can display a Button on a Navigation Bar.
Lets continue from the Previous Tutorial Displaying a UITableView. If you haven’t had a chance to look at it do visit it to get a brief idea of the same.
Open up HomeScreenTableViewController.m file. We should be able to see a method viewDidLoad as commented.
/*
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem; } */
Go ahead and uncomment the method and add the following lines of code as shown below
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle:@"Click" style:UIBarButtonItemStyleBordered target:self action:@selector(myButtonClicked:)];
self.navigationItem.rightBarButtonItem = myButton;
[myButton release];
// Uncomment the following line to display an Edit button in the navigation bar for this view controller. // self.navigationItem.rightBarButtonItem = self.editButtonItem; }
Here we are creating a new Button of type UIBarButtonItem. We then provide a Title which will be displayed to the user and also we specify the target as self. Hence when an user clicks on the Button, the Button will notify this program by calling the method myButtonClicked.
Click on Build and Run. You should be able to see a Button on the Right side of the Navigation Bar.
Click on the Button and we should see the App crashing and some log messages displayed on the Console as given below:
2010-03-03 05:58:26.065 NavigationButton[284:207] *** -[HomeScreenTableViewController myButtonClicked:]: unrecognized selector sent to instance 0x501cc80
2010-03-03 05:58:26.067 NavigationButton[284:207] *** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘*** -[HomeScreenTableViewController myButtonClicked:]: unrecognized selector sent to instance 0x501cc80′
2010-03-03 05:58:26.075 NavigationButton[284:207] Stack: (
40655403,
2424304905,
40925099,
40389990,
40386658,
2762594,
4803290,
2762594,
3222313,
3231075,
3226262,
2882811,
2780545,
2811225,
48992936,
40196222,
40192120,
48986701,
48986898,
2803293,
8732,
8586
)
From the log above, we could see that the button tried to send a message to HomeScreenTableViewController by was unable to find it. Lets go ahead and implement the method as given below. Add the following lines of code before the dealloc method.
#pragma mark Button methods
- (void)myButtonClicked:(UIBarButtonItem *)myButton
{
NSLog(@"My Button Clicked!");
}
Click on Build and Run. This time we should get My Button Clicked message at the console.
[Session started at 2010-03-03 06:22:36 +0530.]
2010-03-03 06:22:39.633 NavigationButton[324:207] My Button Clicked!
Thats it for this tutorial. Hope you enjoyed.
Thanks
| Print article |


about 2 years ago
Most certainly agree with those things you affirmed. Your own explanation was definitely the easiest to fully grasp. I actually tell you, I quite often get mad once others talk over issues that they obviously really don’t have knowledge of. You managed to give reasons for every thing without requiring complications.
about 2 years ago
Thanks
about 1 year ago
I tried this but I get unrecognized selector sent to instance…Using IOS 5.1 on an Ipad 1…Xcode 4.3…Here is my code:
-(void)addSortButton
{
//if(sortButtonLoaded==NO)
//{
UIBarButtonItem *sortButton = [[UIBarButtonItem alloc] initWithTitle:@”Sort By Date” style:UIBarButtonItemStyleBordered target:self action:@selector(sortCoverage:)];
self.navigationItem.rightBarButtonItem = sortButton;
[sortButton release];
}
-(void)sortCoverage
{
if(sortType==@”ByDate”)
{
sortType = @”ByName”;
self.navigationItem.rightBarButtonItem.title=@”By Name”;
}
else {
sortType = @”ByDate”;
self.navigationItem.rightBarButtonItem.title=@”By Date”;
}
//Call Sort on the Search Results now…
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//TODO:::
}
about 1 year ago
Hey Mark
Thanks for reply. We went through your code and found this.
You have “:” in @selector(sortCovrerage:)
Hence the method name should be changed to
-(void)sortCoverage:(id)sender {
//Existing Code
Thanks
Innovate Labs Team