1

.h file:

@interface MyView: UIViewController <UIWebViewDelegate> {
    UIWebView *webView;
}

.m file:

-(void)viewDidLoad {
    CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
    webView= [[UIWebView alloc] initWithFrame:webFrame];
    webView.delegate = self;
    [self.view addSubview:webView];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]];
}

-(void)webViewDidStartLoad:(UIWebView *)webView {
    NSLog(@"start");    
}   

-(void)webViewDidFinishLoad:(UIWebView *)webView {
    NSLog(@"finish");   
}

-(void)dealloc {
    webView.delegate = nil;
    webView = nil;
}

create MyView in another ViewController:

MyView * myView = [[MyView alloc] init];
[self.view addSubView:myView.view];
myView = nil;

myView is set to be nil so its delegate is also destroyed.

To solve this, remove the sentence of myView = nil.

Thank you guys who answered me. I did learnt from your answers.

Cœur
  • 37,241
  • 25
  • 195
  • 267
fmchan
  • 760
  • 1
  • 11
  • 29

2 Answers2

1

As srikanth said myview is a controller and it needs to be told its on screen and retained so it doesnt go away AND that is your immediate the real issue in your case (I think). Nobody retains myView. make it a member variable so it isnt released directly after it gave you its view :).

@interface MyCallerWithDoesTheASI : NSObject {
    MyView *myView;
}
@end

or do it right and add it like srikanth said ::

[self addChildViewController:myView];
[self.view addSubview:myView.view];
[myView didMoveToParentViewController:self];

or for example and present myView modal

[self presentViewController:myView animated:NO completion:^{ [[myView dismiss...]; }
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
  • Thank you Daij-Djan. I do also think it probably related to memory management attributes. and I did do this: [self.view addSubview:myView.view]; already. Finally, I figured out UIWebViewDelegate has not been called because I typed myView = nil; after I added myView.view to be the subview of self.view. Anyways, thank you all who answered me! – fmchan Dec 14 '12 at 15:01
  • Btw, I have another extensive question asked here. I hope you may also have a look. Thank you! – fmchan Dec 14 '12 at 15:12
0

MyView is a view controller. And you are treating it like a view, in the second case. If you want to add a view of a view controller directly, it will not get all of the view controller methods called. To be able to get the correct functionality, you will have to do some thing different.

Assuming you are in another view controller then in the other view controller, when ever you want to add the view of MyView view controller, do the following

[self addChildViewController:myView];
[self.view addSubview:myView.view];
[myView didMoveToParentViewController:self];

But even before doing that, please use correct naming convention. If you are naming a subclass of UIViewController as MyView, you will confuse yourself. Name it as MyViewController. That way, you will be able to keep track of stuff better.

Read more about view controller containment for your problem.

Srikanth
  • 1,725
  • 2
  • 10
  • 11
  • sorry, just type something wrong here. I did do this: [self.view addSubView:myView.view]; and no error or warning in the program. – fmchan Dec 13 '12 at 14:52
  • It wont give you a warning, because what you are doing is not wrong. Remember though, that you are just adding a view to your current view. View is only front end. View will not know about the methods in the view controller it belongs to. Viewcontroller knows about its view. View does not know about its view controller necessarily. That is the reason, you will see the added view, but nothing will happen if you click on a button etc on the view. – Srikanth Dec 13 '12 at 14:55
  • If I create MyView, which is a UIViewController in another UIViewController directly just like what I typed. The functions of UIWebViewDelegate can be called properly. However, if I use AsiHttpRequest and call a function which creates MyView after it called back. MyView can be called and initialized but no functions of UIWebViewDelegate can be called. This is the problem I am meeting. – fmchan Dec 13 '12 at 14:58
  • sorry for making you misunderstand. Anyways, thanks your kind reply. And I still hope someone may help me :) – fmchan Dec 13 '12 at 15:08
  • 1
    @Srikanth you can add a VC's view ok.. you need to retain the VC though and that what he doesnt do. – Daij-Djan Dec 13 '12 at 16:05
  • you're correct on all other fronts and I would really urge fmchan to do all you said! – Daij-Djan Dec 13 '12 at 16:06