Can we disabled the navigation controller of leftBarButtonItem that is back button of the view controller in iPhone? - objective-c

I want to disabled the default back button of navigation controller
self.navigationItem.rightBarButtonItem.enabled = NO;
// Below code does not work since leftBarButtonItem is always nil.
self.navigationItem.leftBarButtonItem.enabled = NO;
I have done it with manually shown below, But Is there any property to disabled the default back button with just single line?
backButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 100, 30)];
[backButton setBackgroundImage:[UIImage imageNamed:#"backbutton_100.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
[backButton setTitle:#" All Customers" forState:UIControlStateNormal];
backButton.titleLabel.font = [UIFont boldSystemFontOfSize:12];
[buttonView addSubview:backButton];
UIBarButtonItem* leftButton = [[UIBarButtonItem alloc] initWithCustomView:buttonView];
self.navigationItem.leftBarButtonItem = leftButton;
[leftButton release];
// Now it is working.
self.navigationItem.leftBarButtonItem.enabled = NO;

Its very easy ..... just try this out
self.navigationController.navigationBar.userInteractionEnabled = NO; //for disabling
self.navigationController.navigationBar.userInteractionEnabled = YES; //for again enabling

Using "hidesBackButton=YES" is really not an elegant solution, cause it HIDES the button which is not what we want. An acceptable work-around would be adding a UILabel to the window just over the back button at least disabling the touches on the button.
Add this method to your AppDelegate class:
- (void) disableLeftBarButtonItemOnNavbar:(BOOL)disable
{
static UILabel *l = nil;
if (disable) {
if (l != nil)
return;
l = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 160, 44)];
l.backgroundColor = [UIColor clearColor];
l.userInteractionEnabled = YES;
[self.window addSubview:l];
}
else {
if (l == nil)
return;
[l removeFromSuperview];
[l release];
l = nil;
}
}
You can call it like this from any view controller to disable:
MyAppDelegate *appDeleg = (MyAppDelegate *) [[UIApplication sharedApplication] delegate];
[appDeleg disableLeftBarButtonItemOnNavbar:YES];
To enable:
MyAppDelegate *appDeleg = (MyAppDelegate *) [[UIApplication sharedApplication] delegate];
[appDeleg disableLeftBarButtonItemOnNavbar:NO];

Call [self.navigationItem setHidesBackButton:YES]; for the view controller you do not want to have the back button. Then set the leftBarButtonItem as normal.

You can also use
[[_navigationController.topViewController.navigationItem leftBarButtonItem] setEnabled:NO]; // The top view controller on the stack.
[[_navigationController.visibleViewController.navigationItem leftBarButtonItem] setEnabled:NO];// Return modal view controller if it exists. Otherwise the top view controller.
you can use this when you want to disable or enable UIViewControler from Appdelegate, or any other viewcontroler.

Related

i need a popupview when clicking button and again clicking i want to disapper that view in objective c

Here is the code that I am using for popup:
-(void)popup:(id)sender
{
//button action
UIView *popView=[[UIView alloc]initWithFrame:CGRectMake(Screen_Width-200,40, self.view.frame.size.width-130, self.view.frame.size.height/2-60)];
[popView setBackgroundColor:[UIColor redColor]];
popView.userInteractionEnabled = YES;
[self.view addSubview: popView];
}
You can do something like this in your viewDidLoad. So it's hidden bydefault.
UIView *popView=[[UIView alloc]initWithFrame:CGRectMake(Screen_Width-200,40, self.view.frame.size.width-130, self.view.frame.size.height/2-60)];
[popView setBackgroundColor:[UIColor redColor]];
popView.hidden = YES;
popView.userInteractionEnabled = YES;
[self.view addSubview: popView];
On your button click show it like,
popView.hidden = NO;
And take UITapGestureRecognizer on main View or root view and on touches call,
popView.hidden = YES;
Exaample of TapGestureRecognizer
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleSingleTap:)];
[self.view addGestureRecognizer:singleFingerTap];
[self.view setUserInteractionEnabled:YES];
//The event handling method
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
popView.hidden = YES;
}
Hope this will help :)

