开发者

removeFromSuperview is not removing view calling from another class

开发者 https://www.devze.com 2023-02-07 00:30 出处:网络
i am getting the web services from the .net web server. while in the process (getting data) iam displaying a开发者_Python百科 subview with activity indicator.

i am getting the web services from the .net web server.

while in the process (getting data) i am displaying a开发者_Python百科 subview with activity indicator.

After completing getting data i need to close that view.

i have two classes one is myclassviewcontroller,webservices

Basically i am writing code to get web services webservices.

In webservices class at

-(void)connectionDidFinishLoading:(NSURLConnection *)connection i call myclass like this.

myclassviewcontroller *obj = [[myclassviewcontroller alloc]init];
[obj mymethod];

At myclassviewcontroller i write this code for my method.

  • (void)mymethod {

    [loadview removeFromSuperview]; }

the method is executed but view is not removed.

I already declared it in myclassviewcontroller.h class also.

i am checking this by keeping some text in NSlog

But if i calling this mymethod in myclassviewcontroller.m using timer then it removes view.

what the wrong.

can any one please help me.

I think it may be understand what is my problem.Let me place comment if not.

Thank u in advance.


I believe the problem with your code is how you access the myclassviewcontroller. It must have already been on the screen while the data was loading, so creating a new instance of that class and calling a method against one of it's uninitialized members (loadview) does nothing.

myclassviewcontroller *obj = [[myclassviewcontroller alloc]init];
// here object has just been initialized 
//    (it is not the same instance as the one on screen)
[obj mymethod];

If obj was a reference to the actual viewcontroller that is on screen, you could easily call:

[obj.loadview removeFromSuperview];

or

[obj mymethod]; // if you wanted to add more code in that function

So, the real problem is that you accessing a different instance of myclassviewcontroller than the one which is actually on screen. You need a variable holding some reference to the correct instance of myclassviewcontroller to access the loadview ivar.

In webservices.h:

@interface webservices : NSObject {
...

// This ivar will have to be set when webservices is initialized
myclassviewcontroller * viewController;
}
@property (nonatomic, retain) myclassviewcontroller * viewController;

and webservices.m would need to @synchronize viewController.

Then in connectionDidFinishLoading: you can just call [viewController.loadview removeFromSuperview];


the problem could be that you instantiate your myclassviewcontroller when "loadview" is already allocated by your "main" class but "invisible" in your myclassviewcontroller, so your new instance of myclassviewcontroller doesn't really know who "loadview" is...

i mean: loadview is allocated and added to the mainView (in the same class where you allocate "myclassviewcontroller"...) but then you try to remove it not in your mainView, but in myclassviewcontroller...

try to modify your method this way:

  (void)mymethod {
    if (loadview!=nil){
      NSLog(@"I'm here...");
      [loadview removeFromSuperview];
    }
  }

to see if "loadview" exist when and WHERE you call the method (in myclassviewcontroller)

luca

0

精彩评论

暂无评论...
验证码 换一张
取 消