开发者

Can't access UIView in external method

I'm new to Objective-C, so the way I'm going about this might be ludicrous, but here goes:

I have a login form in my iPhone application. When the user has entered their credentials, they hit Done in the top right corner, which triggers an IBAction and a custom progress indicator pops up. I've created this indicator by using a class containing an instance method named showProgressIndicator. showProgressIndicator creates and returns a UIView, which I then add to my view like so:

ProgressIndicatorElement *ProgressIndicator = [[ProgressIndicatorElement alloc] init];
box = [ProgressIndicator showProgressIndicator];
[self.view addSubview:box];

I have of course declared box as a UIView in my header file. The progress indicator pops up beautifully and in the meantime I'm doing a behind-the-scenes URL request that, when finished, calls another method in my view controller named receivedServerResponse. Now, what I want to do is to remove the progress indicator, which is why I'm doing this:

- (void)receivedServerResponse {
    [box removeFromSuperview];
}

But nothing happens at all. I'm not getting any errors or warnings, and the code is being highlighted just as if everything was running smoothly. I've tried retaining the indicator in my IBAction, but that doesn't help either.

Hope you can help out!

Updated:

Here is the showProgressIndicator method:

- (UIView *)showProgressIndicator {
    box = [[UIView alloc] initWithFrame:CGRectMake(85, 190, 210, 140)];
    box.backgroundColor = [UIColor colorWithRed:0.0 / 255 green:0.0 / 255 blue:0.0 /开发者_StackOverflow中文版 255 alpha:.6];
    box.layer.cornerRadius = 8;

    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    spinner.layer.frame = CGRectMake((box.layer.bounds.size.width - spinner.layer.bounds.size.width)/2, 20, spinner.layer.bounds.size.width, spinner.layer.bounds.size.height);
    [spinner startAnimating];
    [box addSubview:spinner];

    UILabel *titleInBox = [[UILabel alloc] initWithFrame:CGRectMake(0, 65, 150, 20)];
    titleInBox.font = [UIFont boldSystemFontOfSize:16];
    titleInBox.textColor = [UIColor whiteColor];
    titleInBox.textAlignment = UITextAlignmentCenter;
    titleInBox.backgroundColor = [UIColor clearColor];
    titleInBox.text = @"Authorizing...";
    [box addSubview:titleInBox];

    return box;
}

Second update:

@Deepak just pointed out in the comments that I might be running two different instances of my view controller, which actually seems to be the case. In the external class that handles the aforementioned URL request, I get back to the view controller's receivedServerResponse method by doing this:

- (void)requestFinished:(ASIHTTPRequest *)request {
    NSString *responseString = [request responseString];
    SignInViewController *viewController = [[SignInViewController alloc] init];
    [viewController receivedServerResponse];
}

Without spreading myself too thin (probably too late ;)), ASIHTTPRequest is set up so that if you call one method that performs an asynchronous URL request, a predefined method called requestFinished (above) is called, which is why I've had to call my view controller this way, because I can't access the returned value in an easier way (that I know of).


Creating a new instance of SignInViewController is not the correct way. It only seems correct to maintain a weak reference (assigned property) of the SignInViewController object. Say your class is RequestHandler.

@interface RequestHandler: [..] {
}

@property (nonatomic, assign) SignInViewController * signInViewController;

@end

@implementation RequestHandler
@synthesize signInViewController;
[..]
- (void)requestFinished:(ASIHTTPRequest *)request {
    NSString *responseString = [request responseString];
    [signInViewController receivedServerResponse];
}
@end

So when you're creating a RequestHandler object within the SignInViewController instance, you do,

RequestHandler * requestHandler = [[RequestHandler alloc] init];
requestHandler.signInViewController = self;
[..]

Note, you can also look at delegation and notifications.


I think part of the problem may be with memory management. If showProgressIndicator does not return an autoreleased object, try releasing box after adding it as a subview, like so:

[self.view addSubview:box];
[box release];

box may not disappear if box is not deallocated when removed from the superview.

My other recommendation is that instead of doing it the way you are doing, creating a view, adding it, and then trying to removing it, you might want to try adding box as a subview when the login view is created and setting its hidden property to YES then unhiding it later when necessary.

Based on your update: You have some memory management issues in showProgressIndicator. Whenever you alloc an object, you should release it. In this case, release all of your variables after adding them as subviews as I mentioned above. box however should be returned as an autoreleased object since showProgressIndicator does not know when it will need to be released. For that you should replace return box; with return [box autorelease];


You need to send the activity indicator a stopAnimating message when you want the animation to stop. There's no need to remove it from its superview; instead, simply make sure that its hidesWhenStopped property is set to YES.


How about adding box view on window in appDelgate? Give a tag to your boxView and in the remove method get the boxView back by using tag. For example if you give tag 99

- (void)receivedServerResponse {
    UIView *box = [window viewWithTag:99];
    [box removeFromSuperview];
}

also you don't need to declare an instance variable in header file. and you can access progress indicator anywhere in the application.


Without spreading myself too thin (probably too late ;)), ASIHTTPRequest is set up so that if you call one method that performs an asynchronous URL request, a predefined method called requestFinished (above) is called, which is why I've had to call my view controller this way, because I can't access the returned value in an easier way (that I know of).

ASIHTTPRequest calls -requestFinished: on the object you set as the request's delegate. You should design your classes such that this delegate object either has a reference to the view controller you want it to act on or has some means of notifying that view controller to take action. The easiest solution might be to make the controller the request's delegate.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