How to Show and Hide UISearchDisplayController's UISearchBar

I have a button search that located in the right side of the navigation.
This is my code:
UIButton *btnSearch = [UIButton buttonWithType:UIButtonTypeCustom];
btnSearch.frame = CGRectMake(0, 0, 22, 22);
[btnSearch setImage:[UIImage imageNamed:#"search_btn.png"] forState:UIControlStateNormal];
[btnSearch addTarget:self action:#selector(showSearch:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *searchItem = [[UIBarButtonItem alloc] initWithCustomView:_btnSearch];
self.navigationItem.rightBarButtonItems = searchItem ;
This is how it's look.
And I want to display search bar after clicked on the search button, and close it after clicked on cancel and then show the Navigationbar back, but I don't know how to code it.
- (IBAction)showSearch:(id)sender{
???
}
This what I want.
Please help or provide some sample code. I really need it.
Thank for reading.
Add a property UISearchBar *mySearchBar to your viewController as
#property(nonatomic, retain) UISearchBar *mySearchBar;
Conform to UISearchBarDelegate as
#interface HomeViewController () <UISearchBarDelegate>
...
...
#end
Then implement the showSearch method as
-(void)showSearch:(id)sender {
if(!mySearchBar) {
mySearchBar = [[UISearchBar alloc] init];
[mySearchBar setFrame:CGRectMake(0, 0, self.view.frame.size.width, 55)];
[mySearchBar setShowsCancelButton:YES animated:YES];
[self.view addSubview: mySearchBar];
mySearchBar.delegate = self;
self.navigationController.navigationBarHidden = YES;
}else {
searchBar.alpha = 1.0;
self.navigationController.navigationBarHidden = YES;
}
Then implement the search bar delegate method as :
- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar {
self.navigationController.navigationBarHidden = NO;
[mySearchBar setAlpha:0.0];
}
Hope you have got the idea by doing this. Also you can add it directly to navigation controller, itself, if you want & then play with Showing/ hiding the searchBar alone.
You can add it to navigation controller as just initialize the mysearchBar & add it to navigationBar as :
UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc] initWithCustomView:mySearchBar];
self.navigationItem.rightBarButtonItem = searchBarItem;

Display default tabBarView on tabBarItem Click event

I have used UITabbarController in my app.One of the tabBarItem is contactsViewController which displays list of contacts with UITableView.When I click on the tableRow it loads another view.then I click some other tabBarItem.again I click contactsViewController it takes me to the view where i left.It does not display default contact view.I have created UITabbarController progrmattically.How do i display default tabBarView on tabBarItem click?
tabbarController = [[UITabBarController alloc]init];
self.tabbarController.delegate = self;
tabbarView = [[UIView alloc]initWithFrame:CGRectMake(0, 431, 320, 49)];
UIImageView *tabImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 49)];
[tabImage setImage:[UIImage imageNamed:#"Taskbar.png"]];
[tabbarView addSubview:tabImage];
UIButton *tabItem1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 64, 49)];
[tabItem1 setImage:[UIImage imageNamed:#"Btn_Home.png"] forState:UIControlStateNormal];
[tabItem1 setTag:1];
[tabItem1 addTarget:self action:#selector(tabBarBtnAction:) forControlEvents:UIControlEventTouchUpInside];
[tabbarView addSubview:tabItem1];
-(IBAction)tabBarBtnAction:(id)sender
{
UIButton *btn = (UIButton *)sender;
// NSLog(#"tag %d\n",btn.tag);
[self resetTabBarBtnImage];
[self resetAllTabBarBtnImage];
PreviousBtnTag = btn.tag;
if ([btn tag]==1) {
tabbarView.hidden = YES;
[self.tabbarController setSelectedIndex:0];
[self.navigationController popToRootViewControllerAnimated:YES];
[btn setImage:[UIImage imageNamed:#"Btn_Home-Over.png"] forState:UIControlStateNormal];
}
else if([btn tag]==2)
{
tabbarView.hidden = NO;
[self.tabbarController setSelectedIndex:1];
[btn setImage:[UIImage imageNamed:#"Btn_Contacts-Over.png"] forState:UIControlStateNormal];
[self.navigationController popToRootViewControllerAnimated:YES];
}
In the appDelegate class , initially set the UItabbarController delegate as self
In the implememtation of appDelegate i.e .m file
//Assuming the first tab has the contactsviewController
-
(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if (tabBarController.selectedIndex == 0) {
UINavigationController *requiredNavigationController = [tabBarController.viewControllers objectAtIndex:0];
[requiredNavigationController popToRootViewControllerAnimated:YES];
}
}
I would recommend you to create the "tab bar controller" with the Interface Builder, as it is much easier to implement these kind of things this way. Anyway, if you want to keep trying to create it programmatically, try changing this:
[self.navigationController popToRootViewControllerAnimated:YES];
for this:
RootViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"Root"];
[self.navigationController pushViewController:controller animated:YES];
If this does not work for you, why don't you use Tool Bar Items, in instead of a Tab Bar Controller? It is quite easy to manage events with Tool Bar items, as they are treated like buttons.
Good luck!

Controls created on runtime has no window. Popovers cannot be presented from a view which does not have a window

i have button on the view and call following void to open popover controller with image picker, no problem here popover come up with image picker etc.
when i create UIButton programmatically on viewDidLoad the buttons calls same void below however this time that throws an exception "Popovers cannot be presented from a view which does not have a window."
-(IBAction)pickupPhoto:(id)sender
{
CGRect rect = CGRectMake(100, 100, 1, 1); // [(UIScrollView *)sender frame];
self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.delegate = self;
self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.popOver = [[UIPopoverController alloc] initWithContentViewController:imgPicker];
[popOver setDelegate:self];
[popOver presentPopoverFromRect:rect inView:[self view] permittedArrowDirections:UIPopoverArrowDirectionUp animated:NO];
[imgPicker release];
}
here i am creating buttons on same view buttons call the void.
-(void)addButtons
{
for (int i = 0; i < 10; i++) {
self.btn = [[UIButton alloc] initWithFrame:CGRectMake(i * 100, 0, 100, 80)];
[[self.btn layer] setBorderColor:RGB(245, 245, 245).CGColor];
[[self.btn layer] setBorderWidth:1.0f];
[[self.btn titleLabel] setFont:[UIFont fontWithName:#"Thonburi" size:70]];
[self.btn setTitle:doubleToString(i+1) forState:UIControlStateNormal];
[self.btn setTitleColor:RGB(245, 245, 245) forState:UIControlStateNormal];
[self.btn setTitle:doubleToString(i+1) forState:UIControlStateSelected];
[self.btn setTitleColor:RGB(125, 225, 225) forState:UIControlStateHighlighted];
[self.btn addTarget:[NMBPostUI alloc] action:#selector(pickupPhoto:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[btn release];
}
}
i can see when i call -(void)addButtons, self.view.window returns nil for the buttons created at runtime that causes the problem. yes i should create buttons during init but there is no init call when you call view controller as -storyBoard- Page Sheet. how can i walk around this?
any ideas are welcome.
many thanks.
Try creating buttons in:
initWithNibName:bundle:
or
awakeFromNib
methods. Maybe this will work.

How to add button to toolbar using Three20?

I'm trying to use this to add button to toolbar:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setToolbarHidden:NO animated:NO];
self.navigationController.toolbar.translucent = YES;
self.navigationController.toolbar.barStyle = UIBarStyleBlack;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0.0, 0.0, 10.0, 10.0);
UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithCustomView:button];
NSMutableArray *a = [NSMutableArray arrayWithObject:infoButton];
[self.navigationController setToolbarItems:a];
}
But there is no button in toolbar when I started application! :(
Instead of setting the navigation controllers toolBarItems property try setting it in the currently displayed view controller like so:
[self setToolbarItems:[NSArray arrayWithObjects:infoButton, nil]];
Also adding infoButton to the toolBarItems will automatically retain it. So remember to place
[infoButton release];
at the bottom of your viewWillAppear method.