1

私はIOSとObjectiveCを初めて使用します。シナリオは、UIWebviewを含む2つのビューコントローラーを(セグで)開く2つのボタンがあることです。1つのUIWebViewでそれを行う方が良いと思ったので、webvewのリクエストオブジェクトを渡し、1つのwebviewコントローラーのみを使用しようとしました。

そのため、wwwBtn(サイトを開くUIButton)とfbBtn(FacebookのURLに移動するUIButton)を取得し、viewControllerとUIWebViewを含むwwwWebViewControllerを取得しました。

これが私がそれをした方法です。

viewController.hファイル:

@interface ViewController : UIViewController {
    UIButton *wwwBtn;
    UIButton *fbButton;
}
@property (retain, nonatomic) IBOutlet UIButton *wwwBtn;
@property (retain, nonatomic) IBOutlet UIButton *fbBtn;

ViewController.mファイル:

#import "ViewController.h"
#import "wwwWebViewController.h"

@implementation ViewController

@synthesize wwwBtn;
@synthesize fbBtn;

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{        
    if ([[segue identifier] isEqualToString:@"web"]) {
            NSString *urlstr=@"http ://www.google.com";
            wwwWebViewController *vc = [segue destinationViewController];
            vc.urlStr = urlstr;
        } else if ([[segue identifier] isEqualToString:@"fb"]) { 
            NSString *urlstr = @"http://www.facebook.com";
            wwwWebViewController *vc = [segue destinationViewController];
            vc.urlStr = urlstr;
        }
    }

wwwWebViewController.hファイル:

@interface wwwWebViewController : UIViewController {
    UIWebView *webView;
}

@property (retain, nonatomic) IBOutlet UIWebView *webView;

wwwWebViewController.mファイル:

    - (void)viewDidLoad
   {
        NSURL *url = [NSURL URLWithString:urlStr];
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
        [webView loadRequest:requestObj];
        [super viewDidLoad];
    }
4

1 に答える 1

4

次のようにして、 NSString *urlstrを渡す必要がある宛先 UIViewController にこのクラスを割り当てます。

WebViewController.h

@interface WebViewController : UIViewController {
    NSString *urlstr;
}

@property (strong, nonatomic) IBOutlet UIWebView *WebView;
@property (strong, nonatomic) NSString *urlstr;

WebViewController.m

@implementation WebViewController

@synthesize WebView;
@synthesize urlstr;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    NSURL *url = [NSURL URLWithString:urlstr];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [self.WebView loadRequest:requestObj];

}

ここで、メイン ストーリーボード クラス InfoViewController から宛先クラス WebViewController への 2 つのセグエを作成します。私の場合、3 つのセグエを作成しましたが、WebViewController クラスを使用しているのは 2 つだけです。

ここに画像の説明を入力

また、セグエにそれぞれの空間識別子を与えて、メイン クラスの InfoViewController でプログラムによって対象にする必要があります。

InfoViewController : UITableViewController がどのように見えるかを見てみましょう:

InfoViewController.m

#import "InfoViewController.h"
#import "WebViewController.h"

@interface InfoViewController ()

@end

@implementation InfoViewController

@synthesize AboutCell = _AboutCell;
@synthesize CGCell = _CGCell;
@synthesize FTACell = _FTACell;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *AboutCellClicked = [self.tableView cellForRowAtIndexPath:indexPath];
    UITableViewCell *CGCellClicked = [self.tableView cellForRowAtIndexPath:indexPath];
    UITableViewCell *FTACellClicked = [self.tableView cellForRowAtIndexPath:indexPath];

    if (AboutCellClicked == _AboutCell) {
        [self performSegueWithIdentifier:@"AboutPush" sender:self];
    } else if (CGCellClicked == _CGCell) {
        [self performSegueWithIdentifier:@"CGPush" sender:self];
    } else if (FTACellClicked == _FTACell) {
        [self performSegueWithIdentifier:@"FTAPush" sender:self];
    }

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    WebViewController *webController = [[WebViewController alloc] init];

    if ([[segue identifier] isEqualToString:@"CGPush"]) {
        NSString *urlstr=@"http://www.google.com";
        webController = [segue destinationViewController];
        webController.urlstr = urlstr;
    } else if ([[segue identifier] isEqualToString:@"FTAPush"]) {
        NSString *urlstr=@"http://www.yahoo.com";
        webController = [segue destinationViewController];
        webController.urlstr = urlstr;
    }
}

@end

これで問題が解決することを願っています。ご不明な点がございましたら、お気軽にお問い合わせください。

于 2012-12-05T12:56:05.173 に答える