2015-06-23 29 views
0

我有一個ViewController (A),包含n個按鈕。所有按鈕映射到包含WebView的其他ViewController(B)以顯示不同的PDF。UIWebView路徑取決於上一次按下的按鈕Xcode

而不是創建n ViewController,我會知道如何根據按下哪個按鈕來更改路徑。


我錯了嘗試:

1按鈕的使用在ClassB的viewDidLoad:(id)發送者(我添加發送器)與其他類

標籤

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

2-接入公共變量class Ah

@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]; 
+0

RU使用故事板或XIB] –

+0

@ Anbu.Karthik我'使用故事板 –

回答

1

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; 
    } 
} 
+0

如果你需要幫助我當然會協助你 –

+0

非常感謝,pdf不是出現在模擬器上並警告:試圖在上呈現,其視圖不在窗口層次結構中! –

+0

簡單的兄弟,改變這種方法viewdidload到viewDidappear,只是等待我修改我的答案 –

2

你必須做一些事情,如:

//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方法中設置變量。

+0

我的意思是你應該設置classB的變量,而不是設置classA的成員變量。另外'@property(readwrite,nonatomic)NSString * var;'會自動使用名爲'_var'的成員變量,你創建的變量var'將不會被屬性使用,除非你在setter/getter中定義它你應該得到一個警告) – Daniel

+0

謝謝你讓我注意到有衝突的變量名稱,我已經定義了一個變量classB與同樣的問題 –

1

如果你能有所不同,你要根據數字位,顯示該網址,那麼使用標籤是一個理想的選擇。

首先在視圖控制器(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]; 
} 
} 
+0

謝謝你,我看到它類似於Anbu.Karthik解答 –