How to change the action of the first tabbaritem (when clicked) - iphone

I'm trying to show a tableviewcontroller as the first page, with the tabbarcontroller in it.
After it is shown I would like to change the action of the first item in the tabbarcontroller to show a menu.
I have created the menu, I only need to find a way to change the click event of the first item on the tabbarcontroller.
I've been searching for hours now, and help would be greatly appreciated!
Thanks in advance,
Tommy
*edit: I found the solution, I will post it tomorrow!

Implement
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
if(tabBar.selectedItem.tag == 0)
{
RootViewController *objRootViewController = [[RootViewController alloc]init];
[self.navigationController pushViewController:objRootViewController animated:YES];
[objRootViewController release];
}
}

I found out a good way to handle this problem...
I used the UITabBarController delegate and implemented this:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
if([viewController.title isEqualToString:#"YourTitleHere"])
{
// do your stuff here
}else
{
return YES;
}
}
this Worked great for me, very happy that I finally found a solution! Hope this helps others as well!

Related

Load iOS tab content only once

I'm writing an iOS app that provides a native tabbed interface to a bunch of WebViews that display content from my website. I want re-tapping the same tab to refresh the page, so I implemented (word choice? I'm a Java guy...) UITabBarDelegate in my ViewController and have this:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
AbstractPageController *otherController = (AbstractPageController *)[[self viewControllers] objectAtIndex:[[tabBar items] indexOfObject:item]];
[otherController loadPage];
}
Which loads a page in the appropriate WebView.
And, of course, to load the page in the first place (on startup), I have this in my AbstractPageController
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadPage];
}
As you might imagine, this results in the page being loaded twice if a new tab is selected. How do I avoid this? Right now I think the best idea is to remove the loadPage call from viewDidLoad, but that would mean that on startup the page didn't get loaded (right?). And it seems hacky to special-case that. What do you recommend?
I decided that the cleanest way was to simply have tabBardidSelectItem only do any work if the same tab was re-tapped. Thus:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
NSUInteger newIndex = [[tabBar items] indexOfObject:item];
if (newIndex == [self selectedIndex])
//etc
}
Works great, and doesn't feel hacky!
I believe if you invoke the method -loadView rather than your own instance method, the view will remain loaded upon presentation and the duplication of loading will not occur.
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html

Calling UIAlertView clickedbutton at index programmatically

I am trying to call UIAlertView's delegate method programmatically. Here is the code:
if([vc respondsToSelector:#selector(alertView:clickedButtonAtIndex:)]) {
// Manually invoke the alert view button handler
[(id <UIAlertViewDelegate>)vc alertView:nil
clickedButtonAtIndex:0];
}
It works fine on iOS5.0 but is not working on iOS6.0 and comments or suggestions are most welcomed :)
Here is the complete method for detail:
TWTweetComposeViewController *vc = [[[TWTweetComposeViewController alloc] init]autorelease];
// Settin The Initial Text
[vc setInitialText:status];
[vc setCompletionHandler:^(TWTweetComposeViewControllerResult result) {
if(result == TWTweetComposeViewControllerResultDone) {
NSLog(#"Tweeted Sucessfully");
}
}];
if([delegate isKindOfClass:[UIViewController class]]){
[(UIViewController *)delegate presentModalViewController:vc animated:YES];
}
//alertView:clickedButtonAtIndex:
if([vc respondsToSelector:#selector(alertView:clickedButtonAtIndex:)]) {
// Manually invoke the alert view button handler
[(id <UIAlertViewDelegate>)vc alertView:nil
clickedButtonAtIndex:0];
}
}
in you code just give the alertview with your alertview obect name like bellow..
[(id <UIAlertViewDelegate>)vc alertView:yourAlertView
clickedButtonAtIndex:0];
otherwise Just try with this bellow code..
id<UIAlertViewDelegate> delegate = yourAlertView.delegate;
yourAlertView.delegate = nil;
[delegate alertView:yourAlertView clickedButtonAtIndex:0];
see this link for some other option about it..
why-doesnt-dismisswithclickedbuttonindex-ever-call-clickedbuttonatindex
It is bad practice to directly call delegate methods. UIAlertView has a method called dismissWithClickedButtonIndex:animated:. If you call that, the UIAlertViewDelegate methods alertView:willDismissWithButtonIndex: and
alertView:didDismissWithButtonIndex: will be called, assuming your delegate is set correctly.
You can use this delegate this will work for you..
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;
There are no such differences regarding implementation of Alert view in iOS 6. You can complete your task easily by using this delegate method - :
(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;
try this and after that let us know what kind of warning you get in console...
TWTeetComposeViewController deprecated in IOS6. Please try with DETweet instead. :) Works fine on iOS 6 too. :)

view events not being called in UITabBarController

i've got a little problem with my custom UITabBarController class. My UICustomTabBarController is a subclass of UITabBarController. In my didSelectItem event I implemented the following code:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
[self showActivityIndicator];
}
I my showActivityIndicator method I add a activity indicator to my current view. It works just fine.
Now i would like to remove the activity indicator when the current view will disappear.
i found the following events:
-(void)viewDidDisappear:(BOOL)animated {
NSLog(#"hello");
}
-(void)viewWillDisappear:(BOOL)animated {
NSLog(#"hello");
}
-(void)viewWillAppear:(BOOL)animated {
NSLog(#"hello");
}
-(void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
NSLog(#"hello");
}
Unfortunately none of them are working. They are not being called. Am I doing anything wrong?
Thanks for your help!
Is the delegate property for UITabBar is set in the .h file ? ?
i.e. <UITabBarDelegate,UITabBarControllerDelegate>

tabBarController:shouldSelectViewController method doesn't fire

I have read the Apple docs - http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/TabBarControllers/TabBarControllers.html#//apple_ref/doc/uid/TP40007457-CH102-SW1 about creating TabBar programmatically. I want to detect the TabBar selection so I have used following delegate methods. I am not sure why but these methods don't get fired when I change the Tabs on my iPhone. Could anyone please provide some thought on what's going wrong here. It would be really helpful. Thanks.
- (BOOL)tabBarController:(UITabBarController *)tbController shouldSelectViewController:(UIViewController *)viewController
{
if (viewController == [tbController.viewControllers objectAtIndex:3] )
{
// Enable all but the last tab.
return NO;
}
return YES;
}
- (void)tabBarController:(UITabBarController *)tbController didSelectViewController:(UIViewController *)viewController {
if (viewController == [tbController.viewControllers objectAtIndex:self.appTabs.count] )
{
//do some action
}
}
Did you forget to set the delegate when you created the UITabBarController?
someTabBarController.delegate = self;

How to specify a group when displaying an ABPeoplePickerNavigationController

How do you specify a group when initially displaying an ABPeoplePickerNavigationController (so it doesn't automatically display "All Contacts")?
Yeah, I do. I had to make it work.
Set your class as the delegate of the people picker (pp.delegate = self;)
Then implement:
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if([navigationController.viewControllers count] > 1) {
navigationController.delegate = nil;
[navigationController popViewControllerAnimated:NO];
}
}
It seems to work best with animation off, but still works with it on but sort of goofy. Only tested on simulator.
D