Reputation: 3832
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. 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. 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.
EDIT 2: I changed the code to
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
ExerciseViewController *controller = [mainStoryboard instantiateViewControllerWithIdentifier: @"id"];
[controller next];
However, still, only NSLog works. Any thoughts?
Upvotes: 0
Views: 408
Reputation: 52227
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.
ExerciseViewController *vc = [[ExerciseViewController alloc] init];
will also not create the View Controller as design in the storyboard, you have to instantiate from the storyboard.
after you created your vc
object, you will have to display it on the screen. i.e. 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. have a look on their guides. Now!
Upvotes: 3