如何调用在 objective C 中使用 self 的另一个 class 的方法
How to call method of another class which uses self in objective C
RKSwipeBetweenViewControllers class 下面(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];
}
}];
}
}
}
}
现在我已经创建了一个在上面的方法中创建的全局按钮数组,并将它从另一个 class.
传递到下面的代码中
现在我想使用下面的代码从第一个 class 调用上面的方法:
-(void)callMethodOfSecondClass {
UIButton *button = [globalArrayOfButtons objectAtIndex:1];
[rkSwipeControllrObject tapSegmentButtonAction:button];
}
现在的问题显然是 self.currentPageIndex 在第二个 class 中为零。
我如何赋予它原来的早期自我的价值。
请帮忙
Now the issue is obviously self.currentPageIndex is nil in second
class.
How do i give it the value of its original earlier self.
你不能。 self
始终引用拥有正在执行的方法的对象。如果 self.currentPageIndex
是 nil
,那是因为您要向其发送消息 methodOfSecondClass:
的对象具有 nil
,因为它是 currentPageIndex
属性。
很难看出您的代码中发生了什么,因为您的 -callMethodOfSecondClass:
方法实际上从未调用过 -methodOfSecondClass:
,尽管您的声明与此相反。您能确定您提供了出现问题的真实代码吗?
一个可能的问题是您遇到了错误身份的情况。您可能有两个(或更多!)SecondClass
实例,其中您知道其中一个的 currentPageIndex
是有效的,但您将 -methodOfSecondClass :
消息发送到与另一个实例不同的实例你期待。
'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
当你说 rkSwipeControllr = [SecondClass new]
时,rkSwipeControllr
得到 new 实例的地址 SecondClass
,完全独立于任何你已经之前创建。
我认为考虑 class 和 class 实例之间的区别会让您受益。 class SecondClass
定义了一个 kind 的对象,但是 class 本身并不存储你感兴趣的信息,比如 currentPageIndex
。要使用 class,您必须创建 class 的实例,即 type 为 SecondClass
.
的对象
这就像 Ford F-150 和 皮卡车之间的区别,ios_Dev's Ford F-150,这是一辆真正的卡车(或者如果你开的是 F-150 的话)。假设我去经销商那里买了一辆 F-150,我把它停在车道上,把电锯放在后面。那天晚些时候,当我需要电锯时,我必须去那辆特定的卡车上拿。如果我转而去经销商处购买另一台 F-150,我就不能合理地期望在后面找到我的电锯,对吗? SecondClass
的实例也是如此——如果您创建 SecondClass
的新实例并将其设置为 currentPageIndex
,您需要使用 same 对象,如果你想取回 currentPageIndex
值。为此,您需要在创建对象时保留对该对象的 强引用 。如果您没有对该对象的至少一个强引用,那么 没有人 会记住该对象的位置,并且 Objective-C 运行时将有助于销毁该对象。保持强引用的一种方法是在对象中标记为 strong
的 属性 中,稍后需要引用该对象。
RKSwipeBetweenViewControllers class 下面(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];
}
}];
}
}
}
}
现在我已经创建了一个在上面的方法中创建的全局按钮数组,并将它从另一个 class.
传递到下面的代码中现在我想使用下面的代码从第一个 class 调用上面的方法:
-(void)callMethodOfSecondClass {
UIButton *button = [globalArrayOfButtons objectAtIndex:1];
[rkSwipeControllrObject tapSegmentButtonAction:button];
}
现在的问题显然是 self.currentPageIndex 在第二个 class 中为零。
我如何赋予它原来的早期自我的价值。
请帮忙
Now the issue is obviously self.currentPageIndex is nil in second class.
How do i give it the value of its original earlier self.
你不能。 self
始终引用拥有正在执行的方法的对象。如果 self.currentPageIndex
是 nil
,那是因为您要向其发送消息 methodOfSecondClass:
的对象具有 nil
,因为它是 currentPageIndex
属性。
很难看出您的代码中发生了什么,因为您的 -callMethodOfSecondClass:
方法实际上从未调用过 -methodOfSecondClass:
,尽管您的声明与此相反。您能确定您提供了出现问题的真实代码吗?
一个可能的问题是您遇到了错误身份的情况。您可能有两个(或更多!)SecondClass
实例,其中您知道其中一个的 currentPageIndex
是有效的,但您将 -methodOfSecondClass :
消息发送到与另一个实例不同的实例你期待。
'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
当你说 rkSwipeControllr = [SecondClass new]
时,rkSwipeControllr
得到 new 实例的地址 SecondClass
,完全独立于任何你已经之前创建。
我认为考虑 class 和 class 实例之间的区别会让您受益。 class SecondClass
定义了一个 kind 的对象,但是 class 本身并不存储你感兴趣的信息,比如 currentPageIndex
。要使用 class,您必须创建 class 的实例,即 type 为 SecondClass
.
这就像 Ford F-150 和 皮卡车之间的区别,ios_Dev's Ford F-150,这是一辆真正的卡车(或者如果你开的是 F-150 的话)。假设我去经销商那里买了一辆 F-150,我把它停在车道上,把电锯放在后面。那天晚些时候,当我需要电锯时,我必须去那辆特定的卡车上拿。如果我转而去经销商处购买另一台 F-150,我就不能合理地期望在后面找到我的电锯,对吗? SecondClass
的实例也是如此——如果您创建 SecondClass
的新实例并将其设置为 currentPageIndex
,您需要使用 same 对象,如果你想取回 currentPageIndex
值。为此,您需要在创建对象时保留对该对象的 强引用 。如果您没有对该对象的至少一个强引用,那么 没有人 会记住该对象的位置,并且 Objective-C 运行时将有助于销毁该对象。保持强引用的一种方法是在对象中标记为 strong
的 属性 中,稍后需要引用该对象。