【问题标题】:UIWebView path depends on previous pressed button XcodeUIWebView 路径取决于先前按下的按钮 Xcode
【发布时间】:2015-06-23 10:37:19
【问题描述】:

我有一个ViewController (A),包含 n 个按钮。所有按钮都映射到包含WebView 的其他ViewController(B) 以显示不同的PDF。

我会知道如何根据按下的按钮更改路径,而不是创建 n ViewController


我的错误尝试:

1-使用ClassB中的按钮标签viewDidLoad:(id)sender(我加了sender)

([sender tag] == 1)
//Action

2- 从其他类访问公共变量

在 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"];
}

B 类:

ClassB *obj ;
NSURL *url= [NSURL fileURLWithPath:obj->path];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
[_webview loadRequest:request];
[_webview setScalesPageToFit:YES];

【问题讨论】:

  • 你使用故事板或 xib]
  • @Anbu.Karthik 我正在使用故事板

标签: ios objective-c iphone xcode uiviewcontroller


【解决方案1】:

你必须这样做:

//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 中只需使用您设置的变量。

如果您不使用 segue,您可以创建、推送 UIViewController 并在 IBAction 方法中设置变量。

【讨论】:

  • 我的意思是你应该设置一个classB的变量,而不是设置一个classA的成员变量。同样@property (readwrite, nonatomic) NSString* var; 将自动使用一个名为_var 的成员变量,除非你在setter/getter 中定义它,否则你创建的名为var 的变量将不会被属性使用(你应该得到一个警告)
  • 谢谢你让我注意到有冲突的变量名,我已经定义了一个 B 类的变量有同样的问题
【解决方案2】:

在你的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;
  }
}

【讨论】:

  • 如果您需要帮助,我一定会为您提供帮助
  • 非常感谢,模拟器上没有出现 pdf 并且警告:尝试在视图不在窗口层次结构中的 上呈现
  • 简单的兄弟,把这个方法viewdidload改成viewDidappear,等我修改我的答案
  • 对不起,同样的警告 :(,我已将 classA 更改为导航控制器,但不起作用
  • 现在我更改了 prepareForSegue 方法,请更新并检查
【解决方案3】:

如果您可以根据数字来区分要显示的网址,那么使用标签是一个理想的选择。

首先在 ViewController(B) 中声明一个简单的属性,如下所示:

@property (strong, nonatomic) NSInteger tagValue;

然后,只需为所有按钮提供特定标签,并将所有按钮连接到同一个IBAction。假设您的IBActionbuttonPressed:,您的代码将类似于:

- (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];
 }
}

【讨论】:

    猜你喜欢
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多