简体   繁体   English

从另一个类Objective-C调用时方法不起作用

[英]Method not working when called from another class Objective-C

I have the following method in my app. 我的应用中有以下方法。

- (void)next{
    NSLog(@"called!");
    NSArray *visibleItems = [self.collectionView indexPathsForVisibleItems];
    NSIndexPath *currentItem = [visibleItems objectAtIndex:0];
    NSIndexPath *nextItem = [NSIndexPath indexPathForItem:currentItem.item + 1 inSection:currentItem.section];
    [self.collectionView scrollToItemAtIndexPath:nextItem atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];

    CATransition *fadeTextAnimation = [CATransition animation];
    fadeTextAnimation.duration = 0.2;
    fadeTextAnimation.type = kCATransitionFade;

    [self.navigationController.navigationBar.layer addAnimation: fadeTextAnimation forKey: @"fadeText"];
    self.navigationItem.title = @"Test";
}

It is working great when I call it from the class it is implemented in. However if I call it from another class, the NSLog works fine, but nothing else happens. 当我从实现它的类中调用它时,它工作得很好。但是,如果我从另一个类中调用它,则NSLog可以正常工作,但是什么也没有发生。 I guess it has to do something with self.collectionView not actually referencing my collectionView when called from another class, but I have no idea how to solve the issue. 我猜想它必须与self.collectionView做一些事情,当从另一个类调用它时,实际上并没有引用我的collectionView ,但是我不知道如何解决该问题。 Can you please point me in the right direction? 您能指出我正确的方向吗?

EDIT: This is how I call my method from another class. 编辑:这就是我从另一个类调用我的方法的方式。

ExerciseViewController *vc;
[vc next];

The method gets called fine, because NSLog works, it just that the rest of the code is not executed properly. 该方法被称为罚款,因为NSLog可以工作,只是剩下的代码没有正确执行。

EDIT 2: I changed the code to 编辑2:我将代码更改为

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
ExerciseViewController *controller = [mainStoryboard instantiateViewControllerWithIdentifier: @"id"];
[controller next];

However, still, only NSLog works. 但是,仍然只有NSLog有效。 Any thoughts? 有什么想法吗?

ExerciseViewController *vc;
[vc next];

You only declare the view controller, you don't instantiate an object. 您只声明视图控制器,不实例化对象。

vc is nil, and nil can accept any message, there for this doesn't lead to a crash. vc是nil,并且nil可以接受任何消息,因此不会导致崩溃。


ExerciseViewController *vc = [[ExerciseViewController alloc] init];

will also not create the View Controller as design in the storyboard, you have to instantiate from the storyboard. 也不会在情节提要中按设计创建View Controller,则必须从情节提要中实例化。


after you created your vc object, you will have to display it on the screen. 创建vc对象后,必须将其显示在屏幕上。 ie push it to a view controller to present it. 即将其推送到视图控制器以呈现它。 You will find tons of example codes. 您会发现大量示例代码。 you just need to search for it. 您只需要搜索它。 also it seems like you did not invest much time on https://developer.apple.com . 似乎您没有在https://developer.apple.com上花费很多时间。 have a look on their guides. 看看他们的指南。 Now! 现在!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM