Display default tabBarView on tabBarItem Click event - objective-c

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!

Related

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;

Multi view App Programatically

I want to know if it is possible to make multi view app without using the Interface Builder.
I know how to make it with just using Xcode and Storyboards, but I just want to be able make it programmatically.
For example : If I have a UIViewController A (default view) with a UIButton and than I have another UIViewController B with UIWebView so when I click on the UIButton of UIViewController A I want to be able to display the second View (Without IB).
How can I set the action to the UIButton to display my second View (Without Interface Builder) ?
You can add a target to your button:
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
Then in ControllerA, add this method:
-(void)buttonPressed:(id)sender
{
[self.superview addSubview:controllerB.view];
[self.view removeFromSuperview];
}
On ViewControllerA
- (void)viewDidLoad
{
UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(50.0f,200.0f,60.0f,30.0f)];
[btn setTitle:#"Next" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[btn addTarget:self action:#selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(IBAction)btnPressed:(id)sender
{
NextViewController *nxt=[[NextViewController alloc]initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:nxt animated:YES];
}
On viewControllerB
- (void)viewDidLoad
{
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0, 320, 460)];
NSURL *targetURL = [NSURL URLWithString:#"http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebView_Class/UIWebView_Class.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
}
Make sure that in AppDelegate.h should be navigation controller like this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:self.self.viewController ];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}

Issues with UISplitViewController and modal UIViewControllers

I have a really strange UI glitch in my iPhone/iPad app. Because I wanted to find the cause of it, I created a new project with as little code as possible. The important code is below.
Basically, I have a UISplitViewController containing two UIViewController subclasses. When a button is tapped, the first one presents modally and as UIModalPresentationFormSheet a UIViewController subclass called Modal. In there, when a button is tapped, another UIViewController subclass called Text is presented, this time as UIModalPresentationFullScreen. In Text, there is a UITextView. When it is tapped, everything is alright, but when the iPad is rotated, I get this:
The white part is the Text view controller, the red part in the background is ViewControllerTwo.
Does anybody have any idea why this happens? And what I can do to fix it?
Here is the Project: MediaFire
Here is the relevant source code:
// AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ViewControllerOne *one = [[ViewControllerOne alloc] init];
ViewControllerTwo *two = [[ViewControllerTwo alloc] init];
UISplitViewController *split = [[UISplitViewController alloc] init];
split.viewControllers = [NSArray arrayWithObjects:one, two, nil];
self.window.rootViewController = split;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
// ViewControllerOne.m
#import "Modal.h"
#implementation ViewControllerOne
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blueColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(30, 30, 44, 44);
[button addTarget:self action:#selector(buttonTapped) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonTapped
{
Modal *modalOne = [[Modal alloc] init];
modalOne.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:modalOne animated:YES];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
// Modal.m
#import "Text.h"
#implementation Modal
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *buttonOne = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonOne.frame = CGRectMake(30, 30, 44, 44);
buttonOne.tag = 1;
[buttonOne addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
UIButton *buttonTwo = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonTwo.frame = CGRectMake(30, 100, 44, 44);
buttonTwo.tag = 2;
[buttonTwo addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonOne];
[self.view addSubview:buttonTwo];
}
- (void)buttonTapped:(UIButton *)button
{
if (button.tag == 1)
{
Text *text = [[Text alloc] init];
text.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentModalViewController:text animated:YES];
}
else
{
[self dismissModalViewControllerAnimated:YES];
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
// Text.m
#implementation Text
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
UITextView *textView = [[UITextView alloc] initWithFrame:self.view.bounds];
textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:textView];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
You may need to present the modal views from the UISplitViewController instead of it's subviews...
UISplitViewController *splitViewController = [(AppDelegate *)[[UIApplication sharedApplication] delegate] splitViewController];
[splitViewController presentModalViewController:modal animated:YES];

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.

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

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.