Detect which tab we are in using CAPSPageMenu in objective c - ios

I am using CAPSPageMenu with 2 tabs. Now there is an button at right bar button. When ever i click that i have an view with one button called sayHello. Now when i click that button i needs to know which tab i was in. That bar button is for both tabs.But how can i check which tab was i am when i click on that sayHello.
code :
In my homevc i added that two tabs .
VC1, VC2..
And i tried in viewwillAppear added on bool in nsuserdefault and tried to fetch. But that bool is always coming as TRUE. Which ever tab i am - still the bool values i coming as true.Here is an code :
In vc1
-(void)viewWillAppear:(BOOL)animated {
userDefault = [NSUserDefaults standardUserDefaults];
[userDefault setBool:TRUE forKey:#"fromVC1"]; // Tried true, YES also
NSLog(#"from vc1");
}
In vc2
-(void)viewWillAppear:(BOOL)animated {
userDefault = [NSUserDefaults standardUserDefaults];
[userDefault FALSE forKey:#"fromVC1"]; // Tried false, No also
NSLog(#"from vc1");
}
And i am checking like :
BOOL Val;
userDefault = [NSUserDefaults standardUserDefaults];
Val = [userDefault objectForKey:#"fromVC1"];
if (Val) {
NSLog(#"from VC1");
}else {
NSLog(#"from VC2");
}
But always its coming as True. Any idea how to get that ?Which tab i was i before. when i press my button sayHello.
- (void)didTapGoToLeft {
NSInteger currentIndex = self.pageMenu.currentPageIndex;
if (currentIndex > 0) {
[_pageMenu moveToPage:currentIndex - 1];
}
}
//
- (void)didTapGoToRight {
NSInteger currentIndex = self.pageMenu.currentPageIndex;
if (currentIndex < self.pageMenu.controllerArray.count) {
[self.pageMenu moveToPage:currentIndex + 1];
}
}

if you conform with delegate of CAPSPageMenu, you get the tab with the following two delegate methods.
// Optional delegate
- (void)willMoveToPage:(UIViewController *)controller index:(NSInteger)index {}
- (void)didMoveToPage:(UIViewController *)controller index:(NSInteger)index {}
for e.g
Intially, you nee to tell the compiler that your class implements the protocol:
#interface ViewController : UIViewController<CAPSPageMenuDelegate>
#property (nonatomic) CAPSPageMenu *pagemenu;
thereafter you need to conform the delegate
_pageMenu.delegate = self;
and finally access the delegate as your need.
(void)didMoveToPage:(UIViewController *)controller index:(NSInteger)index {
NSLog(#"controller: %#", controller);
NSLog(#"tabbed Index : %d", index);
}
for step by step intro : https://github.com/PageMenu/PageMenu/blob/master/README.md

I quite agree with the #Anbu.Karthik. Karthik implementation
But I would like to answer that you always get TRUE value when userDefault appears
Because you do not provide a full method context to read the userDefault Value.
So I don't know if it's due to the call life cycle, but if you try to write userDefault methods to viewDidAppear:(BOOL)animated, I'm sure you'll get the expected values.
Schematic code:
- (void)viewDidAppear:(BOOL)animated {
BOOL value;
NSUserDefaults* userDefault = [NSUserDefaults standardUserDefaults];
value = [userDefault objectForKey:#"fromVC1"];
}
I hope I can help you

Related

How to retrieve UISwitch state in each of my UITableView cells?

I have a UISwitch set as the accessoryView in each of my TableView cells.
If I press my Confirm button, I want to save the state of each UISwitch with NSUserDefaults. Then when I leave and go back to that View Controller, I should be able to load those saved states which will be different for each cell (either on or off, as shown in image).
I'm almost there but I guess I am not sure how to save/load with the right indexPath.row so it's not working correctly. Right now it is just saving/loading one BOOL value only, so if I save one cell with the switch ON, then all of them will be ON, and vice versa.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
BOOL menuSwitchState = [[NSUserDefaults standardUserDefaults] boolForKey:#"menuItemSwitch"];
NSLog(#"Menu Switch State is: %#", menuSwitchState ? #"Yes": #"No");
[self.switchView setOn:menuSwitchState animated:YES];
}
UISwitch code in my cellForRowAtIndexPath:
// Add a UISwitch to the accessory view.
self.switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = self.switchView;
self.switchView.tag = indexPath.row;
self.switchView.on = [[NSUserDefaults standardUserDefaults] boolForKey:#"menuItemSwitch"];
[self.switchView addTarget:self action:#selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
Switch action method which sets a BOOL:
- (void) switchChanged:(id)sender {
UISwitch *switchControl = sender;
NSLog(#"The switch's tag number is %ld", (long)switchControl.tag);
// NSLog(#"The switch is %#", switchControl.on ? #"ON" : #"OFF" );
if ([sender isOn])
{
self.switchIsOn = YES;
NSLog(#"THE SWITCH IS ON");
}
else
{
self.switchIsOn = NO;
NSLog(#"THE SWITCH IS OFF");
}
}
Confirm button that should save the state of the switch:
#pragma mark - UIBUTTONS
- (IBAction)onConfirmMenuButtonPressed:(id)sender
{
[self performSegueWithIdentifier:#"segueMenuToGettingStarted" sender:self];
//TODO: Save state of Switch.
if (self.switchIsOn == YES)
{
[[NSUserDefaults standardUserDefaults] setBool:self.switchView.on forKey:#"menuItemSwitch"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
else
{
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
[ud setBool:NO forKey:#"menuItemSwitch"];
}
}
You save that value just like any other value you use in a table view -- in an array that's your data source. Given what you show in your image, your data source should be an array of dictionaries with keys for the menu item, price, and switch state. In cellForRowAtIndexPath, you would set the state of the switch based on the value ( a BOOL) in your array.
in your code, you just set one NSUserDefault value for the all items(now, looks like three), I think you must separate each kind of item, it means you must have the count of NSUserDefault same as the count of UISwitch, In UIViewController, you can hold the NSUserDefault values in an Array, when you change the switch, you changed the cell data, when you leave the ViewController, save the Array's values. Enter the ViewController, update the Array.

xcode - Label changes value when I change views

I have searched around and cannot find anything, help would be appreciated. I am very new to objective-C and Xcode.
In my app the player starts with 100 coins, this is represented in a label. When the user clicks a button to spend 10 coins, a popup box appears and asks 'are you sure', the user can click ok, or cancel.
If the user clicks 'ok' they spend 10 coins. At the moment, in the simulator when i'm in the same view everything is fine, the 100 goes down to 90 etc...
But when I go to another view and then back again, the coin amount goes back up to 100. This is the same when the user exits the app.
Here is my code:
.h file
//Coin
IBOutlet UILabel * coinCount;
.m file
int coinAmount = 100;
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
NSLog(#"user pressed Cancel");
// Any action can be performed here
}
else
{
NSLog(#"user pressed OK");
coinAmount -= 10;
[coinCount setText:[NSString stringWithFormat:#"%d", coinAmount]];
NSString * string = [NSString stringWithFormat:#"%d", coinAmount];
//Save coin amount
NSString * saveCoinAmount = string;
NSUserDefaults * defaultsCoinAmount = [NSUserDefaults standardUserDefaults];
[defaultsCoinAmount setObject:saveCoinAmount forKey:#"saveCoinLabel"];
[defaultsCoinAmount synchronize];
}
}
That seems to save the new coin amount, so now when the user goes to another view and back i try and load the saved coin amount:
- (void)viewDidLoad
{
[super viewDidLoad];
//Coin Label
NSUserDefaults * defaultsLoadCoin = [NSUserDefaults standardUserDefaults];
NSString * loadCoinLabel = [defaultsLoadCoin objectForKey:#"saveCoinLabel"];
[coinCount setText:loadCoinLabel];
}
Any help would be greatly appreciated!
Your problem is that you are storing your coins in two places - an integer variable and the label. When you return to your view you restore the saved coin amount directly into the label, but when you perform the "purchase" you use the integer, which has been re-initialised to 100.
I also suggest you get out of the habit of using instance variables and use properties.
You should do something like this -
.m file
#interface MyClass () // Change this to suit your class name
#property NSInteger coinAmount;
#property (weak,nonatomic) IBOutlet UILabel *coinLabel;
#end
#implementation MyClass
- (void)viewDidLoad
{
[super viewDidLoad];
//Coin Label
NSUserDefaults * defaultsLoadCoin = [NSUserDefaults standardUserDefaults];
if ([defaultsLoadCoin objectForKey:#"coins] == nil) {
self.coinAmount=100;
[defaultsLoadCoin setInteger:self.coinAmount forKey:#"coins"];
}
else {
self.coinAmount = [defaultsLoadCoin integerForKey:#"coins"];
}
self.coinLabel.text=[NSString stringWithFormat:#"%ld",self.coinAmount];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
NSLog(#"user pressed Cancel");
// Any action can be performed here
}
else
{
NSLog(#"user pressed OK");
self.coinAmount -= 10;
self.coinLabel.text=[NSString stringWithFormat:#"%ld",self.coinAmount];
//Save coin amount
NSString * saveCoinAmount = string;
NSUserDefaults * defaultsCoinAmount = [NSUserDefaults standardUserDefaults];
[defaultsCoinAmount setInteger:self.coinAmount forKey:#"coins"];;
[defaultsCoinAmount synchronize];
}
Your coinAmount property is not persistent through app launches or initialisations of the view controller in which it is created. You should consider persisting this value in database (like CoreData) or in NSUserDefaults.
My advice: start with basics (links to docs):
CoreData
NSUserDefaults

how to access the value of a UISwitch

i have created a settingsView and also a view that gives you a choice to be notified if you want at a certain time its a yes or no but i do not know how to access the value of the uiswitch i know instead of trying to get the views object i should try the preference that the state has been saved to but i don't know how i just want to tap my UISwitch and then in the settingsView the UILabel gets filled but i don't know how to access the value on the UISwitch once tapped i have saved it in NSUserDefaults in the notifiedView but do not know how to access that value in the settingsView so i can put in the settingsView
if (UISwitch.on)
{
//notify me please
} else {
// leave as is
}
of course that's just a quick layout of how i want it to be though and also where do i put the it in the settingsView viewDidLoad, viewWillAppear or both please help im a new be as well so please be kind if i have said things that you would deem not the right syntax thanks
Try this code:
// add this code in your switch touch event
- (IBAction)YourSwitch:(UISwitch*)sender
{
if (UISwitch.on)
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:true forKey:#"Sound"];
[defaults synchronize];
}
else
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:false forKey:#"Sound"];
[defaults synchronize];
}
}
Add this code in your viewdidload .
BOOL swithState = [[NSUserDefaults standardUserDefaults] boolForKey:#"Sound"];
if (swithState)
{
_ref_slider.on=true;
}
else
{
_ref_slider.on=false;
}
i hope this code is useful for you.
Just make a UISwitch property in your UIViewController and make sure you set it in the init/viewDidLoad of your class (from an IBOutlet or when you programmatically add it to your view). Then you can access the on-property of the UISwitch from anywhere within your class (and a load of extra features!) and use the if-statement as you want..
Apple Doc ref: https://developer.apple.com/library/ios/documentation/uikit/reference/UISwitch_Class/Reference/Reference.html
If your are using storyboard then hook up your UISwitch's action and write the below code in it.
- (IBAction)switchTapped:(id)sender
{
if ([sender isOn]) {
// your code
}
else
{
// your code
}
}

don't show again ViewController

In my app, I created a first ViewController (in storyboard) called welcome, with all the instructions. At the end of the page, I wanted to insert a box that could be selected showing "don't show again". By clicking on this box, and going to the next page, the first viewController will vanish. When I reopen the app, the controller won't be shown again. The only way to show it is by going on settings and selecting the switch "repristinate original settings" or something like that. Please, can anyone help me? Thanks!
I found an example but is not what I want:
- (IBAction)leggi{
NSString *stringaTesto = campo.text;
testo.text = [[NSString alloc] initWithFormat:#"%#", stringaTesto];
NSString *testoInserito = testo.text;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:testoInserito forKey:#"ciao"];
[defaults synchronize];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *testoSalvato = [[NSUserDefaults standardUserDefaults] objectForKey:#"ciao"];
if (testoSalvato == nil) {
testo.text = #"Non hai ancora inserito il tuo nome";
} else {
testo.text = [[NSString alloc] initWithFormat:#"Ciao %#", testoSalvato];
}
}
The IB Outlet is linked to a button
store the don't show again as a BOOL in NSUserDefaults.. and check it before showing the view..
if it is TRUE..dont show.other wise show it.
edit
Lets say you have your app delegate and currently show your don't show view again (Lets say A) from it ..after which you show another view(Lets say B)
then in your app delegate you have to get a BOOL
like this
BOOL _Dont_Show_Again = [NSUSerDefaults standardDefaults] boolForkey : #"Don't Show"];
if(_Dont_Show_Again)
{
load B Code here...
}
else
{
load A Code here;
}
first time _Dont_Show_Again will be 0 since it does not exist in default..but if user select don't show you should save it in the default and this code will then work fine for you

iOS - Login view in UITabBarControler best practice

I've tabbar controller with 6 to 7 tabs on it with customization allowed (means that the user can change the order of tab bar display). And tab 1,3,5,7 requires the user to login access before seeing any contents on the view.
I don’t know as to how to identify which tabbar requires login access (Can we create #protocal or something). And after the user logs in successfully. I don’t know how to select the last tabbar, which the user has touched on.
I was using appdelegate by confiming to UITabBarControllerDelegate in AppDelegate
-(BOOL)tabBarController:(UITabBarController *)aTabBar shouldSelectViewController:(UIViewController *)viewController {
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
BOOL hasValidLogin = [standardUserDefaults boolForKey:#"hasValidLogin"];
if (hasValidLogin == NO && /* I don't know to find a controller which requires login access*/) {
[LoginView showModal:viewController delegate:self];
return NO;
}
return YES;
}
pragma mark -
pragma mark LoginDelegate methods-
-(void)loginViewController:(LoginView *)loginViewController didLoginSuccess:NSString *)userName {
NSLog((#"%s [Line %d] "), __func__, __LINE__);
NSLog(#"userName = %#", userName);
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
[standardUserDefaults setBool:YES forKey:#"hasValidLogin"];
[standardUserDefaults synchronize];
[self.tabBarController setSelected:/* How do i find the last touched tabbar*/];
}
Any good practice,recipes and code snippet you can direct me is appreciated.
I was reading matt's iOS Recipes. But I couldn't find anything related to this one.
Focus on UITabBarDelegate didSelectItem. Keep track of the previously selected tab item. If the user is not authenticated, pop up your authentication screen and select the previously selected tab item. Here is an example.
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
switch ( item.tag ) {
case ME_TAB_BAR_TAG:
if ( [AppHelper checkAuthentication] ) {
[self showMe:YES];
} else if ( previousItem ) {
[tabBar setSelectedItem:previousItem];
return;
}
break;
case ITEMS_TAB_BAR_TAG:
[self showMe:NO];
break;
case CREATE_TAB_BAR_TAG:
if ( [AppHelper checkAuthentication] ) {
[self createNewItem];
} else if ( previousItem ) {
[tabBar setSelectedItem:previousItem];
return;
}
break;
}
previousItem = item;
}

Resources