0

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];
    }
}
david
  • 636
  • 2
  • 12
  • 29

2 Answers2

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

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • So should i needs to save that `Index == \(index)` in user default. And needs to access. Or directly i can access in my button action ? – david Feb 22 '19 at 05:57
  • no need , if you want to print this you can directly called the delegate , the delegate will respond for your tapped action – Anbu.Karthik Feb 22 '19 at 05:58
  • i needs to check in my button action. Not to any other fucntion. say i have an button action called `sayHelloClick`. here how can i check which index was i am ? – david Feb 22 '19 at 05:59
  • Not inside this delegate method. outside of ibaction needs to access which index or tab i was in ? – david Feb 22 '19 at 06:00
  • @david - simple , then use `CAPSPageMenu.currentPageIndex` , it will give the current page Index, you will use on your button action call – Anbu.Karthik Feb 22 '19 at 06:02
  • Oh okay.. Let me check it. – david Feb 22 '19 at 06:08
  • @david - welcome bro, in this problem solved a : https://stackoverflow.com/questions/54745952/not-able-to-show-the-alert-if-user-denied-the-camera-access/54747567#54747567 – Anbu.Karthik Feb 22 '19 at 09:02
  • Any solution for this https://stackoverflow.com/questions/54930376/notificationcenter-is-not-getting-trigger-in-my-obj-c-class-utills-class-or-ob – david Mar 01 '19 at 07:15
0

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

Jersey
  • 391
  • 1
  • 4
  • 14
  • Any help on https://stackoverflow.com/questions/54930376/notificationcenter-is-not-getting-trigger-in-my-obj-c-class-utills-class-or-ob – david Mar 01 '19 at 07:15