How to pass data viewcontroller via Tab bar Controller - uitabbarcontroller

I want to pass datum from view controller to view controller via tab bar controller.
I mean, my storyboard is like this.
View Controller → Tab Bar Controller → VC1/VC2/VC3
For the purpose of this, I tried to use PrepareforSegue, but it's failed.
The code is here.
(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
friendsDetailViewController* vc = [[friendsDetailViewController alloc] init];
vc = (friendsDetailViewController*)[segue destinationViewController];
vc.candidateid = [[NSString alloc] init];
vc.candidateid = [candidateid objectAtIndex:selectedIndexPath.row];
Though I was trying to pass data from VC to VC1, SIGABT happened on the last line.
Update:
I've got figured out how. Here is the script:
friendsDetailViewController* vc = [[friendsDetailViewController alloc] init];
UITabBarController* tbc = [segue destinationViewController];
vc = (friendsDetailViewController*)[[tbc customizableViewControllers] objectAtIndex:0];
Firstly, I have to attach Tab Bar Controller.
Secondly, retrieve each View Controller's pointer.
You can select by using objectIndex.

In childviewcontrollers of tab bar controllers you can easily pass data by using
[self.parentViewController.view setAccessibilityValue:#"your data"];
and can easily access this data in other child viewcontrollers of tabbar
or you can add an object in nsuserdefaults for some key and attach that key in parentviewcontroller.view ..
get that object by using objectforkey from nsuserdefaults in next viewcontroller

I had same problem.
I had NSString which i waned to be sent like this:
View Controller → Tab Bar Controller → VC1/VC2/VC3
Before anything else I created ViewController-s and TabBarController classes and connected them with storyboard.
This worked for me:
First lets send data from ViewController to TabBarController:
add segue to connect ViewController and TabBarController in storyboard and name it:
toMyTabController
add property u want to change in TabBarController:
#property (strong, nonatomic) NSString *name;
add this method somewhere in ViewController.m:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"toMyTabController"])
{
MyTabController *tabController = (MyTabController *)segue.destinationViewController;
tabController.name = #"SOME AWESOME NAME";
}
}
==================================================================================
Now lets send data from TabBarController to ViewController
Add property to every ViewController where you want to send info, and lets name it name (so creative)
add this to **ViewController1.h, ViewController2.h, ViewController3.h
#property (strong, nonatomic) NSString *name;
add this to ViewController1.m, ViewController2.m, ViewController3.m
#synthesize name;
go to your TabBarController.m and add this in ViewDidLoad method:
for (UIViewController* myController in self.viewControllers)
{
if([myController respondsToSelector:#selector(setName:)])
{
[myController performSelector:#selector(setName:) withObject:self.name];
}
}
Thats all,
Hope it helps. Good Luck

Related

Instantly remove AdMob banner?

I've made a game in using cocos2d and have integrated an admob banner in the appdelegate so that it is displayed on all view controllers. Ive implemented a remove ads feature which happens in the "game over" screen. Here is my problem. The remove ads function works but does not take affect immediately. I have to kill the app and restart it for the banner to go away. Is there a way to just reload the appdelegate and the ads are removed instantly instead of having to reload the app? PLEASE HELP This is driving me crazy.
EDITED: HERE IS MY CURRENT CODE. THE BANNER LOADS WHEN THE APP LOADS I AM TRYING TO MAKE THE BANNER GO AWAY WHEN THE USER PURCHASES TO REMOVE IT WITHOUT HAVING TO RESTART THE APP(CLOSE THE APP RESTART)
AppDelegate.h
#import "GADBannerViewDelegate.h"
// Added only for iOS 6 support
#interface MyNavigationController : UINavigationController <CCDirectorDelegate>
#end
#class RootViewController;
#class GADBannerView, GADRequest;
#interface AppController : NSObject <UIApplicationDelegate,ADBannerViewDelegate,UIActionSheetDelegate, GKLeaderboardViewControllerDelegate, GameCenterManagerDelegate,ChartboostDelegate,GADBannerViewDelegate>
{
UIWindow *window_;
MyNavigationController *navController_;
CCDirectorIOS *director_;
UIViewController *tempVC; // weak ref
RootViewController *viewController_;
//Admob
GADBannerView *bannerView_;
BOOL isAdPositionAtTop_;
}
AppDelegate.m i use calladmob to start ads if removeads is "no"
-(void)callAdMob{
//Admob ads
CGPoint origin = CGPointMake(0.0,self.window.frame.size.height -CGSizeFromGADAdSize(kGADAdSizeSmartBannerPortrait).height);
bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait origin:origin];
bannerView_.adUnitID = #"idhere";
bannerView_.delegate = self;
[bannerView_ setRootViewController:tempVC];
CGRect adBannerViewFrame = [bannerView_ frame];
int lSizeValue=0;
if([[CCDirector sharedDirector] winSize].width==320)
lSizeValue=50;
else if([[CCDirector sharedDirector] winSize].width==768)
lSizeValue=66;
adBannerViewFrame.origin.x = 0;
adBannerViewFrame.origin.y = [[CCDirector sharedDirector] winSize].height-lSizeValue;
[bannerView_ setFrame:adBannerViewFrame];
[[self navController].view addSubview:bannerView_];
[bannerView_ loadRequest:[self createRequest]];}
Why not send a notification from your "remove ads" function?
And then anywhere you have an AdMob banner, you can add an observer that watches for that notification and the method that gets called would do something like:
[bannerView removeFromSuperview] or bannerView.hidden = YES
provided you have your AdMob banner connected to an IBOutlet.

Create a left pointed UIBarButton in a UINavigation Bar Programmatically

I'm trying to create an app with a settings page just like IOS's native settings app. However I cannot find out how to create a UIBarButton that is left pointed. Basically I want a left pointed button on the top of a navigation bar, that you can set to call a function. I am using the engine cocos2d, if this helps. I am creating the entire app programmatically.
Here is my code for creating the UINavigationBar...
UINavigationBar *naviBarObj = [[[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, 320, 44)] autorelease];
[[[CCDirector sharedDirector] openGLView] addSubview:naviBarObj];
[naviBarObj release];
UINavigationItem *item = [[[UINavigationItem alloc] initWithTitle:#"Title"] autorelease];
UIBarButtonItem *back = [[[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(back)] autorelease];
item.leftBarButtonItem = back;
[naviBarObj setItems:[NSArray arrayWithObjects:item, nil] animated:YES];
Although this creates a square back button.
My goal is to create something like this
I am aware that a UINavigationItem
UINavigationItem *back2 = [[[UINavigationItem alloc] initWithTitle:#"Settings"] autorelease];
with [naviBarObj setItems:[NSArray arrayWithObjects:back,item, nil] animated:YES];
will create that effect, but unfortunately I am not currently using a UINavigationController class. This is all made in a CCLayer.
Thank You!!
One way would be to use a custom back image, but that would be kind of tedious.
Another way would be like exactly what you mentioned in your last part, to create another "dummy" previous navigation item with the title set to the text you want to be on your back button, and then adding both items to the UINavigationBar.
UINavigationItem *item = [[[UINavigationItem alloc] initWithTitle:#"Title"] autorelease];
UINavigationItem *back2 = [[[UINavigationItem alloc] initWithTitle:#"Settings"] autorelease];
[naviBarObj setItems:[NSArray arrayWithObjects:back2,item, nil] animated:YES];
However I don't think you will need a UINavigationController for these to happen. In order to handle your back button event, I suppose you can try making your CCLayer a delegate of UINavigationBar, ie. UINavigationBarDelegate:
#interface MyLayer : CCLayer <UINavigationBarDelegate> {
}
then setting the delegate of your UINavigationBar to that layer:
naviBarObj.delegate = self;
and finally implement didPopItem to get notified of the event when the button is clicked:
- (void) navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item
{
[self back]; // or whatever
}
Let me know if this works.

UINavigationBar Not Visible With UITabBarController [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Trying to construct a tableview with a navigation bar at top
I have got a UITabBarController with a TabBar in it. Now what I need is a UINavigationBar on top of controller just to show title and a button. For that, I have taken a stand alone UINavigationBar, but it is not visible I can't understand why.
I am sorry but haven't found any appropriate solution up till now. Is there any specific way to use and access stand alone UINavigationBar with UITabBarController?
Create your UIViewControllers and there views using Interface Builder or coding to be displayed in each of the Tabs. Add the NavigationBar to each one of these views.
Then add these UIViewControllers to the UITabViewController's viewControllers NSArray.
ie. In you AppDelegate's application didFinishLaunching function
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
Add the UINavigationBar to the viewController1 and viewControllers2 views, either using IB or coding it.
Following link worked for me perfectly!
Trying to construct a tableview with a navigation bar at top

iOS 5 Storyboard: NavigationViewController to TabbarController - Not working , Why?

I am using iOS5 with Storyboard and my scenes are like this :
NavigationCOntroller ->(Nav view 1) TableViewController -> TabBarController ->(Tab1)TableViewController
and similarly I more tabs under my TabBarController.
Now I go to Tab1 when user clicks on any row in my TableViewCOntroller and before PerformingSegue I want to send some data to my Tab1(TableViewController) like this
MyTableVController *tvc = segue.destinationViewController;
tvc.selectedObject = currentObject;
[UITabBarController setSelectedObject:]: unrecognized selector sent to instance 0x68c9450
Now why is it assuming that 'MyTableVController' is a UITabBarController and searching for setSelectedObject method ???
And how can I pass data to my TableViewCOntroller in this scenario ?
Thanks.
OK I have found solution to my problem , I did like this
UITabBarController *tabController = (UITabBarController *)segue.destinationViewController;
MyTableVController *tvc = (MyTableVController *)[viewControllers objectAtIndex:0];
And thats how you have your required viewcontroller and pass data to it.

UITabBarController moreNavigationController as a grouped TableView?

I am using a UITabBarController that has more than 5 items, so it shows the more tab.
Is it possible to make this moreNavigationController be a grouped TableView (uitableviewstylegrouped)? Currently it defaults to a normal table view.
Try giving exactly 5, instead of giving more than 5 and let the 5th be the host of your grouped table view controller
The following code snippet does precisely that. It uses the instance of UITabBarController (here called controller) to get access to the navigation bar and tableview controller using the UITabBarController moreNavigationController property.
#define BACKGROUNDCOLOUR [UIColor colorWithRed:253.0/255.0 green:255.0/255.0 blue:240.0/255.0 alpha:1.0]
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//... other init code
UINavigationBar *moreNavBar = controller.moreNavigationController.navigationBar;
moreNavBar.tintColor = [UIColor blackColor];
UITableView *moreTableView = (UITableView *)controller.moreNavigationController.topViewController.view;
[moreTableView initWithFrame:CGRectZero style:UITableViewStyleGrouped];
[moreTableView setBackgroundColor:BACKGROUNDCOLOUR];
[self.window addSubview:controller.view];
[self.window makeKeyAndVisible];
return YES;
}