How to remove a button from navigation bar?

I have been working on ipad application. In this application I have several views. here is the flow

Welcome Screen > Home Screen > rest of the sreens

I have applied a home icon (button) on navigation bar of all screens. Pressing home icon on any screen takes the user to home screen. I wrote the following code in the viewDidLoad of Home class

//**** Home button on navigation bar ****//
CGRect frame1 = CGRectMake(975.0, 4.0, 35, 35);
UIImage *buttonImage1 = [UIImage imageNamed:@"HomeIcon.png"];
UIButton *homeButton = [UIButton buttonWithType:UIButtonTypeCustom];

homeButton.frame = frame1;
[homeButton setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
homeButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
homeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[homeButton addTarget:self action:@selector(goHome:) forControlEvents:UIControlEventTouchUpInside];
[homeButton setBackgroundColor:[UIColor clearColor]];

[self.navigationController.navigationBar addSubview:homeButton];

This button is functional. goHome is the name of the method applied in the @selector. I want to remove this button from home screen and keep it on rest of the screens. I have applied several things but i have no idea how to do it. This seems very simple but still I am not getting it. Please guide..

Regards PC

Answers


In your "Home Screen" viewDidAppear method do this:

for(UIView* view in self.navigationController.navigationBar.subviews)
{
     if(view.tag == 10)
     {
        view.hidden = YES;
     }
}

In your other View controller where you created the button set tag for home button to 10.

/**** Home button on navigation bar ****//
CGRect frame1 = CGRectMake(975.0, 4.0, 35, 35);
UIImage *buttonImage1 = [UIImage imageNamed:@"HomeIcon.png"];
UIButton *homeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[homebutton setTag:10]; // Set tag to 10 or any value
homeButton.frame = frame1;
[homeButton setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
homeButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
homeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[homeButton addTarget:self action:@selector(goHome:) forControlEvents:UIControlEventTouchUpInside];
[homeButton setBackgroundColor:[UIColor clearColor]];

[self.navigationController.navigationBar addSubview:homeButton];

And in you viewDidAppear: of this other view Controller:

for(UIView* view in self.navigationController.navigationBar.subviews)
{
    if(view.tag == 10)
    {
      view.hidden = NO;
    }
}

Need Your Help

mysql - return results grouped in a column

php mysql search search-engine

I am working on a search/tag system. My original query I wrote was for when I was storing 'title', 'description' and a comma seperated 'tags' column in my article/video table. I have since realised...

Consuming a kinesis stream in python

python amazon-web-services stream boto

I cant seem to find a decent example that shows how can I consume an AWS Kinesis stream via Python. Can someone please provide me with some examples I could look into?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.