0

RKSwipeBetweenViewControllers class below (Github library to swipe between controllers) it also provides an option where i can tap on buttons(which are names of viewControllers) how ever i am implementing a next button in each of my viewcontroller, so that if a user doesnot know he can swipe he can simply click on next button to go to next controller:

   -(void)setupSegmentButtons {
    navigationView = [[UIView alloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.navigationBar.frame.size.height)];


    NSInteger numControllers = [viewControllerArray count];

    if (!buttonText) {
         buttonText = [[NSArray alloc]initWithObjects: @"Login",@"Personal",@"Contact",@"Club Info",nil]; //%%%buttontitle
    }

    for (int i = 0; i<numControllers; i++) {

        RKButton = [[UIButton alloc]initWithFrame:CGRectMake(X_BUFFER+i*(self.view.frame.size.width-2*X_BUFFER)/numControllers-X_OFFSET, Y_BUFFER, (self.view.frame.size.width-2*X_BUFFER)/numControllers, HEIGHT)];

        [navigationView addSubview:RKButton];

        RKButton.tag = i; //%%% IMPORTANT: if you make your own custom buttons, you have to tag them appropriately

        [RKButton setBackgroundColor:ThemeColor];//%%% buttoncolors

        [RKButton addTarget:self action:@selector(tapSegmentButtonAction:) forControlEvents:UIControlEventTouchUpInside];

        //[RKButton addTarget:self action:@selector(CalltapSegmentButtonAction) forControlEvents:UIControlEventTouchUpInside];

        [RKButton setTitle:[buttonText objectAtIndex:i] forState:UIControlStateNormal]; //%%%buttontitle

        [_RKSwipeRegisterModal.RKButtonArray addObject:RKButton];

        NSLog(@"_RKSwipeRegisterModal.RKButtonArray %@",_RKSwipeRegisterModal.RKButtonArray);

    }

    pageController.navigationController.navigationBar.topItem.titleView = navigationView;


    [self setupSelector];
}

and the selector method is below:

-(void)tapSegmentButtonAction:(UIButton *)RKButton {

    if (!self.isPageScrollingFlag) {

        NSInteger tempIndex = self.currentPageIndex;

        __weak typeof(self) weakSelf = self;

        //%%% check to see if you're going left -> right or right -> left
        if (RKButton.tag > tempIndex) {

            //%%% scroll through all the objects between the two points
            for (int i = (int)tempIndex+1; i<=RKButton.tag; i++) {
                [pageController setViewControllers:@[[viewControllerArray objectAtIndex:i]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL complete){

                    //%%% if the action finishes scrolling (i.e. the user doesn't stop it in the middle),
                    //then it updates the page that it's currently on
                    if (complete) {



                        [weakSelf updateCurrentPageIndex:i];
                    }
                }];
            }
        }

        //%%% this is the same thing but for going right -> left
        else if (RKButton.tag < tempIndex) {
            __weak typeof(self) kjhg = self;
            for (int i = (int)tempIndex-1; i >= RKButton.tag; i--) {
                [pageController setViewControllers:@[[viewControllerArray objectAtIndex:i]] direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:^(BOOL complete){
                    if (complete) {

                        [weakSelf updateCurrentPageIndex:i];
                    }
                }];
            }
        }
    }
}

now i have created a global array of button created in above method and passed it in below code from another class.

Now i want to call the above method from first class using below code:

-(void)callMethodOfSecondClass {

        UIButton *button = [globalArrayOfButtons objectAtIndex:1];

        [rkSwipeControllrObject tapSegmentButtonAction:button];

    }

Now the issue is obviously self.currentPageIndex is nil in second class.

How do i give it the value of its original earlier self.

Any help please

ios_Dev
  • 95
  • 1
  • 10

1 Answers1

0

Now the issue is obviously self.currentPageIndex is nil in second class.

How do i give it the value of its original earlier self.

You cannot. self always refers to the object that owns the method that's executing. If self.currentPageIndex is nil, that's because the object that you're sending the message methodOfSecondClass: to has nil for it's currentPageIndex property.

