UIWebView 路径取决于之前按下的按钮 Xcode
UIWebView path depends on previous pressed button Xcode
我有一个 ViewController (A)
,包含 n 个按钮。所有按钮都映射到包含 WebView
的其他 ViewController(B)
以显示不同的 PDF。
而不是创建 n ViewController
,我会知道如何根据按下的按钮更改路径。
我的错误尝试:
1-使用ClassB中按钮的标签viewDidLoad:(id)
发件人(我添加发件人)
([sender tag] == 1)
//Action
2- 从其他 class
访问 public 变量
在 class A.h
@interface ClassA : UIViewController
{
@public
NSString *path;//was var
}
@property (readwrite, nonatomic) NSString* path;
ClassA.m
- (IBAction)button1:(UIButton *)sender{
path=[[NSBundle mainBundle]
pathForResource:@"filename" ofType:@"pdf"];
}
在class B:
ClassB *obj ;
NSURL *url= [NSURL fileURLWithPath:obj->path];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[_webview loadRequest:request];
[_webview setScalesPageToFit:YES];
您必须执行以下操作:
//implement prepareForSegue at the ViewController with the buttons
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:@"button1Segue"]) {
DesitnationController *controller = (DesitnationController *)segue.destinationViewController;
//set some public variable to know which button called the view
//or directly set a URL or do whatever you need to do. Example:
controler.pdfUrl = @"http://www.pdfsite.com/pdfForButton1.pdf";
}
}
在目标中 ViewController 只需使用您设置的变量。
如果您不使用 segues,您可以创建、推送 UIViewController 并在 IBAction
方法中设置变量。
在你的ClassA.m
- (IBAction)button1:(UIButton *)sender{
path=[[NSBundle mainBundle]
pathForResource:@"filename" ofType:@"pdf"];
[self performSegueWithIdentifier:@"yourIdentifierName" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:@"yourIdentifierName"]) {
classB *clsB =segue.destinationViewController;
clsB.typeofSelect=path;
}
}
在你的 class B.h
@property (nonatomic, weak) NSString *typeofSelect;
在你的 Class B.m
@synthesize typeofSelect;
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if (typeofSelect )
{
NSURL *url= [NSURL fileURLWithPath: typeofSelect];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[_webview loadRequest:request];
[_webview setScalesPageToFit:YES];
}
else
{
// no path found;
}
}
如果您可以根据数字来区分要显示的 url,那么使用标签是一个理想的选择。
首先在 ViewController(B) 中声明一个简单的 属性,如下所示:
@property (strong, nonatomic) NSInteger tagValue;
然后简单地为所有按钮提供特定标签并将所有按钮连接到同一个 IBAction
。假设您的 IBAction
是 buttonPressed:
,您的代码将类似于:
- (IBAction)buttonPressed:(UIButton *)sender{
[self performSegueWithIdentifier:@"loadNextPage" sender:sender];
}
最后,在您的 prepareForSegue:
方法中,执行以下操作:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:@"loadNextPage"]) {
UIButton *button = (UIButton *)sender;
ViewControllerB *controller = segue.destinationViewController;
controller.tagValue = [button tag];
}
}
我有一个 ViewController (A)
,包含 n 个按钮。所有按钮都映射到包含 WebView
的其他 ViewController(B)
以显示不同的 PDF。
而不是创建 n ViewController
,我会知道如何根据按下的按钮更改路径。
我的错误尝试:
1-使用ClassB中按钮的标签viewDidLoad:(id)
发件人(我添加发件人)
([sender tag] == 1)
//Action
2- 从其他 class
访问 public 变量在 class A.h
@interface ClassA : UIViewController
{
@public
NSString *path;//was var
}
@property (readwrite, nonatomic) NSString* path;
ClassA.m
- (IBAction)button1:(UIButton *)sender{
path=[[NSBundle mainBundle]
pathForResource:@"filename" ofType:@"pdf"];
}
在class B:
ClassB *obj ;
NSURL *url= [NSURL fileURLWithPath:obj->path];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[_webview loadRequest:request];
[_webview setScalesPageToFit:YES];
您必须执行以下操作:
//implement prepareForSegue at the ViewController with the buttons
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:@"button1Segue"]) {
DesitnationController *controller = (DesitnationController *)segue.destinationViewController;
//set some public variable to know which button called the view
//or directly set a URL or do whatever you need to do. Example:
controler.pdfUrl = @"http://www.pdfsite.com/pdfForButton1.pdf";
}
}
在目标中 ViewController 只需使用您设置的变量。
如果您不使用 segues,您可以创建、推送 UIViewController 并在 IBAction
方法中设置变量。
在你的ClassA.m
- (IBAction)button1:(UIButton *)sender{
path=[[NSBundle mainBundle]
pathForResource:@"filename" ofType:@"pdf"];
[self performSegueWithIdentifier:@"yourIdentifierName" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:@"yourIdentifierName"]) {
classB *clsB =segue.destinationViewController;
clsB.typeofSelect=path;
}
}
在你的 class B.h
@property (nonatomic, weak) NSString *typeofSelect;
在你的 Class B.m
@synthesize typeofSelect;
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if (typeofSelect )
{
NSURL *url= [NSURL fileURLWithPath: typeofSelect];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[_webview loadRequest:request];
[_webview setScalesPageToFit:YES];
}
else
{
// no path found;
}
}
如果您可以根据数字来区分要显示的 url,那么使用标签是一个理想的选择。
首先在 ViewController(B) 中声明一个简单的 属性,如下所示:
@property (strong, nonatomic) NSInteger tagValue;
然后简单地为所有按钮提供特定标签并将所有按钮连接到同一个 IBAction
。假设您的 IBAction
是 buttonPressed:
,您的代码将类似于:
- (IBAction)buttonPressed:(UIButton *)sender{
[self performSegueWithIdentifier:@"loadNextPage" sender:sender];
}
最后,在您的 prepareForSegue:
方法中,执行以下操作:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:@"loadNextPage"]) {
UIButton *button = (UIButton *)sender;
ViewControllerB *controller = segue.destinationViewController;
controller.tagValue = [button tag];
}
}