【问题标题】:How to call method of another class which uses self in objective C如何在目标C中调用另一个使用self的类的方法
【发布时间】:2017-04-30 16:43:15
【问题描述】:

RKSwipeBetweenViewControllers 下面的类(Github 库在控制器之间滑动)它还提供了一个选项,我可以在其中点击按钮(它们是 viewControllers 的名称)我如何在每个 viewcontroller 中实现下一个按钮,这样如果用户不知道他可以滑动,他只需点击下一步按钮即可转到下一个控制器:

   -(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];
}

选择器方法如下:

-(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];
                    }
                }];
            }
        }
    }
}

现在我已经创建了一个在上述方法中创建的全局按钮数组,并将其从另一个类传递到下面的代码中。

现在我想使用下面的代码从头等舱调用上述方法:

-(void)callMethodOfSecondClass {

        UIButton *button = [globalArrayOfButtons objectAtIndex:1];

        [rkSwipeControllrObject tapSegmentButtonAction:button];

    }

现在问题显然是 self.currentPageIndex 在第二类中为零。

我如何赋予它原来的早期自我的价值。

请帮忙

【问题讨论】:

  • 您是在展示或推送第二个视图控制器吗?
  • 第二类都不是,其实是第三方类库,但我想调用第二类的方法。
  • @ios_Dev。您的问题无法完全理解。请添加更多内容以了解我们,然后我们可以为您提供帮助

标签: ios iphone methods call


【解决方案1】:

现在问题显然是 self.currentPageIndex 在第二个是 nil 类。

我如何赋予它原来的早期自我的价值。

你不能。 self 总是指拥有正在执行的方法的对象。如果self.currentPageIndexnil,那是因为您要向其发送消息methodOfSecondClass: 的对象具有nil,因为它是currentPageIndex 属性。

很难看出您的代码中发生了什么,因为您的-callMethodOfSecondClass: 方法实际上并没有调用-methodOfSecondClass:,尽管您声称相反。您能否确保您提供了显示问题的真实代码?

一个可能的问题是您遇到了一个错误身份的案例。您可能有两个(或更多!)SecondClass 实例,您知道其中一个的currentPageIndex 是有效的,但您将-methodOfSecondClass : 消息发送到与您预期不同的实例。

'currentPageIndex' 在第二类中不是 nil,只有在 它是从另一个类调用的。我明白当我 rkSwipeControllr = [二等新];'它需要新的内存,'currentPageIndex' 应该是'nil'。但是有没有办法分配它 相同的内存地址

当您说rkSwipeControllr = [SecondClass new] 时,rkSwipeControllr 获取SecondClass 实例的地址,与您之前创建的任何实例完全分开。

我认为你会从考虑类和该类的实例之间的区别中受益。 SecondClass 类定义了一个种类 的对象,但该类本身并不存储您感兴趣的信息,例如currentPageIndex。要使用该类,您必须创建该类的实例,即 typeSecondClass 的对象。

这就像 Ford F-150(一种皮卡车)和 ios_Dev's 之间的区别 福特 F-150,这是一辆真正的卡车(或者如果你开的是 F-150 的话)。假设我去经销商那里买了一架 F-150,我把它停在我的车道上,然后把我的电锯放在后面。那天晚些时候,当我需要我的电锯时,我必须去那辆特定的卡车去拿它。如果我转而去经销商处购买另一架 F-150,我无法合理地期望在后面找到我的电锯,对吧? SecondClass 的实例也是如此——如果你创建一个 SecondClass 的新实例并将其设置为 currentPageIndex,如果你想获得那个 @,你需要使用那个 same 对象987654341@ 值回。为此,您需要在创建对象时保留对对象的强引用。如果您没有至少一个对该对象的强引用,那么 nobody 将记住该对象的位置,并且 Objective-C 运行时将有助于销毁该对象。保持强引用的一种方法是在对象中标记为strong 的属性中,稍后需要引用该对象。

【讨论】:

  • 我没有发送当前页面索引,事实上我想使用第二类的 currentpageIndex。我只是在参数中传递 UIbutton 。我该怎么办?
  • @ios_Dev 在Objective-C(和其他一些语言)中“向bar发送消息foo”与“调用foo的方法bar”含义相同。很抱歉对此有任何混淆,但如果你坚持下去,你会经常遇到这种术语。但是,正如我在答案中所说,如果self.currentPageIndexmethodOfSecondClass: 中的nil,那么毫无疑问,您正在调用其方法的对象的self.currentPageIndex 具有nilcurrentPageIndex,因为self 在其方法之一的上下文中准确引用了该对象。
  • 我很乐意提出一个解决方案(这就是我们在这里的原因,对吗?)但我仍在努力理解这个问题。我将编辑上面的答案...
  • 很棒的解释先生,我知道我需要电锯,我知道我需要去车道,但我只是不知道如何去我的车道..;D @property(非原子,强) RKSwipeBetweenViewControllers *RKSwipeObject;
  • 现在 Xcode 8 毁了我的整个故事板,什么都试过了,上次备份是 4 天前,有什么办法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-02
  • 2019-08-11
  • 2019-03-11
  • 1970-01-01
  • 2019-06-27
  • 2016-01-10
  • 1970-01-01
相关资源
最近更新 更多