It's hard to see what'g going on in your code because your -callMethodOfSecondClass: method doesn't actually ever call -methodOfSecondClass: despite your claim to the contrary. Could you make sure you've provided the real code that exhibits the problem?

One possible problem is that you've got a case of mistaken identity. You may have two (or more!) instances of SecondClass, where you know the currentPageIndex of one is valid, but you're sending the -methodOfSecondClass : message to a different instance than the one you expect.

'currentPageIndex' is not nil inside second class, it is only nil when it is called from another class. i understand when i 'rkSwipeControllr = [SecondClass new];' it takes new memory, for which 'currentPageIndex' should be 'nil'. But is there any way to assign it the same memory address

When you say rkSwipeControllr = [SecondClass new], rkSwipeControllr gets the address of a new instance of SecondClass, completely separate from any you've created previously.

I think you'll benefit from considering the difference between a class and an instance of that class. The class SecondClass defines a kind of object, but the class itself doesn't store the information you're interested in, like currentPageIndex. To use the class, you have to create an instance of that class, i.e. an object whose type is SecondClass.

It's like the difference between, say, Ford F-150, which is a kind of pickup truck, and ios_Dev's Ford F-150, which is an actual truck (or would be if you drove an F-150). Let's say I go to the dealer and buy a F-150, and I park it in my driveway and put my chainsaw in the back. Later that day, when I need my chainsaw, I have to go to that particular truck to get it. If I instead go to the dealer and buy another F-150, I can't reasonably expect to find my chainsaw in the back, right? And so it is with instances of SecondClass -- if you create a new instance of SecondClass and set it's currentPageIndex, you need to use that same object if you want to get that currentPageIndex value back. To do that, you need to keep a strong reference to the object when you create it. If you don't have at least one strong reference to the object, then nobody will remember where the object is, and the Objective-C runtime will helpfully destroy the object. One way to keep that strong reference is in a property marked strong in the object that needs to refer to the object later.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • I am not sending current page index, infact i want to use the currentpageIndex of second class. I am just passing UIbutton in parameter. How do i go about it ? – ios_Dev Dec 15 '16 at 06:59
  • @ios_Dev In Objective-C (and some other languages) "sending a message `bar` to `foo`" means the same as "calling `foo`'s method `bar`". Sorry for any confusion about that, but if you keep at it you'll encounter that lingo often. As I said in the answer, though, if `self.currentPageIndex` is `nil` in `methodOfSecondClass:`, then there's no question that the `self.currentPageIndex ` of the object whose method you're calling has `nil` for the `currentPageIndex`, because `self` refers to exactly that object in the context of one of its methods. – Caleb Dec 15 '16 at 07:04
  • I'd be happy to suggest a solution (that's why we're here, right?) but I'm still trying to understand the problem. I'll edit the answer above... – Caleb Dec 15 '16 at 07:18
  • Awesome explanation sir, i know i need chainsaw and i know i need to go to the driveway, but i just dont know how do i go to my driveway..;D @property (nonatomic, strong) RKSwipeBetweenViewControllers *RKSwipeObject; – ios_Dev Dec 15 '16 at 08:57
  • Now Xcode 8 ruins my entire storyboard, have tried everything, last back up was 4 days ago, is there any way around ? – ios_Dev Dec 15 '16 at 10:20
  • i have already tried [http://stackoverflow.com/questions/37851459/the-document-main-storyboard-requires-xcode-8-0-or-later] – ios_Dev Dec 15 '16 at 10:28
  • Glad to help. There's not enough information here about your storyboard problem to know what's going on, but the question you cited certainly seems like a good start. If you're still stuck, might want to pose a new question focussed on the storyboard problem. – Caleb Dec 15 '16 at 14:31
  • i have many questions, is there any way i can ask from you,(as my questions may be silly) because the way you explain things is simply the best. StackOverflow does not provide messaging. – ios_Dev Dec 16 '16 at 10:10