I have two viewControllers. ViewController1
and ViewController2
.
In viewController1
I create a UIWebView
called webView
.
This is viewController1.h
:
#import "viewController2.h"
@class viewController2
@interface viewController1 : UIViewController <UIWebViewDelegate>
{
...
}
@property (nonatomic, strong)UIWebView *webView;
- (void)goToPerc:(float)perc;
This is viewController1.m
:
- (void)viewDidLoad {
...
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
webView.delegate = self;
webView.scrollView.delegate = self;
[self.view addSubview:webView];
...
}
- (void)goToPerc:(float)perc {
CGPoint scrollToPoint = CGPointMake(0, (int)(self.webView.scrollView.contentSize.height * (perc / 100)));
NSLog(@"%@", webView);
[webView.scrollView setContentOffset:scrollToPoint animated:YES];
}
This is viewController2.h
:
#import <UIKit/UIKit.h>
#import "viewController1.h"
@interface viewController2 : UIViewController
{
...
}
This is viewController2.m
:
...
- (void)blaBla
{
viewController1 *vc1 = [[viewController1 alloc] init];
float scrollToPerc = 50.0;
[vc1 goToPerc:scrollToPerc];
NSLog(@"%@", vc1); // returns viewController1 in log
}
The NSLog(@"%@", webView);
in (void)goToPerc:(float)perc
returns null. And the scrolling doesn't work obviously. Why? All the other functions in viewController1
don't return null on webView
and viewDidLoad
on viewController1
is called so webView
does exist!
I also tried self.webView
, no success either.
i think with this you are creating a new viewcontroller, insead accessing the previous one
viewController1 *vc1 = [[viewController1 alloc] init];
float scrollToPerc = 50.0;
[vc1 goToPerc:scrollToPerc];
NSLog(@"%@", vc1);
you could try
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc1 = [storyboard instantiateViewControllerWithIdentifier:@"YourSecondviewcontrollerIdentifier"];
float scrollToPerc = 50.0;
[vc1 goToPerc:scrollToPerc];
NSLog(@"%@", vc1);