如何从目标c中的另一个函数访问实例?

时间:2015-03-07 06:13:20

标签: ios objective-c iphone cocoa

我正在尝试从另一个类访问webView对象。

  

viewController.m

    @property (strong, nonatomic) TOWebViewController *webViewController;

    @end

    @implementation ViewController

    - (void)viewDidLoad {

            [super viewDidLoad];

            self.webViewController = [[TOWebViewController alloc]  initWithURLString:[NSString stringWithFormat:@"http://www.%@/", url] ];

            [self setViewControllers:@[_webViewController]];

        }

- (void)request {

     NSURL *url=[NSURL URLWithString:@"http://www.google.com"];

     NSURLRequest *request=[NSURLRequest requestWithURL:url];

     [_webViewController.webView loadRequest:request];

 }

TOWebViewController具有访问webview的webView属性。当调用(void)请求时,某些内容开始加载,但它不会将其加载到viewDidLoad中创建的webview中。我如何在(void)请求中引用正确的webViewController?

3 个答案:

答案 0 :(得分:1)

我只是查看了github上的TOWebviewController,并在TOWebViewController.h中说明了以下内容:

/**
 The web view used to display the HTML content. You can access it through this
 read-only property if you need to anything specific, such as having it execute arbitrary JS code.

 @warning Usage of the web view's delegate property is reserved by this view controller. Do not set it to another object.
 */
@property (nonatomic,readonly)  UIWebView *webView;

因此,通过直接访问该课程,您似乎不需要与之交互。

您很可能只想设置url

/** 
 Get/set the current URL being displayed. (Will automatically start loading) 
 */
@property (nonatomic,strong)    NSURL *url;

所以试试:

- (void)request {

     NSURL *url=[NSURL URLWithString:@"http://www.google.com"]

     _webViewController.url = url;

 }

答案 1 :(得分:0)

您应该尝试使用childViewController。

参考文件UIViewController Class Reference

以下是您可能需要致电的基本方法:

addChildViewController:

removeFromParentViewController:

willMoveToParentViewController:

didMoveToParentViewController:

答案 2 :(得分:0)

我最好的猜测是你真正想要的是在“父”视图控制器中显示UIWebView,并且不需要“子”视图控制器。

为什么不这样做?

@interface ViewController : UIViewController

@property (weak) IBOutlet UIWebView *webView; // set outlet in IB

@end

@implementation ViewController

// ...

- (void)request {
     NSURL *url=[NSURL URLWithString: @"http://www.google.com"];
     NSURLRequest *request = [NSURLRequest requestWithURL: url];

     [self.webView loadRequest:request];
}

@end